import { AnyAction } from "@reduxjs/toolkit"
import { REDUX_ACTION_FROM } from "@/middleware/undoRedo/interface"
// import { ComponentNode } from "@/redux/currentApp/editor/components/componentsState"
import { changeDisplayNameHelperWhenUndoRedo } from "../componentNode/changeDisplayNameHelper"
import {
  addActionItemWhenUndoRedo,
  removeActionItemWhenUndoRedo,
  updateActionDisplayNameReducerWhenUndoRedo,
  updateActionItemReducerWhenUndoRedo,
} from "./undoRedoMethod/action"
import { ComponentMapNode, ComponentTreeNode } from "../generators/interface"
 

export const reduxActionDependOnRestAPI = async (
  actions: AnyAction[],
  from: REDUX_ACTION_FROM,
  aaaStore,
) => {
  for (let i = 0; i < actions.length; i++) {
    const action = actions[i]
    switch (action.type) {
      case "action/addActionItemReducer": {
        try {
          const newPayload = await addActionItemWhenUndoRedo(action.payload)
          aaaStore.dispatch({
            ...action,
            payload: newPayload,
            from,
          })
        } catch (e) {
          return
        }

        break
      }
      case "action/removeActionItemReducer": {
        try {
          await removeActionItemWhenUndoRedo(action.payload.displayName,aaaStore)
          aaaStore.dispatch({
            ...action,
            from,
          })
        } catch (e) {
          return
        }
        break
      }
      case "action/updateActionItemReducer": {
        try {
          await updateActionItemReducerWhenUndoRedo(action.payload,aaaStore)
          aaaStore.dispatch({
            ...action,
            from,
          })
        } catch (e) {
          return
        }
        break
      }
      case "action/updateActionDisplayNameReducer": {
        try {
          await updateActionDisplayNameReducerWhenUndoRedo(
            action.payload.oldDisplayName,
            action.payload.newDisplayName,
            aaaStore
          )
          aaaStore.dispatch({
            ...action,
            from,
          })
        } catch (e) {
          return
        }
        break
      }
      case "components/addComponentReducer": {
        const originNode = action.payload
        const newOriginNodeByChangeDisplayName = originNode.map(
          (item: ComponentMapNode) => changeDisplayNameHelperWhenUndoRedo(item as unknown as ComponentTreeNode),
        )

        aaaStore.dispatch({
          ...action,
          from,
          payload: newOriginNodeByChangeDisplayName,
        })
        break
      }
      case "components/addTargetPageSectionReducer": {
        if (action.payload.originSectionNode) {
          const newOriginNodeByChangeDisplayName =
            changeDisplayNameHelperWhenUndoRedo(
              action.payload.originSectionNode,
            )
          aaaStore.dispatch({
            ...action,
            from,
            payload: {
              ...action.payload,
              originSectionNode: newOriginNodeByChangeDisplayName,
            },
          })
        }
        break
      }
      case "components/addSectionViewReducer": {
        if (Array.isArray(action.payload.originChildrenNode)) {
          const newOriginNodeByChangeDisplayName =
            action.payload.originChildrenNode.map((item: ComponentMapNode) =>
              changeDisplayNameHelperWhenUndoRedo(item as unknown as ComponentTreeNode),
            )
          aaaStore.dispatch({
            ...action,
            from,
            payload: {
              ...action.payload,
              originChildrenNode: newOriginNodeByChangeDisplayName,
            },
          })
        }
        break
      }
      case "components/addPageNodeWithSortOrderReducer": {
        const originNode = action.payload
        const newOriginNodeByChangeDisplayName =
          changeDisplayNameHelperWhenUndoRedo(originNode)
        aaaStore.dispatch({
          ...action,
          from,
          payload: {
            ...action.payload,
            originChildrenNode: newOriginNodeByChangeDisplayName,
          },
        })
        break
      }
      case "components/updateTargetPageLayoutReducer": {
        const originNode = action.payload.originPageNode
        const newOriginNodeByChangeDisplayName =
          changeDisplayNameHelperWhenUndoRedo(originNode)
        aaaStore.dispatch({
          ...action,
          from,
          payload: {
            ...action.payload,
            originChildrenNode: newOriginNodeByChangeDisplayName,
          },
        })
        break
      }
      default: {
        aaaStore.dispatch({
          ...action,
          from,
        })
      }
    }
  }
}
