import React from 'react';
import MonacoEditor, { loader } from '@monaco-editor/react';

// 配置 Monaco Editor 使用本地资源
loader.config({
    paths: {
        vs: '/monaco-editor/min/vs'
    },
    'vs/nls': {
        availableLanguages: {
            '*': 'zh-cn'
        }
    }
});

// 注册 Groovy 语言支持
loader.init().then((monaco) => {
    monaco.languages.register({ id: 'groovy' });
    monaco.languages.setMonarchTokensProvider('groovy', {
        keywords: [
            'def', 'class', 'interface', 'trait', 'extends', 'implements',
            'package', 'import', 'new', 'null', 'true', 'false',
            'if', 'else', 'for', 'while', 'do', 'switch', 'case',
            'break', 'continue', 'return', 'throw', 'try', 'catch',
            'finally', 'this', 'super', 'abstract', 'static', 'final',
            'public', 'protected', 'private', 'void'
        ],
        operators: [
            '=', '>', '<', '!', '~', '?', ':',
            '==', '<=', '>=', '!=', '&&', '||', '++', '--',
            '+', '-', '*', '/', '&', '|', '^', '%', '<<',
            '>>', '>>>', '+=', '-=', '*=', '/=', '&=', '|=',
            '^=', '%=', '<<=', '>>=', '>>>='
        ],
        symbols: /[=><!~?:&|+\-*\/\^%]+/,
        tokenizer: {
            root: [
                [/[a-zA-Z_$][\w$]*/, {
                    cases: {
                        '@keywords': 'keyword',
                        '@default': 'identifier'
                    }
                }],
                [/".*?"/, 'string'],
                [/'.*?'/, 'string'],
                [/\/\/.*$/, 'comment'],
                [/\/\*/, 'comment', '@comment'],
                [/[0-9]+/, 'number'],
                [/@symbols/, {
                    cases: {
                        '@operators': 'operator',
                        '@default': ''
                    }
                }]
            ],
            comment: [
                [/[^/*]+/, 'comment'],
                [/\*\//, 'comment', '@pop'],
                [/[/*]/, 'comment']
            ]
        }
    });
});

export const Editor = MonacoEditor;
export default Editor;