'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
require('../../../utils/index.js');
require('./config/index.js');
require('../../../../node_modules/.pnpm/@monaco-editor_loader@1.6.1/node_modules/@monaco-editor/loader/lib/es/index.js');
var createNamespace = require('../../../utils/create-namespace.js');
var index = require('../../../../node_modules/.pnpm/@monaco-editor_loader@1.6.1/node_modules/@monaco-editor/loader/lib/es/loader/index.js');
var codeEditor = require('./config/code-editor.js');
var codeThemeList = require('./config/code-theme-list.js');

"use strict";
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{ name: "LlCodeEditor" },
  __name: "code-editor",
  props: codeEditor.codeEditorProps,
  emits: codeEditor.codeEditorEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const bem = createNamespace.createNamespace("code-editor");
    let monaco;
    let editor;
    const editorRef = vue.ref();
    const initMonacoEditor = async () => {
      index.default.config({ "vs/nls": { availableLanguages: { "*": "zh-cn" } } });
      monaco = await index.default.init();
      monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
        noSemanticValidation: true,
        noSyntaxValidation: false
      });
      monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
        target: monaco.languages.typescript.ScriptTarget.ES2020,
        allowNonTsExtensions: true
      });
      editor = monaco.editor.create(editorRef.value, {
        value: props.value,
        language: props.language,
        theme: props.theme,
        ...codeEditor.defaultOptions,
        ...props.options,
        readOnly: props.disabled
      });
      editor.onDidChangeModelContent(() => {
        const value = editor.getValue();
        emits("update:value", value);
        emits("change", value);
      });
      handleChangeTheme(props.theme);
      emits("editor-mounted", editor);
    };
    const handleChangeTheme = async (theme) => {
      const find = codeThemeList.codeThemeList.find((item) => item.value === theme);
      if (find) {
        const themeData = {
          ...find.data
        };
        try {
          monaco.editor.defineTheme(theme, themeData);
          monaco.editor.setTheme(theme);
        } catch (error) {
          monaco.editor.setTheme("vs");
        }
      } else {
        monaco.editor.setTheme("vs");
      }
    };
    vue.watch(
      () => props.value,
      (newValue) => {
        if (editor) {
          const value = editor.getValue();
          if (newValue !== value) {
            editor.setValue(newValue);
          }
        }
      }
    );
    vue.watch(
      () => props.options,
      (newValue) => {
        editor.updateOptions(newValue);
      },
      { deep: true }
    );
    vue.watch(
      () => props.language,
      (newValue) => {
        monaco.editor.setModelLanguage(editor.getModel(), newValue);
      }
    );
    __expose({
      getEditor() {
        return editor;
      }
    });
    vue.onBeforeUnmount(() => {
      editor.dispose();
    });
    vue.onMounted(async () => {
      await initMonacoEditor();
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock(
        "div",
        {
          id: "monaco-editor",
          ref_key: "editorRef",
          ref: editorRef,
          class: vue.normalizeClass(vue.unref(bem).b()),
          style: vue.normalizeStyle({
            width: props.width,
            height: props.height
          })
        },
        null,
        6
        /* CLASS, STYLE */
      );
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=code-editor.vue2.js.map
