makeUnwrapped ::
  MonadA m =>
  Sugar.Payload Sugar.Name m ExprGuiM.Payload ->
  Sugar.List m (ExprGuiM.SugarExpr m) -> Widget.Id ->
  ExprGuiM m (ExpressionGui m)
makeUnwrapped pl list myId =
  ExprGuiM.assignCursor myId cursorDest $ do
    bracketOpenLabel <- makeBracketLabel "[" bracketsIdForAnim
    bracketCloseLabel <- makeBracketLabel "]" bracketsIdForAnim
    config <- ExprGuiM.widgetEnv WE.readConfig
    let
      addFirstElemEventMap =
        actionEventMap (Config.listAddItemKeys config) "Add First Item" Sugar.addFirstItem
      onFirstBracket mItem itemPl label = do
        let hg = itemPl ^. Sugar.plData . ExprGuiM.plHoleGuids
        jumpHolesEventMap <-
          hg
          & case mItem of
            Just item
              | Lens.has (Sugar.liExpr . Sugar.rBody . Sugar._BodyHole) item
              -> ExprGuiM.hgMNextHole %~ (storedGuid <|>)
              where
                storedGuid =
                  item ^? Sugar.liExpr . Sugar.rPayload .
                  Sugar.plActions . Lens._Just . Sugar.storedGuid
            _ -> id
          & ExprEventMap.jumpHolesEventMap []
        ExpressionGui.makeFocusableView firstBracketId label
          <&> ExpressionGui.egWidget %~
              Widget.weakerEvents (mappend addFirstElemEventMap jumpHolesEventMap)
    case Sugar.lValues list of
      [] -> onFirstBracket Nothing pl $ ExpressionGui.hbox [bracketOpenLabel, bracketCloseLabel]
      firstValue : nextValues -> do
        (_, firstEdit) <- makeItem firstValue
        nextEdits <- mapM makeItem nextValues
        bracketOpen <-
          onFirstBracket (Just firstValue) (firstValue ^. Sugar.liExpr . Sugar.rPayload) bracketOpenLabel
        let
          nilDeleteEventMap =
            actionEventMap (Config.delKeys config) "Replace nil with hole" Sugar.replaceNil
          addLastEventMap =
            maybe mempty
            ( Widget.keysEventMapMovesCursor (Config.listAddItemKeys config)
              (E.Doc ["Edit", "List", "Add Last Item"])
            . fmap WidgetIds.fromGuid
            ) $ Sugar.lValues list ^? lastLens . Sugar.liMActions . Lens._Just . Sugar.itemAddNext
          closerEventMap = mappend nilDeleteEventMap addLastEventMap
        bracketClose <-
          ExpressionGui.makeFocusableView closeBracketId bracketCloseLabel
          <&> ExpressionGui.egWidget %~ Widget.weakerEvents closerEventMap
        return . ExpressionGui.hbox $ concat
          [[bracketOpen, firstEdit], nextEdits >>= pairToList, [bracketClose]]
  where
    bracketsIdForAnim = WidgetIds.fromGuid $ Sugar.lNilGuid list
    pairToList (x, y) = [x, y]
    closeBracketId = Widget.joinId myId ["close-bracket"]
    itemId = WidgetIds.fromGuid . (^. Sugar.liExpr . Sugar.rPayload . Sugar.plGuid)
    actionEventMap keys doc actSelect =
      maybe mempty
      ( Widget.keysEventMapMovesCursor keys (E.Doc ["Edit", "List", doc])
      . fmap WidgetIds.fromGuid . actSelect) $
      Sugar.lMActions list
    firstBracketId = Widget.joinId myId ["first-bracket"]
    cursorDest = maybe firstBracketId itemId $ Sugar.lValues list ^? Lens.traversed
makeUnwrapped ::
  MonadA m =>
  Sugar.Payload Sugar.Name m ExprGuiM.Payload ->
  Sugar.List m (ExprGuiM.SugarExpr m) -> Widget.Id ->
  ExprGuiM m (ExpressionGui m)
