import { AnyAction } from "@reduxjs/toolkit"
import { cloneDeep } from "lodash"
// import { createMessage } from "@IMPAKTAPPS-design/react"
// import i18n from "@/i18n/config"
import { REDUX_ACTION_FROM } from "@/middleware/undoRedo/interface"
import { IMPAKTAPPSSnapshot } from "@/page/App/components/dotPanel/constant/snapshotNew"
// import { getExecutionWidgetLayoutInfo } from "@/redux/currentApp/executionTree/executionSelector"
import { reduxActionDependOnRestAPI } from "./antonymyRule"
import { CircularStack } from "./circularStack"
import { getExecutionResult } from "@/redux/currentApp/executionTree/executionSelector"

export const UNDO_REDO_STACK_MAX_LENGTH = 30

// const message = createMessage()

export class IMPAKTAPPS_UNDO_REDO {
  undoStack: CircularStack<AnyAction[]>
  redoStack: CircularStack<AnyAction[]>

  constructor(maxStackLength: number = UNDO_REDO_STACK_MAX_LENGTH) {
    this.undoStack = new CircularStack<AnyAction[]>(maxStackLength)
    this.redoStack = new CircularStack<AnyAction[]>(maxStackLength)
  }

  pushToUndoStack(undoAction: AnyAction[]) {
    if (!this.redoStack.isEmpty() && !undoAction[0].from) {
      this.redoStack.clear()
    }
    this.undoStack.push(undoAction)
  } 

  popFromUndoStack(aaaStore) {
    const rootState = cloneDeep(aaaStore.getState())
    const snapShot = getExecutionResult(rootState)
    IMPAKTAPPSSnapshot.setSnapshot(snapShot)
    if (this.undoStack.isEmpty()) {
      // message.warning({
      //   content: i18n.t("frame.message.undo.nothing"),
      // })
    } else {
      const info = this.undoStack.pop() as AnyAction[]

      reduxActionDependOnRestAPI(info, REDUX_ACTION_FROM.UNDO,aaaStore)
    }
  }

  modifyUndoStackAtLast(undoAction: AnyAction[], isRedo: boolean = false) {
    if (this.undoStack.isEmpty()) {
      return
    }
    const lastUndoAction = this.undoStack.pop() as AnyAction[]
    if (isRedo) {
      this.undoStack.push([...lastUndoAction, ...undoAction])
    } else {
      this.undoStack.push([...undoAction, ...lastUndoAction])
    }
  }

  pushToRedoStack(redoAction: AnyAction[]) {
    this.redoStack.push(redoAction)
  }

  popFromRedoStack(aaaStore) {
    const rootState = cloneDeep(aaaStore.getState())
    const snapShot = getExecutionResult(rootState)
    IMPAKTAPPSSnapshot.setSnapshot(snapShot)
    if (this.redoStack.isEmpty()) {
      // message.warning({
      //   content: i18n.t("frame.message.redo.nothing"),
      // })
    } else {
      const info = this.redoStack.pop() as AnyAction[]
      reduxActionDependOnRestAPI(info, REDUX_ACTION_FROM.REDO,aaaStore)
    }
  }
  destroy() {
    this.undoStack.clear()
    this.redoStack.clear()
  }
}

export default new IMPAKTAPPS_UNDO_REDO()
