import { defineComponent, ref, resolveComponent, openBlock, createElementBlock, createVNode, withCtx, createElementVNode, createTextVNode, unref, createBlock, createCommentVNode } from "vue";
import { M as MonacoEditor } from "./EditorWorker-c209331a.js";
import { S as SavePageEnum, s as setSessionStorage, n as StorageEnum, a1 as JSONStringify, d as goDialog, i as icon, $ as getSessionStorageInfo, p as setTitle, a2 as JSONParse, _ as _export_sfc } from "./index-e2623483.js";
const _hoisted_1 = { class: "go-edit" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  setup(__props) {
    const { ChevronBackOutlineIcon, DownloadIcon, AnalyticsIcon } = icon.ionicons5;
    const showOpenFilePicker = window.showOpenFilePicker;
    const content = ref("");
    window["$message"].warning("请不要刷新此窗口！");
    async function getDataBySession() {
      const localStorageInfo = await getSessionStorageInfo();
      setTitle(`编辑-${localStorageInfo.editCanvasConfig.projectName}`);
      content.value = JSONStringify(localStorageInfo);
    }
    setTimeout(getDataBySession);
    function importJSON() {
      goDialog({
        message: "导入数据将覆盖内容，此操作不可撤回，是否继续？",
        isMaskClosable: true,
        transformOrigin: "center",
        onPositiveCallback: async () => {
          try {
            const files = await showOpenFilePicker();
            const file = await files[0].getFile();
            const fr = new FileReader();
            fr.readAsText(file);
            fr.onloadend = () => {
              content.value = (fr.result || "").toString();
            };
            window["$message"].success("导入成功！");
          } catch (error) {
            window["$message"].error("导入失败，请检查文件是否损坏！");
            console.log(error);
          }
        }
      });
    }
    window.opener.addEventListener(SavePageEnum.CHART, (e) => {
      window["$message"].success("正在进行更新...");
      setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, [e.detail]);
      content.value = JSONStringify(e.detail);
    });
    document.addEventListener("keydown", function(e) {
      if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        e.preventDefault();
        updateSync();
      }
    });
    async function updateSync() {
      if (!window.opener) {
        return window["$message"].error("源窗口已关闭，视图同步失败！");
      }
      goDialog({
        message: "是否覆盖源视图内容? 此操作不可撤！",
        isMaskClosable: true,
        transformOrigin: "center",
        onPositiveCallback: () => {
          try {
            const detail = JSONParse(content.value);
            delete detail.id;
            window.opener.dispatchEvent(new CustomEvent(SavePageEnum.JSON, { detail }));
            window["$message"].success("正在同步内容...");
          } catch (e) {
            window["$message"].error("内容格式有误");
            console.log(e);
          }
        }
      });
    }
    window.onbeforeunload = () => {
      if (window.opener) {
        window.opener.dispatchEvent(new CustomEvent(SavePageEnum.CLOSE));
      }
    };
    return (_ctx, _cache) => {
      const _component_n_text = resolveComponent("n-text");
      const _component_n_icon = resolveComponent("n-icon");
      const _component_n_button = resolveComponent("n-button");
      const _component_n_tag = resolveComponent("n-tag");
      const _component_n_space = resolveComponent("n-space");
      const _component_n_layout_header = resolveComponent("n-layout-header");
      const _component_n_layout_content = resolveComponent("n-layout-content");
      const _component_n_layout = resolveComponent("n-layout");
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createVNode(_component_n_layout, null, {
          default: withCtx(() => [
            createVNode(_component_n_layout_header, {
              class: "go-edit-header go-px-5 go-flex-items-center",
              bordered: ""
            }, {
              default: withCtx(() => [
                createElementVNode("div", null, [
                  createVNode(_component_n_text, { class: "go-edit-title go-mr-4" }, {
                    default: withCtx(() => [
                      createTextVNode("页面在线编辑器")
                    ]),
                    _: 1
                  }),
                  unref(showOpenFilePicker) ? (openBlock(), createBlock(_component_n_button, {
                    key: 0,
                    class: "go-mr-3",
                    size: "medium",
                    onClick: importJSON
                  }, {
                    icon: withCtx(() => [
                      createVNode(_component_n_icon, null, {
                        default: withCtx(() => [
                          createVNode(unref(DownloadIcon))
                        ]),
                        _: 1
                      })
                    ]),
                    default: withCtx(() => [
                      createTextVNode(" 导入 ")
                    ]),
                    _: 1
                  })) : createCommentVNode("", true)
                ]),
                createVNode(_component_n_space, null, {
                  default: withCtx(() => [
                    createVNode(_component_n_tag, {
                      bordered: false,
                      type: "warning"
                    }, {
                      default: withCtx(() => [
                        createTextVNode(" 「Ctrl + S 更新视图」 ")
                      ]),
                      _: 1
                    }),
                    unref(showOpenFilePicker) ? (openBlock(), createBlock(_component_n_button, {
                      key: 0,
                      class: "go-mr-3",
                      size: "medium",
                      onClick: updateSync
                    }, {
                      icon: withCtx(() => [
                        createVNode(_component_n_icon, null, {
                          default: withCtx(() => [
                            createVNode(unref(AnalyticsIcon))
                          ]),
                          _: 1
                        })
                      ]),
                      default: withCtx(() => [
                        createTextVNode(" 保存 ")
                      ]),
                      _: 1
                    })) : createCommentVNode("", true)
                  ]),
                  _: 1
                })
              ]),
              _: 1
            }),
            createVNode(_component_n_layout_content, null, {
              default: withCtx(() => [
                createVNode(unref(MonacoEditor), {
                  modelValue: content.value,
                  "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => content.value = $event),
                  language: "json",
                  editorOptions: {
                    lineNumbers: "on",
                    minimap: { enabled: true }
                  }
                }, null, 8, ["modelValue"])
              ]),
              _: 1
            })
          ]),
          _: 1
        })
      ]);
    };
  }
});
const index_vue_vue_type_style_index_0_scoped_9c28ebc7_lang = "";
const index = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-9c28ebc7"]]);
export {
  index as default
};
