"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const utils = require("../../../utils/utils.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("../../../store/modules/designStore/designStore.js");
require("../../../../node_modules/.pnpm/vue3-lazyload@0.2.5-beta_a6dezdz3je235ci4e3tu4rayna/node_modules/vue3-lazyload/dist/vue3-lazyload.esm.js");
require("../../../../node_modules/.pnpm/vue@3.3.4/node_modules/vue/dist/vue.runtime.esm-bundler.js");
require("../../../components/GoSkeleton/index.vue.js");
require("../../../components/GoLoading/index.js");
require("../../../settings/designSetting.js");
const chartEditStore = require("../../../store/modules/chartEditStore/chartEditStore.js");
const chartEditStore_d = require("../../../store/modules/chartEditStore/chartEditStore.d.js");
const chartHistoryStore = require("../../../store/modules/chartHistoryStore/chartHistoryStore.js");
const chartLayoutStore = require("../../../store/modules/chartLayoutStore/chartLayoutStore.js");
const chartLayoutStore_d = require("../../../store/modules/chartLayoutStore/chartLayoutStore.d.js");
const index = require("../../../packages/index.js");
const eventEnum = require("../../../enums/eventEnum.js");
const publicConfig = require("../../../packages/public/publicConfig.js");
const merge = require("../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/merge.js");
const canvasVersionUpdatePolyfill = (object) => {
  return object;
};
const componentVersionUpdatePolyfill = (newObject, sources) => {
  try {
    if (sources.id) {
      const hasVnodeBeforeMount = "vnodeBeforeMount" in sources.events;
      const hasVnodeMounted = "vnodeMounted" in sources.events;
      if (hasVnodeBeforeMount) {
        newObject.events.advancedEvents.vnodeBeforeMount = sources == null ? void 0 : sources.events.vnodeBeforeMount;
      }
      if (hasVnodeMounted) {
        newObject.events.advancedEvents.vnodeMounted = sources == null ? void 0 : sources.events.vnodeMounted;
      }
      if (hasVnodeBeforeMount || hasVnodeMounted) {
        sources.events = {
          baseEvent: {
            [eventEnum.BaseEvent.ON_CLICK]: void 0,
            [eventEnum.BaseEvent.ON_DBL_CLICK]: void 0,
            [eventEnum.BaseEvent.ON_MOUSE_ENTER]: void 0,
            [eventEnum.BaseEvent.ON_MOUSE_LEAVE]: void 0
          },
          advancedEvents: {
            [eventEnum.EventLife.VNODE_MOUNTED]: void 0,
            [eventEnum.EventLife.VNODE_BEFORE_MOUNT]: void 0
          },
          interactEvents: []
        };
      }
      return newObject;
    }
  } catch (error) {
    return newObject;
  }
};
const componentMerge = (newObject, sources, notComponent = false) => {
  componentVersionUpdatePolyfill(newObject, sources);
  if (notComponent)
    return merge.default(newObject, sources);
  const option = sources.option;
  if (!option)
    return merge.default(newObject, sources);
  sources.option = void 0;
  if (option) {
    return {
      ...merge.default(newObject, sources),
      option
    };
  }
};
const useSync = () => {
  const chartEditStore$1 = chartEditStore.useChartEditStore();
  const chartHistoryStore$1 = chartHistoryStore.useChartHistoryStore();
  const chartLayoutStore$1 = chartLayoutStore.useChartLayoutStore();
  const updateComponent = async (projectData, isReplace = false, changeId = false) => {
    if (isReplace) {
      chartEditStore$1.componentList = [];
      chartHistoryStore$1.clearBackStack();
      chartHistoryStore$1.clearForwardStack();
    }
    projectData.editCanvasConfig = canvasVersionUpdatePolyfill(projectData.editCanvasConfig);
    projectData.componentList.forEach(async (e) => {
      const intComponent = (target) => {
        if (!window["$vue"].component(target.chartConfig.chartKey)) {
          window["$vue"].component(target.chartConfig.chartKey, index.fetchChartComponent(target.chartConfig));
          window["$vue"].component(target.chartConfig.conKey, index.fetchConfigComponent(target.chartConfig));
        }
      };
      if (e.isGroup) {
        e.groupList.forEach((groupItem) => {
          intComponent(groupItem);
        });
      } else {
        intComponent(e);
      }
    });
    const create = async (_componentInstance, callBack) => {
      let newComponent = await index.createComponent(_componentInstance.chartConfig);
      if (_componentInstance.chartConfig.redirectComponent) {
        _componentInstance.chartConfig.dataset && (newComponent.option.dataset = _componentInstance.chartConfig.dataset);
        newComponent.chartConfig.title = _componentInstance.chartConfig.title;
        newComponent.chartConfig.chartFrame = _componentInstance.chartConfig.chartFrame;
      }
      if (callBack) {
        if (changeId) {
          callBack(componentMerge(newComponent, { ..._componentInstance, id: utils.getUUID() }));
        } else {
          callBack(componentMerge(newComponent, _componentInstance));
        }
      } else {
        if (changeId) {
          chartEditStore$1.addComponentList(
            componentMerge(newComponent, { ..._componentInstance, id: utils.getUUID() }),
            false,
            true
          );
        } else {
          chartEditStore$1.addComponentList(componentMerge(newComponent, _componentInstance), false, true);
        }
      }
    };
    for (const key in projectData) {
      if (key === chartEditStore_d.ChartEditStoreEnum.COMPONENT_LIST) {
        let loadIndex = 0;
        const listLength = projectData[key].length;
        for (const comItem of projectData[key]) {
          let percentage = parseInt((parseFloat(`${++loadIndex / listLength}`) * 100).toString());
          chartLayoutStore$1.setItemUnHandle(chartLayoutStore_d.ChartLayoutStoreEnum.PERCENTAGE, percentage);
          if (comItem.isGroup) {
            let groupClass = new publicConfig.PublicGroupConfigClass();
            if (changeId) {
              groupClass = componentMerge(groupClass, { ...comItem, id: utils.getUUID() });
            } else {
              groupClass = componentMerge(groupClass, comItem);
            }
            const targetList = [];
            for (const groupItem of comItem.groupList) {
              await create(groupItem, (e) => {
                targetList.push(e);
              });
            }
            groupClass.groupList = targetList;
            chartEditStore$1.addComponentList(groupClass, false, true);
          } else {
            await create(comItem);
          }
          if (percentage === 100) {
            chartHistoryStore$1.clearBackStack();
            chartHistoryStore$1.clearForwardStack();
          }
        }
      } else {
        if (key !== "editCanvasConfig" && key !== "requestGlobalConfig")
          return;
        componentMerge(chartEditStore$1[key], projectData[key], true);
      }
    }
    chartLayoutStore$1.setItemUnHandle(chartLayoutStore_d.ChartLayoutStoreEnum.PERCENTAGE, 0);
  };
  return {
    updateComponent
  };
};
exports.useSync = useSync;
