"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeRenderer = void 0;
var tslib_1 = require("tslib");
/**
 * @file 代码高亮
 */
var react_1 = tslib_1.__importDefault(require("react"));
var factory_1 = require("../factory");
var helper_1 = require("../utils/helper");
var tpl_builtin_1 = require("../utils/tpl-builtin");
var Code = /** @class */ (function (_super) {
    tslib_1.__extends(Code, _super);
    function Code(props) {
        var _this = _super.call(this, props) || this;
        _this.toDispose = [];
        _this.codeRef = react_1.default.createRef();
        return _this;
    }
    Code.prototype.componentDidMount = function () {
        var _this = this;
        Promise.resolve().then(function () { return new Promise(function(resolve){require(['monaco-editor'], function(ret) {resolve(tslib_1.__importStar(ret));})}); }).then(function (monaco) { return _this.handleMonaco(monaco); });
    };
    Code.prototype.componentDidUpdate = function (preProps) {
        var _this = this;
        var props = this.props;
        helper_1.detectPropValueChanged(props, preProps, function () {
            var dom = _this.codeRef.current;
            _this.editor.colorizeElement(dom, {
                tabSize: _this.props.tabSize,
                theme: _this.props.editorTheme
            });
        });
    };
    Code.prototype.handleMonaco = function (monaco) {
        this.editor = monaco.editor;
        if (this.codeRef.current) {
            var dom = this.codeRef.current;
            monaco.editor.colorizeElement(dom, {
                tabSize: this.props.tabSize,
                theme: this.props.editorTheme
            });
        }
    };
    Code.prototype.render = function () {
        var _a = this.props, className = _a.className, cx = _a.classnames, data = _a.data;
        var sourceCode = helper_1.getPropValue(this.props);
        var language = this.props.language;
        if (tpl_builtin_1.isPureVariable(language)) {
            language = tpl_builtin_1.resolveVariableAndFilter(language, data);
        }
        return (react_1.default.createElement("code", { ref: this.codeRef, className: cx("Code", className), "data-lang": language }, sourceCode));
    };
    Code.defaultProps = {
        language: 'plaintext',
        editorTheme: 'vs',
        tabSize: 4
    };
    return Code;
}(react_1.default.Component));
exports.default = Code;
var CodeRenderer = /** @class */ (function (_super) {
    tslib_1.__extends(CodeRenderer, _super);
    function CodeRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    CodeRenderer = tslib_1.__decorate([
        factory_1.Renderer({
            type: 'code'
        })
    ], CodeRenderer);
    return CodeRenderer;
}(Code));
exports.CodeRenderer = CodeRenderer;
//# sourceMappingURL=./renderers/Code.js.map
