import "../../../../node_modules/.pnpm/vue@3.3.4/node_modules/vue/dist/vue.runtime.esm-bundler.mjs";
import { useDesignStore } from "../../../store/modules/designStore/designStore.mjs";
import { editor } from "../../../../node_modules/.pnpm/monaco-editor@0.33.0/node_modules/monaco-editor/esm/vs/editor/editor.api.mjs";
import { ref } from "../../../../node_modules/.pnpm/@vue_reactivity@3.3.4/node_modules/@vue/reactivity/dist/reactivity.esm-bundler.mjs";
import { onBeforeUnmount, nextTick } from "../../../../node_modules/.pnpm/@vue_runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.mjs";
const useMonacoEditor = (language = "javascript") => {
  const designStore = useDesignStore();
  let monacoEditor = null;
  let initReadOnly = false;
  const el = ref(null);
  const onFormatDoc = async () => {
    var _a;
    await ((_a = monacoEditor == null ? void 0 : monacoEditor.getAction("monacoEditor.action.formatDocument")) == null ? void 0 : _a.run());
  };
  const updateVal = (val) => {
    nextTick(async () => {
      monacoEditor == null ? void 0 : monacoEditor.setValue(val);
      initReadOnly && (monacoEditor == null ? void 0 : monacoEditor.updateOptions({ readOnly: false }));
      await onFormatDoc();
      initReadOnly && (monacoEditor == null ? void 0 : monacoEditor.updateOptions({ readOnly: true }));
    });
  };
  const createEditor = (editorOption = {}) => {
    if (!el.value)
      return;
    const javascriptModel = editor.createModel("", language);
    initReadOnly = !!editorOption.readOnly;
    monacoEditor = editor.create(el.value, {
      model: javascriptModel,
      // 是否启用预览图
      minimap: { enabled: false },
      // 圆角
      roundedSelection: true,
      // 主题
      theme: designStore.getDarkTheme ? "vs-dark" : "vs",
      // 主键
      multiCursorModifier: "ctrlCmd",
      // 滚动条
      scrollbar: {
        verticalScrollbarSize: 8,
        horizontalScrollbarSize: 8
      },
      // 行号
      lineNumbers: "off",
      // tab大小
      tabSize: 2,
      //字体大小
      fontSize: 16,
      // 控制编辑器在用户键入、粘贴、移动或缩进行时是否应自动调整缩进
      autoIndent: "advanced",
      // 自动布局
      automaticLayout: true,
      ...editorOption
    });
    return monacoEditor;
  };
  onBeforeUnmount(() => {
    if (monacoEditor)
      monacoEditor.dispose();
  });
  return {
    el,
    updateVal,
    getEditor: () => monacoEditor,
    createEditor,
    onFormatDoc
  };
};
export {
  useMonacoEditor
};
