"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
        s = arguments[i];
        for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
            t[p] = s[p];
    }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_monaco_editor_1 = require("react-monaco-editor");
var common_1 = require("../../common");
var CodeEditor = (function (_super) {
    __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.editorDidMount = function (editor, monacoModule) {
            var focusOnLoad = _this.props.focusOnLoad === undefined ? true : _this.props.focusOnLoad;
            _this.editor = editor;
            if (focusOnLoad) {
                _this.focus();
            }
        };
        _this.onChange = function (code, ev) {
            var onChange = _this.props.onChange;
            if (onChange) {
                onChange(__assign({ code: code }, ev));
            }
        };
        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.focus = function () {
        if (this.editor) {
            this.editor.focus();
        }
    };
    Object.defineProperty(CodeEditor.prototype, "isFocused", {
        get: function () {
            return this.editor && this.editor.isFocused();
        },
        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.render = function () {
        var _a = this.props, _b = _a.theme, theme = _b === void 0 ? 'DARK' : _b, _c = _a.language, language = _c === void 0 ? 'javascript' : _c, _d = _a.code, code = _d === void 0 ? '' : _d;
        var styles = {
            base: common_1.css({
                backgroundColor: theme === 'DARK' ? '#1E1E1E' : undefined,
            }),
        };
        var options = {
            selectOnLineNumbers: true,
        };
        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, editorDidMount: this.editorDidMount, onChange: this.onChange }));
        return (common_1.React.createElement("div", __assign({}, common_1.css(STYLES.BASE, styles.base, this.props.style)), elEditor));
    };
    return CodeEditor;
}(common_1.React.PureComponent));
exports.CodeEditor = CodeEditor;
var STYLES = {
    BASE: common_1.css({
        flex: 1,
        position: 'relative',
        boxSizing: 'border-box',
        overflow: 'hidden',
    }),
};
//# sourceMappingURL=CodeEditor.js.map