/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __extends, __rest, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import MonacoEditor from './Editor.js';
import { autobind } from 'amis-core';

var DiffEditor = /** @class */ (function (_super) {
    __extends(DiffEditor, _super);
    function DiffEditor() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.toDispose = [];
        _this.domRef = React__default.createRef();
        _this.prevHeight = 0;
        return _this;
    }
    DiffEditor.prototype.componentDidUpdate = function (prevProps) {
        var _a, _b;
        var _c = this.props, value = _c.value, originValue = _c.originValue;
        if (this.originalEditor && originValue !== prevProps.originValue) {
            this.originalEditor.getModel().setValue(originValue || '');
        }
        if (this.modifiedEditor && value !== prevProps.value) {
            var editorValue = this.modifiedEditor.getModel().getValue();
            editorValue !== value &&
                this.modifiedEditor.getModel().setValue(value || '');
        }
        if (this.props.disabled !== prevProps.disabled && this.modifiedEditor) {
            (_b = (_a = this.modifiedEditor).updateOptions) === null || _b === void 0 ? void 0 : _b.call(_a, {
                readOnly: this.props.disabled
            });
        }
    };
    DiffEditor.prototype.componentWillUnmount = function () {
        this.toDispose.forEach(function (fn) { return fn(); });
    };
    DiffEditor.prototype.updateContainerSize = function (editor, monaco) {
        var _a, _b;
        var dom = (_a = this.domRef.current) === null || _a === void 0 ? void 0 : _a.getDom();
        if (!dom) {
            return;
        }
        var lineHeight = editor.getOption(monaco.editor.EditorOption.lineHeight);
        var lineCount = ((_b = editor.getModel()) === null || _b === void 0 ? void 0 : _b.getLineCount()) || 1;
        var height = editor.getTopForLineNumber(lineCount + 1) + lineHeight;
        if (this.prevHeight !== height && dom.parentElement) {
            this.prevHeight = height;
            dom.parentElement.style.height = "".concat(height, "px");
            editor.layout();
        }
    };
    DiffEditor.prototype.editorFactory = function (containerElement, monaco, options) {
        if (this.props.editorFactory) {
            return this.props.editorFactory(containerElement, monaco, options);
        }
        return monaco.editor.createDiffEditor(containerElement, options);
    };
    DiffEditor.prototype.handleModifiedEditorChange = function (e) {
        var onChange = this.props.onChange;
        var value = this.modifiedEditor.getModel().getValue();
        value !== this.props.value && (onChange === null || onChange === void 0 ? void 0 : onChange(value, e));
    };
    DiffEditor.prototype.editorDidMount = function (editor, monaco) {
        var _this = this;
        var _a = this.props, value = _a.value, originValue = _a.originValue, language = _a.language, onFocus = _a.onFocus, onBlur = _a.onBlur, editorDidMount = _a.editorDidMount;
        editorDidMount === null || editorDidMount === void 0 ? void 0 : editorDidMount(editor, monaco);
        this.monaco = monaco;
        this.editor = editor;
        this.modifiedEditor = editor.getModifiedEditor();
        this.originalEditor = editor.getOriginalEditor();
        onFocus &&
            this.toDispose.push(this.modifiedEditor.onDidFocusEditorWidget(onFocus).dispose);
        onBlur &&
            this.toDispose.push(this.modifiedEditor.onDidBlurEditorWidget(onBlur).dispose);
        this.toDispose.push(this.modifiedEditor.onDidChangeModelDecorations(function () {
            _this.updateContainerSize(_this.modifiedEditor, monaco); // typing
            requestAnimationFrame(_this.updateContainerSize.bind(_this, _this.modifiedEditor, monaco)); // folding
        }).dispose);
        this.toDispose.push(this.modifiedEditor.onDidChangeModelContent(this.handleModifiedEditorChange).dispose);
        this.editor.setModel({
            original: this.monaco.editor.createModel(originValue || '', language),
            modified: this.monaco.editor.createModel(value, language)
        });
    };
    DiffEditor.prototype.render = function () {
        var _a = this.props; _a.value; _a.originValue; _a.options; var rest = __rest(_a, ["value", "originValue", "options"]);
        return (React__default.createElement(MonacoEditor, __assign({}, rest, { ref: this.domRef, editorDidMount: this.editorDidMount, editorFactory: this.editorFactory, isDiffEditor: true })));
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object, Object]),
        __metadata("design:returntype", void 0)
    ], DiffEditor.prototype, "updateContainerSize", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object, Object, Object]),
        __metadata("design:returntype", void 0)
    ], DiffEditor.prototype, "editorFactory", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], DiffEditor.prototype, "handleModifiedEditorChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object, Object]),
        __metadata("design:returntype", void 0)
    ], DiffEditor.prototype, "editorDidMount", null);
    return DiffEditor;
}(React__default.Component));

export { DiffEditor as default };
