"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const pinia = require("pinia");
require("../../../../node_modules/.pnpm/vue@3.3.4/node_modules/vue/dist/vue.runtime.esm-bundler.js");
require("../../../../node_modules/.pnpm/screenfull@6.0.2/node_modules/screenfull/index.js");
require("../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_root.js");
require("../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseGetTag.js");
require("../../../../node_modules/.pnpm/html2canvas@1.4.1/node_modules/html2canvas/dist/html2canvas.esm.js");
require("../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseClone.js");
require("../../../../node_modules/.pnpm/crypto-js@4.1.1/node_modules/crypto-js/index.js");
require("../../../enums/pageEnum.js");
require("../../../router/index.js");
require("../designStore/designStore.js");
const plugin = require("../../../utils/plugin.js");
require("../../../components/GoLoading/index.js");
const designSetting = require("../../../settings/designSetting.js");
const chartHistoryStore_d = require("./chartHistoryStore.d.js");
const useChartHistoryStore = pinia.defineStore({
  id: "useChartHistoryStore",
  state: () => ({
    // 后退栈
    backStack: [],
    // 前进栈
    forwardStack: []
  }),
  getters: {
    getBackStack() {
      return this.backStack;
    },
    getForwardStack() {
      return this.forwardStack;
    }
  },
  actions: {
    /**
     * * 新增记录并插入栈
     * @param item 图表实例
     * @param actionType 动作类型
     * @param targetType 对象类型（默认图表）
     */
    createStackItem(item, actionType, targetType = chartHistoryStore_d.HistoryTargetTypeEnum.CHART) {
      this.pushBackStackItem(
        Object.freeze({
          [chartHistoryStore_d.HistoryStackItemEnum.ID]: (/* @__PURE__ */ new Date()).getTime().toString(),
          [chartHistoryStore_d.HistoryStackItemEnum.HISTORY_DATA]: item,
          [chartHistoryStore_d.HistoryStackItemEnum.ACTION_TYPE]: actionType,
          [chartHistoryStore_d.HistoryStackItemEnum.TARGET_TYPE]: targetType
        })
      );
    },
    // * 画布初始化
    canvasInit(canvas) {
      this.createStackItem([canvas], chartHistoryStore_d.HistoryActionTypeEnum.ADD, chartHistoryStore_d.HistoryTargetTypeEnum.CANVAS);
    },
    // * 推入后退栈
    pushBackStackItem(item, notClear = false) {
      if (item instanceof Array)
        this.backStack = [...this.backStack, ...item];
      else
        this.backStack.push(item);
      this.backStack.splice(0, this.backStack.length - designSetting.editHistoryMax);
      if (notClear)
        return;
      this.clearForwardStack();
    },
    // * 推入前进栈
    pushForwardStack(item) {
      if (item instanceof Array)
        this.forwardStack = [...this.forwardStack, ...item];
      else
        this.forwardStack.push(item);
    },
    // * 移出后退栈
    popBackStackItem() {
      if (this.backStack.length > 0) {
        return this.backStack.pop();
      }
    },
    // * 移出前进栈
    popForwardStack() {
      if (this.forwardStack.length > 0) {
        return this.forwardStack.pop();
      }
    },
    // * 清空前进栈
    clearForwardStack() {
      this.forwardStack = [];
    },
    // * 清空后退栈(保留初始化)
    clearBackStack() {
      const canvasHistory = this.getBackStack[0];
      this.backStack = [canvasHistory];
    },
    // * 撤回
    backAction() {
      try {
        plugin.loadingStart();
        if (this.getBackStack.length > 1) {
          const targetData = this.popBackStackItem();
          if (!targetData) {
            plugin.loadingFinish();
            return;
          }
          this.pushForwardStack(targetData);
          plugin.loadingFinish();
          return targetData;
        }
        plugin.loadingFinish();
      } catch (error) {
        plugin.loadingError();
      }
    },
    // * 前进
    forwardAction() {
      try {
        plugin.loadingStart();
        if (this.getForwardStack.length) {
          const targetData = this.popForwardStack();
          if (!targetData) {
            plugin.loadingFinish();
            return;
          }
          this.pushBackStackItem(targetData, true);
          plugin.loadingFinish();
          return targetData;
        }
        plugin.loadingFinish();
      } catch (error) {
        plugin.loadingError();
      }
    },
    // * 新增组件记录
    createAddHistory(item) {
      this.createStackItem(item, chartHistoryStore_d.HistoryActionTypeEnum.ADD, chartHistoryStore_d.HistoryTargetTypeEnum.CHART);
    },
    // * 更新属性记录（大小、图表属性）
    createUpdateHistory(item) {
      this.createStackItem(item, chartHistoryStore_d.HistoryActionTypeEnum.UPDATE, chartHistoryStore_d.HistoryTargetTypeEnum.CHART);
    },
    // * 删除组件记录
    createDeleteHistory(item) {
      this.createStackItem(item, chartHistoryStore_d.HistoryActionTypeEnum.DELETE, chartHistoryStore_d.HistoryTargetTypeEnum.CHART);
    },
    // * 移动组件记录
    createMoveHistory(item) {
      this.createStackItem(item, chartHistoryStore_d.HistoryActionTypeEnum.MOVE, chartHistoryStore_d.HistoryTargetTypeEnum.CHART);
    },
    // * 改变层级组件记录
    createLayerHistory(item, type) {
      this.createStackItem(item, type, chartHistoryStore_d.HistoryTargetTypeEnum.CHART);
    },
    // * 剪切组件记录
    createPasteHistory(item) {
      this.createStackItem(item, chartHistoryStore_d.HistoryActionTypeEnum.CUT, chartHistoryStore_d.HistoryTargetTypeEnum.CHART);
    },
    // * 创建分组
    createGroupHistory(item) {
      this.createStackItem(item, chartHistoryStore_d.HistoryActionTypeEnum.GROUP, chartHistoryStore_d.HistoryTargetTypeEnum.CHART);
    },
    // * 解除分组
    createUnGroupHistory(item) {
      this.createStackItem(item, chartHistoryStore_d.HistoryActionTypeEnum.UN_GROUP, chartHistoryStore_d.HistoryTargetTypeEnum.CHART);
    },
    // * 锁定记录
    createLockHistory(item) {
      this.createStackItem(item, chartHistoryStore_d.HistoryActionTypeEnum.LOCK, chartHistoryStore_d.HistoryTargetTypeEnum.CHART);
    },
    // * 解锁记录
    createUnLockHistory(item) {
      this.createStackItem(item, chartHistoryStore_d.HistoryActionTypeEnum.UNLOCK, chartHistoryStore_d.HistoryTargetTypeEnum.CHART);
    },
    // * 隐藏记录
    createHideHistory(item) {
      this.createStackItem(item, chartHistoryStore_d.HistoryActionTypeEnum.HIDE, chartHistoryStore_d.HistoryTargetTypeEnum.CHART);
    },
    // * 展示记录
    createShowHistory(item) {
      this.createStackItem(item, chartHistoryStore_d.HistoryActionTypeEnum.SHOW, chartHistoryStore_d.HistoryTargetTypeEnum.CHART);
    }
  }
});
exports.useChartHistoryStore = useChartHistoryStore;