makeUnwrapped pl list myId =
  ExprGuiM.assignCursor myId cursorDest $ do
    bracketOpenLabel <- makeBracketLabel "[" bracketsIdForAnim
    bracketCloseLabel <- makeBracketLabel "]" bracketsIdForAnim
    config <- ExprGuiM.widgetEnv WE.readConfig
    let
      addFirstElemEventMap =
        actionEventMap (Config.listAddItemKeys config) "Add First Item" Sugar.addFirstItem
      onFirstBracket mItem itemPl label = do
        let hg = itemPl ^. Sugar.plData . ExprGuiM.plHoleGuids
        jumpHolesEventMap <-
          hg
          & case mItem of
            Just item
              | Lens.has (Sugar.liExpr . Sugar.rBody . Sugar._BodyHole) item
              -> ExprGuiM.hgMNextHole %~ (storedGuid <|>)
              where
                storedGuid =
                  item ^? Sugar.liExpr . Sugar.rPayload .
                  Sugar.plActions . Lens._Just . Sugar.storedGuid
            _ -> id
          & ExprEventMap.jumpHolesEventMap []
        ExpressionGui.makeFocusableView firstBracketId label
          <&> ExpressionGui.egWidget %~
              Widget.weakerEvents (mappend addFirstElemEventMap jumpHolesEventMap)
    case Sugar.lValues list of
      [] -> onFirstBracket Nothing pl $ ExpressionGui.hbox [bracketOpenLabel, bracketCloseLabel]
      firstValue : nextValues -> do
        (_, firstEdit) <- makeItem firstValue
        nextEdits <- mapM makeItem nextValues
        bracketOpen <-
          onFirstBracket (Just firstValue) (firstValue ^. Sugar.liExpr . Sugar.rPayload) bracketOpenLabel
        let
          nilDeleteEventMap =
            actionEventMap (Config.delKeys config) "Replace nil with hole" Sugar.replaceNil
          addLastEventMap =
            maybe mempty
            ( Widget.keysEventMapMovesCursor (Config.listAddItemKeys config)
              (E.Doc ["Edit", "List", "Add Last Item"])
            . fmap WidgetIds.fromGuid
            ) $ Sugar.lValues list ^? lastLens . Sugar.liMActions . Lens._Just . Sugar.itemAddNext
          closerEventMap = mappend nilDeleteEventMap addLastEventMap
        bracketClose <-
          ExpressionGui.makeFocusableView closeBracketId bracketCloseLabel
          <&> ExpressionGui.egWidget %~ Widget.weakerEvents closerEventMap
        return . ExpressionGui.hbox $ concat
          [[bracketOpen, firstEdit], nextEdits >>= pairToList, [bracketClose]]
  where
    bracketsIdForAnim = WidgetIds.fromGuid $ Sugar.lNilGuid list
    pairToList (x, y) = [x, y]
    closeBracketId = Widget.joinId myId ["close-bracket"]
    itemId = WidgetIds.fromGuid . (^. Sugar.liExpr . Sugar.rPayload . Sugar.plGuid)
    actionEventMap keys doc actSelect =
      maybe mempty
      ( Widget.keysEventMapMovesCursor keys (E.Doc ["Edit", "List", doc])
      . fmap WidgetIds.fromGuid . actSelect) $
      Sugar.lMActions list
    firstBracketId = Widget.joinId myId ["first-bracket"]
    cursorDest = maybe firstBracketId itemId $ Sugar.lValues list ^? Lens.traversed
makeUnwrapped pl list myId =
  ExprGuiM.assignCursor myId cursorDest $ do
    bracketOpenLabel <- makeBracketLabel "[" bracketsIdForAnim
    bracketCloseLabel <- makeBracketLabel "]" bracketsIdForAnim
    config <- ExprGuiM.widgetEnv WE.readConfig
    let
      addFirstElemEventMap =
        actionEventMap (Config.listAddItemKeys config) "Add First Item" Sugar.addFirstItem
      onFirstBracket mItem itemPl label = do
        let hg = itemPl ^. Sugar.plData . ExprGuiM.plHoleGuids
        jumpHolesEventMap <-
          hg
          & case mItem of
            Just item
              | Lens.has (Sugar.liExpr . Sugar.rBody . Sugar._BodyHole) item
              -> ExprGuiM.hgMNextHole %~ (storedGuid <|>)
              where
                storedGuid =
                  item ^? Sugar.liExpr . Sugar.rPayload .
                  Sugar.plActions . Lens._Just . Sugar.storedGuid
            _ -> id
          & ExprEventMap.jumpHolesEventMap []
        ExpressionGui.makeFocusableView firstBracketId label
          <&> ExpressionGui.egWidget %~
              Widget.weakerEvents (mappend addFirstElemEventMap jumpHolesEventMap)
    case Sugar.lValues list of
      [] -> onFirstBracket Nothing pl $ ExpressionGui.hbox [bracketOpenLabel, bracketCloseLabel]
      firstValue : nextValues -> do
        (_, firstEdit) <- makeItem firstValue
        nextEdits <- mapM makeItem nextValues
        bracketOpen <-
          onFirstBracket (Just firstValue) (firstValue ^. Sugar.liExpr . Sugar.rPayload) bracketOpenLabel
        let
          nilDeleteEventMap =
            actionEventMap (Config.delKeys config) "Replace nil with hole" Sugar.replaceNil
          addLastEventMap =
            maybe mempty
            ( Widget.keysEventMapMovesCursor (Config.listAddItemKeys config)
              (E.Doc ["Edit", "List", "Add Last Item"])
            . fmap WidgetIds.fromGuid
            ) $ Sugar.lValues list ^? lastLens . Sugar.liMActions . Lens._Just . Sugar.itemAddNext
          closerEventMap = mappend nilDeleteEventMap addLastEventMap
        bracketClose <-
          ExpressionGui.makeFocusableView closeBracketId bracketCloseLabel
          <&> ExpressionGui.egWidget %~ Widget.weakerEvents closerEventMap
        return . ExpressionGui.hbox $ concat
          [[bracketOpen, firstEdit], nextEdits >>= pairToList, [bracketClose]]
  where
    bracketsIdForAnim = WidgetIds.fromGuid $ Sugar.lNilGuid list
    pairToList (x, y) = [x, y]
    closeBracketId = Widget.joinId myId ["close-bracket"]
    itemId = WidgetIds.fromGuid . (^. Sugar.liExpr . Sugar.rPayload . Sugar.plGuid)
    actionEventMap keys doc actSelect =
      maybe mempty
      ( Widget.keysEventMapMovesCursor keys (E.Doc ["Edit", "List", doc])
      . fmap WidgetIds.fromGuid . actSelect) $
      Sugar.lMActions list
    firstBracketId = Widget.joinId myId ["first-bracket"]
    cursorDest = maybe firstBracketId itemId $ Sugar.lValues list ^? Lens.traversed