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

'use strict';

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

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');

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;
var CodeMirrorEditor = /** @class */ (function (_super) {
    tslib.__extends(CodeMirrorEditor, _super);
    function CodeMirrorEditor() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.dom = React__default["default"].createRef();
        _this.toDispose = [];
        _this.unmounted = false;
        return _this;
    }
    CodeMirrorEditor.prototype.componentDidMount = function () {
        var _a, _b, _c, _d, _e;
        return tslib.__awaiter(this, void 0, void 0, function () {
            var cm;
            var _this = this;
            return tslib.__generator(this, function (_f) {
                switch (_f.label) {
                    case 0: return [4 /*yield*/, Promise.resolve().then(function() {return new Promise(function(fullfill) {require(['codemirror', "tslib"], function(mod, tslib) {fullfill(tslib.__importStar(mod))})})})];
                    case 1:
                        cm = (_f.sent()).default;
                        // @ts-ignore
                        return [4 /*yield*/, Promise.resolve().then(function() {return new Promise(function(fullfill) {require(['codemirror/mode/javascript/javascript', "tslib"], function(mod, tslib) {fullfill(tslib.__importStar(mod))})})})];
                    case 2:
                        // @ts-ignore
                        _f.sent();
                        // @ts-ignore
                        return [4 /*yield*/, Promise.resolve().then(function() {return new Promise(function(fullfill) {require(['codemirror/mode/htmlmixed/htmlmixed', "tslib"], function(mod, tslib) {fullfill(tslib.__importStar(mod))})})})];
                    case 3:
                        // @ts-ignore
                        _f.sent();
                        return [4 /*yield*/, Promise.resolve().then(function() {return new Promise(function(fullfill) {require(['codemirror/addon/mode/simple', "tslib"], function(mod, tslib) {fullfill(tslib.__importStar(mod))})})})];
                    case 4:
                        _f.sent();
                        return [4 /*yield*/, Promise.resolve().then(function() {return new Promise(function(fullfill) {require(['codemirror/addon/mode/multiplex', "tslib"], function(mod, tslib) {fullfill(tslib.__importStar(mod))})})})];
                    case 5:
                        _f.sent();
                        return [4 /*yield*/, Promise.resolve().then(function() {return new Promise(function(fullfill) {require(['codemirror/addon/display/placeholder', "tslib"], function(mod, tslib) {fullfill(tslib.__importStar(mod))})})})];
                    case 6:
                        _f.sent();
                        if (this.unmounted) {
                            return [2 /*return*/];
                        }
                        this.dom.current.innerHTML = '';
                        this.editor =
                            (_c = (_b = (_a = this.props).editorFactory) === null || _b === void 0 ? void 0 : _b.call(_a, this.dom.current, cm, this.props)) !== null && _c !== void 0 ? _c : cm(this.dom.current, {
                                value: this.props.value || '',
                                readOnly: this.props.readOnly ? 'nocursor' : false,
                                autofocus: false
                            });
                        (_e = (_d = this.props).editorDidMount) === null || _e === void 0 ? void 0 : _e.call(_d, cm, this.editor);
                        this.editor.on('change', this.handleChange);
                        this.editor.on('blur', this.handleBlur);
                        this.editor.on('focus', this.handleFocus);
                        this.props.value && this.setValue(this.props.value);
                        this.toDispose.push(amisCore.resizeSensor(this.dom.current, function () { var _a; return (_a = _this.editor) === null || _a === void 0 ? void 0 : _a.refresh(); }));
                        // todo 以后优化这个，解决弹窗里面默认光标太小的问题
                        setTimeout(function () { var _a; return (_a = _this.editor) === null || _a === void 0 ? void 0 : _a.refresh(); }, 350);
                        this.toDispose.push(function () {
                            var _a, _b;
                            (_b = (_a = _this.props).editorWillUnMount) === null || _b === void 0 ? void 0 : _b.call(_a, cm, _this.editor);
                        });
                        return [2 /*return*/];
                }
            });
        });
    };
    CodeMirrorEditor.prototype.componentDidUpdate = function (prevProps) {
        var _this = this;
        var props = this.props;
        if (props.value !== prevProps.value) {
            this.editor && this.setValue(props.value);
        }
        amisCore.changedEffect(['readOnly'], prevProps, this.props, function (changes) {
            var _a;
            (_a = _this.editor) === null || _a === void 0 ? void 0 : _a.setOption('readOnly', changes.readOnly ? 'nocursor' : false);
        });
    };
    CodeMirrorEditor.prototype.componentWillUnmount = function () {
        var _a, _b, _c;
        this.unmounted = true;
        (_a = this.editor) === null || _a === void 0 ? void 0 : _a.off('change', this.handleChange);
        (_b = this.editor) === null || _b === void 0 ? void 0 : _b.off('blur', this.handleBlur);
        (_c = this.editor) === null || _c === void 0 ? void 0 : _c.off('focus', this.handleFocus);
        this.toDispose.forEach(function (fn) { return fn(); });
        this.toDispose = [];
    };
    CodeMirrorEditor.prototype.handleChange = function (editor) {
        var _a, _b;
        (_b = (_a = this.props).onChange) === null || _b === void 0 ? void 0 : _b.call(_a, editor.getValue());
    };
    CodeMirrorEditor.prototype.handleBlur = function (editor) {
        var _a, _b;
        (_b = (_a = this.props).onBlur) === null || _b === void 0 ? void 0 : _b.call(_a, editor);
    };
    CodeMirrorEditor.prototype.handleFocus = function (editor) {
        var _a, _b;
        (_b = (_a = this.props).onFocus) === null || _b === void 0 ? void 0 : _b.call(_a, editor);
    };
    CodeMirrorEditor.prototype.setValue = function (value) {
        var doc = this.editor.getDoc();
        if (value !== doc.getValue()) {
            var cursor = doc.getCursor();
            doc.setValue(value || '');
            doc.setCursor(cursor);
        }
    };
    CodeMirrorEditor.prototype.render = function () {
        var _a = this.props, className = _a.className, style = _a.style;
        return _J$X_("div", { className: className, style: style, ref: this.dom });
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], CodeMirrorEditor.prototype, "handleChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], CodeMirrorEditor.prototype, "handleBlur", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], CodeMirrorEditor.prototype, "handleFocus", null);
    return CodeMirrorEditor;
}(React__default["default"].Component));

exports.CodeMirrorEditor = CodeMirrorEditor;
exports["default"] = CodeMirrorEditor;
