"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var react_monaco_editor_1 = require("react-monaco-editor");
var common_1 = require("../../common");
var config_language_1 = require("./config.language");
var CodeEditor = (function (_super) {
    tslib_1.__extends(CodeEditor, _super);
    function CodeEditor() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = { isMounted: false };
        _this.unmounted$ = new common_1.Subject();
        _this.handleBeforeMount = function (monacoModule) {
            var _a = _this.options, javascriptDefaults = _a.javascriptDefaults, typescriptDefaults = _a.typescriptDefaults;
            var typescript = monacoModule.languages.typescript;
            if (javascriptDefaults) {
                config_language_1.configureLanguage(javascriptDefaults, typescript.javascriptDefaults);
            }
            if (typescriptDefaults) {
                config_language_1.configureLanguage(typescriptDefaults, typescript.typescriptDefaults);
            }
        };
        _this.handleAfterMount = function (editor, monacoModule) {
            _this.editor = editor;
            var focusOnLoad = _this.props.focusOnLoad === undefined ? true : _this.props.focusOnLoad;
            if (focusOnLoad) {
                _this.focus();
            }
            var _a = _this.options.tabSize, tabSize = _a === void 0 ? 2 : _a;
            var model = _this.editor.getModel();
            model.updateOptions({ tabSize: tabSize });
        };
        _this.handleChange = function (code, e) {
            var onChange = _this.props.onChange;
            if (onChange) {
                onChange(tslib_1.__assign({ code: code }, e));
            }
        };
        return _this;
    }
    CodeEditor.prototype.componentDidMount = function () {
        var _this = this;
        this.setState({ isMounted: true });
        this.el = common_1.ReactDOM.findDOMNode(this);
        var resize$ = common_1.events.resize$.takeUntil(this.unmounted$);
        resize$.subscribe(function (e) { return _this.updateSize(); });
        this.updateSize();
    };
    CodeEditor.prototype.componentWillUnmount = function () {
        this.unmounted$.next();
    };
    CodeEditor.prototype.render = function () {
        var _a = this.props.code, code = _a === void 0 ? '' : _a;
        var _b = this.options, selectOnLineNumbers = _b.selectOnLineNumbers, fontSize = _b.fontSize, fontFamily = _b.fontFamily, theme = _b.theme, language = _b.language;
        var styles = {
            base: common_1.css({
                backgroundColor: theme === 'DARK' ? '#1E1E1E' : undefined,
            }),
        };
        var options = { selectOnLineNumbers: selectOnLineNumbers, fontSize: fontSize, fontFamily: fontFamily };
        var elEditor = this.state.isMounted && (common_1.React.createElement(react_monaco_editor_1.default, { width: this.state.width, height: this.state.height, language: language, theme: common_1.constants.THEMES[theme], value: code, options: options, editorWillMount: this.handleBeforeMount, editorDidMount: this.handleAfterMount, onChange: this.handleChange }));
        return (common_1.React.createElement("div", tslib_1.__assign({}, common_1.css(STYLES.BASE, styles.base, this.props.style)), elEditor));
    };
    Object.defineProperty(CodeEditor.prototype, "isFocused", {
        get: function () {
            return this.editor && this.editor.isFocused();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(CodeEditor.prototype, "options", {
        get: function () {
            var options = this.props.options || {};
            var _a = options.selectOnLineNumbers, selectOnLineNumbers = _a === void 0 ? true : _a, _b = options.fontSize, fontSize = _b === void 0 ? 14 : _b, fontFamily = options.fontFamily, _c = options.tabSize, tabSize = _c === void 0 ? 2 : _c, _d = options.theme, theme = _d === void 0 ? 'DARK' : _d, _e = options.language, language = _e === void 0 ? 'typescript' : _e, javascriptDefaults = options.javascriptDefaults, typescriptDefaults = options.typescriptDefaults;
            return {
                selectOnLineNumbers: selectOnLineNumbers,
                fontSize: fontSize,
                fontFamily: fontFamily,
                tabSize: tabSize,
                theme: theme,
                language: language,
                javascriptDefaults: javascriptDefaults,
                typescriptDefaults: typescriptDefaults,
            };
        },
        enumerable: true,
        configurable: true
    });
    CodeEditor.prototype.updateSize = function () {
        var width = this.el.offsetWidth;
        var height = this.el.offsetHeight;
        this.setState({ width: width, height: height });
    };
    CodeEditor.prototype.focus = function () {
        if (this.editor) {
            this.editor.focus();
        }
    };
    return CodeEditor;
}(common_1.React.PureComponent));
exports.CodeEditor = CodeEditor;
var STYLES = {
    BASE: common_1.css({
        flex: 1,
        position: 'relative',
        boxSizing: 'border-box',
    }),
};
//# sourceMappingURL=CodeEditor.js.map