import { defineComponent, computed, resolveComponent, openBlock, createBlock, withCtx, createElementBlock, Fragment, renderList, resolveDynamicComponent, createElementVNode, toDisplayString } from "vue";
import { u as useChartEditStore, a as useChartHistoryStore, C as ChartEditStoreEnum, g as getUUID, m as merge, f as fetchChartComponent, b as fetchConfigComponent, c as createComponent, P as PublicGroupConfigClass, B as BaseEvent, E as EventLife, d as goDialog, S as SavePageEnum, e as useRoute, l as lodashExports, r as renderIcon, i as icon, h as fetchPathByName, j as PreviewEnum, k as getLocalStorage, n as StorageEnum, s as setSessionStorage, o as routerTurnByPath, _ as _export_sfc } from "./index-e2623483.js";
import { u as useChartLayoutStore, C as ChartLayoutStoreEnum } from "./chartLayoutStore-65bb9d12.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: {
            [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 chartEditStore2 = useChartEditStore();
  const chartHistoryStore = useChartHistoryStore();
  const chartLayoutStore = useChartLayoutStore();
  const updateComponent = async (projectData, isReplace = false, changeId = false) => {
    if (isReplace) {
      chartEditStore2.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) {
          chartEditStore2.addComponentList(
            componentMerge(newComponent, { ..._componentInstance, id: getUUID() }),
            false,
            true
          );
        } else {
          chartEditStore2.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;
            chartEditStore2.addComponentList(groupClass, false, true);
          } else {
            await create(comItem);
          }
          if (percentage === 100) {
            chartHistoryStore.clearBackStack();
            chartHistoryStore.clearForwardStack();
          }
        }
      } else {
        if (key !== "editCanvasConfig" && key !== "requestGlobalConfig")
          return;
        componentMerge(chartEditStore2[key], projectData[key], true);
      }
    }
    chartLayoutStore.setItemUnHandle(ChartLayoutStoreEnum.PERCENTAGE, 0);
  };
  return {
    updateComponent
  };
};
useSync();
const chartEditStore = useChartEditStore();
const syncData = () => {
  goDialog({
    message: "是否覆盖源视图内容，此操作不可撤回?",
    isMaskClosable: true,
    transformOrigin: "center",
    onPositiveCallback: () => {
      window["$message"].success("正在同步编辑器...");
      dispatchEvent(new CustomEvent(SavePageEnum.CHART, { detail: chartEditStore.getStorageInfo }));
    }
  });
};
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  setup(__props) {
    const { BrowsersOutlineIcon, SendIcon, AnalyticsIcon } = icon.ionicons5;
    const chartEditStore2 = useChartEditStore();
    const routerParamsInfo = useRoute();
    const previewHandle = () => {
      const path = fetchPathByName(PreviewEnum.CHART_PREVIEW_NAME, "href");
      if (!path)
        return;
      const { id } = routerParamsInfo.params;
      const previewId = typeof id === "string" ? id : id[0];
      const storageInfo = chartEditStore2.getStorageInfo;
      const sessionStorageInfo = getLocalStorage(StorageEnum.GO_CHART_STORAGE_LIST) || [];
      if (sessionStorageInfo == null ? void 0 : sessionStorageInfo.length) {
        const repeateIndex = sessionStorageInfo.findIndex((e) => e.id === previewId);
        if (repeateIndex !== -1) {
          sessionStorageInfo.splice(repeateIndex, 1, { id: previewId, ...storageInfo });
          setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, sessionStorageInfo);
        } else {
          sessionStorageInfo.push({
            id: previewId,
            ...storageInfo
          });
          setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, sessionStorageInfo);
        }
      } else {
        setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, [{ id: previewId, ...storageInfo }]);
      }
      routerTurnByPath(path, [previewId], void 0, true);
    };
    const sendHandle = () => {
      goDialog({
        message: "保存当前设计，需要表单提交相关信息",
        positiveText: "了然",
        closeNegativeText: true,
        onPositiveCallback: () => {
        }
      });
    };
    const btnList = [
      {
        select: true,
        title: "同步内容",
        type: "primary",
        icon: renderIcon(AnalyticsIcon),
        event: syncData
      },
      {
        select: true,
        title: "预览",
        icon: renderIcon(BrowsersOutlineIcon),
        event: previewHandle
      },
      {
        select: true,
        title: "保存",
        icon: renderIcon(SendIcon),
        event: sendHandle
      }
    ];
    const comBtnList = computed(() => {
      if (chartEditStore2.getEditCanvas.isCodeEdit) {
        return btnList;
      }
      const cloneList = lodashExports.cloneDeep(btnList);
      cloneList.shift();
      return cloneList;
    });
    return (_ctx, _cache) => {
      const _component_n_button = resolveComponent("n-button");
      const _component_n_space = resolveComponent("n-space");
      return openBlock(), createBlock(_component_n_space, { class: "go-mt-0" }, {
        default: withCtx(() => [
          (openBlock(true), createElementBlock(Fragment, null, renderList(comBtnList.value, (item, index2) => {
            return openBlock(), createBlock(_component_n_button, {
              key: item.title,
              type: item.type,
              ghost: "",
              onClick: item.event
            }, {
              icon: withCtx(() => [
                (openBlock(), createBlock(resolveDynamicComponent(item.icon)))
              ]),
              default: withCtx(() => [
                createElementVNode("span", null, toDisplayString(item.title), 1)
              ]),
              _: 2
            }, 1032, ["type", "onClick"]);
          }), 128))
        ]),
        _: 1
      });
    };
  }
});
const index_vue_vue_type_style_index_0_scoped_819c0583_lang = "";
const index = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-819c0583"]]);
export {
  index as default
};
