import { getUUID } from "../../../utils/utils.mjs";
import "../../../../node_modules/.pnpm/crypto-js@4.1.1/node_modules/crypto-js/index.mjs";
import "../../../enums/pageEnum.mjs";
import "../../../router/index.mjs";
import "../../../store/modules/designStore/designStore.mjs";
import "../../../../node_modules/.pnpm/vue3-lazyload@0.2.5-beta_a6dezdz3je235ci4e3tu4rayna/node_modules/vue3-lazyload/dist/vue3-lazyload.esm.mjs";
import "../../../../node_modules/.pnpm/vue@3.3.4/node_modules/vue/dist/vue.runtime.esm-bundler.mjs";
import "../../../components/GoSkeleton/index.vue.mjs";
import "../../../components/GoLoading/index.mjs";
import "../../../settings/designSetting.mjs";
import { useChartEditStore } from "../../../store/modules/chartEditStore/chartEditStore.mjs";
import { ChartEditStoreEnum } from "../../../store/modules/chartEditStore/chartEditStore.d.mjs";
import { useChartHistoryStore } from "../../../store/modules/chartHistoryStore/chartHistoryStore.mjs";
import { useChartLayoutStore } from "../../../store/modules/chartLayoutStore/chartLayoutStore.mjs";
import { ChartLayoutStoreEnum } from "../../../store/modules/chartLayoutStore/chartLayoutStore.d.mjs";
import { fetchChartComponent, fetchConfigComponent, createComponent } from "../../../packages/index.mjs";
import { BaseEvent, EventLife } from "../../../enums/eventEnum.mjs";
import { PublicGroupConfigClass } from "../../../packages/public/publicConfig.mjs";
import merge from "../../../../node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/merge.mjs";
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: {
            [BaseEvent.ON_CLICK]: void 0,
            [BaseEvent.ON_DBL_CLICK]: void 0,
            [BaseEvent.ON_MOUSE_ENTER]: void 0,
            [BaseEvent.ON_MOUSE_LEAVE]: void 0
          },
          advancedEvents: {
            [EventLife.VNODE_MOUNTED]: void 0,
            [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(newObject, sources);
  const option = sources.option;
  if (!option)
    return merge(newObject, sources);
  sources.option = void 0;
  if (option) {
    return {
      ...merge(newObject, sources),
      option
    };
  }
};
const useSync = () => {
  const chartEditStore = useChartEditStore();
  const chartHistoryStore = useChartHistoryStore();
  const chartLayoutStore = useChartLayoutStore();
  const updateComponent = async (projectData, isReplace = false, changeId = false) => {
    if (isReplace) {
      chartEditStore.componentList = [];
      chartHistoryStore.clearBackStack();
      chartHistoryStore.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, fetchChartComponent(target.chartConfig));
          window["$vue"].component(target.chartConfig.conKey, fetchConfigComponent(target.chartConfig));
        }
      };
      if (e.isGroup) {
        e.groupList.forEach((groupItem) => {
          intComponent(groupItem);
        });
      } else {
        intComponent(e);
      }
    });
    const create = async (_componentInstance, callBack) => {
      let newComponent = await 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: getUUID() }));
        } else {
          callBack(componentMerge(newComponent, _componentInstance));
        }
      } else {
        if (changeId) {
          chartEditStore.addComponentList(
            componentMerge(newComponent, { ..._componentInstance, id: getUUID() }),
            false,
            true
          );
        } else {
          chartEditStore.addComponentList(componentMerge(newComponent, _componentInstance), false, true);
        }
      }
    };
    for (const key in projectData) {
      if (key === 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.setItemUnHandle(ChartLayoutStoreEnum.PERCENTAGE, percentage);
          if (comItem.isGroup) {
            let groupClass = new PublicGroupConfigClass();
            if (changeId) {
              groupClass = componentMerge(groupClass, { ...comItem, id: 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.addComponentList(groupClass, false, true);
          } else {
            await create(comItem);
          }
          if (percentage === 100) {
            chartHistoryStore.clearBackStack();
            chartHistoryStore.clearForwardStack();
          }
        }
      } else {
        if (key !== "editCanvasConfig" && key !== "requestGlobalConfig")
          return;
        componentMerge(chartEditStore[key], projectData[key], true);
      }
    }
    chartLayoutStore.setItemUnHandle(ChartLayoutStoreEnum.PERCENTAGE, 0);
  };
  return {
    updateComponent
  };
};
export {
  useSync
};
