<script lang="ts" setup>
import { nextTick, onMounted, ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import * as monaco from 'monaco-editor';
// import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
// import CssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker';
// import HtmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker';
// import JsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
// import TsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker';

defineOptions({
  name: 'InputCodePreview',
});
const emit = defineEmits(['success']);
// const state = reactive<{
//   language: string;
// }>({
//   language: 'javascript',
// });

// globalThis.MonacoEnvironment = {
//   getWorker: (_: string, label: string) => {
//     if (label === 'json') {
//       return new JsonWorker();
//     }
//     if (['css', 'less', 'scss'].includes(label)) {
//       return new CssWorker();
//     }
//     if (['handlebars', 'html', 'razor'].includes(label)) {
//       return new HtmlWorker();
//     }
//     if (['javascript', 'typescript'].includes(label)) {
//       return new TsWorker();
//     }
//     return new EditorWorker();
//   },
// };
const monacoEditorRef = ref();
// 初始化monacoEditor对象
let monacoEditor: any = null;

const initMonacoEditor = () => {
  monacoEditor = monaco.editor.create(monacoEditorRef.value, {
    theme: 'vs-dark', // 主题 vs vs-dark hc-black
    value: '', // 默认显示的值
    language: 'javascript',
    formatOnPaste: true,
    wordWrap: 'on', // 自动换行，注意大小写
    wrappingIndent: 'indent',
    folding: true, // 是否折叠
    foldingHighlight: true, // 折叠等高线
    foldingStrategy: 'indentation', // 折叠方式  auto | indentation
    showFoldingControls: 'always', // 是否一直显示折叠 always | mouSEOver
    disableLayerHinting: true, // 等宽优化
    emptySelectionClipboard: false, // 空选择剪切板
    selectionClipboard: false, // 选择剪切板
    automaticLayout: true, // 自动布局
    codeLens: false, // 代码镜头
    scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕
    colorDecorators: true, // 颜色装饰器
    accessibilitySupport: 'auto', // 辅助功能支持  "auto" | "off" | "on"
    lineNumbers: 'on', // 行号 取值： "on" | "off" | "relative" | "interval" | function
    lineNumbersMinChars: 5, // 行号最小字符   number
    // enableSplitViewResizing: false,
    readOnly: false, // 是否只读  取值 true | false
  });
};

const [Modal, { close, setState, getData }] = useVbenModal({
  draggable: true,
  fullscreen: false,
  onConfirm: async () => {
    try {
      setState({ confirmLoading: true });
      const scriptCode = monacoEditor.getValue();

      close();
      emit('success', scriptCode);
    } finally {
      setState({ confirmLoading: false });
    }
  },
  onOpenChange: async (isOpen: boolean) => {
    if (isOpen) {
      setState({ loading: true });
      const data = getData<Record<string, any>>();
      if (monacoEditor === null) {
        nextTick(() => {
          initMonacoEditor();
        });
      }
      nextTick(() => {
        const { scriptCode, name, language } = data.baseData;
        if (scriptCode) {
          monacoEditor.setValue(scriptCode);
        }

        if (language) {
          monaco.editor.setModelLanguage(monacoEditor.getModel()!, language);
        }
        setState({ title: `${name}`, loading: false });
      });
    }
  },
  title: ' 脚本编辑',
});

onMounted(() => {});
</script>
<template>
  <Modal class="h-[800px] w-[1000px]">
    <div ref="monacoEditorRef" class="h-full w-full"></div>
  </Modal>
</template>
