/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

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

var tslib = require('tslib');
var amisCore = require('amis-core');
var React = require('react');
var CodeMirror = require('../CodeMirror.js');
var plugin = require('./plugin.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
function CodeEditor(props, ref) {
    var cx = props.classnames, className = props.className, value = props.value, onChange = props.onChange, editorDidMount = props.editorDidMount, onFocus = props.onFocus, onBlur = props.onBlur, functions = props.functions, variables = props.variables, evalMode = props.evalMode, singleLine = props.singleLine, autoFocus = props.autoFocus, editorTheme = props.editorTheme, defaultTheme = props.theme, editorOptions = props.editorOptions, placeholder = props.placeholder, highlightMode = props.highlightMode;
    var pluginRef = React__default["default"].useRef();
    var editorFactory = React__default["default"].useCallback(function (dom, cm) {
        var theme = (editorTheme !== null && editorTheme !== void 0 ? editorTheme : ((defaultTheme || '').includes('dark') ? 'dark' : 'light')) === 'dark'
            ? 'base16-dark'
            : 'idea';
        var options = tslib.__assign({ autoFocus: autoFocus, indentUnit: 2, lineNumbers: true, lineWrapping: true, // 自动换行
            theme: theme, placeholder: placeholder }, editorOptions);
        if (singleLine) {
            options = tslib.__assign({ lineNumbers: false, indentWithTabs: false, indentUnit: 4, lineWrapping: false, scrollbarStyle: null, theme: theme, placeholder: placeholder }, editorOptions);
        }
        return plugin.editorFactory(dom, cm, props, options);
    }, []);
    var _a = tslib.__read(React__default["default"].useState(props.readOnly), 2), readOnly = _a[0], setReadOnly = _a[1];
    React__default["default"].useEffect(function () { return setReadOnly(props.readOnly); }, [props.readOnly]);
    React__default["default"].useEffect(function () { var _a, _b; return (_b = (_a = pluginRef.current) === null || _a === void 0 ? void 0 : _a.editor) === null || _b === void 0 ? void 0 : _b.setOption('placeholder', placeholder); }, [placeholder]);
    // singleLine 模式下，禁止输入换行符
    var onEditorBeforeChange = React__default["default"].useCallback(function (cm, event) {
        // Identify typing events that add a newline to the buffer.
        var hasTypedNewline = event.origin === '+input' &&
            typeof event.text === 'object' &&
            event.text.join('') === '';
        // Prevent newline characters from being added to the buffer.
        if (hasTypedNewline) {
            return event.cancel();
        }
        // Identify paste events.
        var hasPastedNewline = event.origin === 'paste' &&
            typeof event.text === 'object' &&
            event.text.length > 1;
        // Format pasted text to replace newlines with spaces.
        if (hasPastedNewline) {
            var newText = event.text.join(' ');
            return event.update(null, null, [newText]);
        }
        return null;
    }, []);
    var onEditorMount = React__default["default"].useCallback(function (cm, editor) {
        var plugin$1 = (pluginRef.current = new plugin.FormulaPlugin(editor, cm));
        plugin$1.setEvalMode(!!evalMode);
        plugin$1.setFunctions(functions || []);
        plugin$1.setVariables(variables || []);
        plugin$1.setHighlightMode(highlightMode || 'formula');
        editorDidMount === null || editorDidMount === void 0 ? void 0 : editorDidMount(cm, editor, plugin$1);
        plugin$1.autoMarkText();
        // 单行模式，不允许输入换行，同时原来的换行符也要去掉
        if (singleLine) {
            editor.on('beforeChange', onEditorBeforeChange);
            var value_1 = editor.getValue();
            if (value_1 && /[\n\r]/.test(value_1)) {
                // 初始数据有换行，不允许直接编辑
                // 只能弹窗弹出非单行模式编辑
                setReadOnly(true);
                editor.setValue(value_1.replace(/[\n\r]+/g, ''));
            }
        }
    }, [evalMode, functions, variables]);
    React__default["default"].useEffect(function () {
        return function () {
            var _a, _b;
            (_a = pluginRef.current) === null || _a === void 0 ? void 0 : _a.editor.off('beforeChange', onEditorBeforeChange);
            (_b = pluginRef.current) === null || _b === void 0 ? void 0 : _b.dispose();
        };
    }, []);
    React__default["default"].useImperativeHandle(ref, function () {
        return {
            insertContent: function (value, type) { var _a; return (_a = pluginRef.current) === null || _a === void 0 ? void 0 : _a.insertContent(value, type); },
            setValue: function (value) { var _a; return (_a = pluginRef.current) === null || _a === void 0 ? void 0 : _a.setValue(value); },
            getValue: function () { var _a; return (_a = pluginRef.current) === null || _a === void 0 ? void 0 : _a.getValue(); },
            setDisableAutoMark: function (value) { var _a; return (_a = pluginRef.current) === null || _a === void 0 ? void 0 : _a.setDisableAutoMark(value); }
        };
    });
    React__default["default"].useEffect(function () {
        var plugin = pluginRef.current;
        if (!plugin) {
            return;
        }
        plugin.setEvalMode(!!evalMode);
        plugin.setFunctions(functions || []);
        plugin.setVariables(variables || []);
        plugin.autoMarkText();
    }, [evalMode, functions, variables, value]);
    return (_J$X_(CodeMirror.CodeMirrorEditor, { className: cx('FormulaCodeEditor', className, singleLine ? 'FormulaCodeEditor--singleLine' : ''), value: value, onChange: onChange, editorFactory: editorFactory, editorDidMount: onEditorMount, onFocus: onFocus, onBlur: onBlur, readOnly: readOnly }));
}
var CodeEditor$1 = amisCore.themeable(React__default["default"].forwardRef(CodeEditor));

exports["default"] = CodeEditor$1;
