"use strict";
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
    if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
    return cooked;
};
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        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 extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var assign = require("lodash/assign");
var cloneDeep = require("lodash/cloneDeep");
var React = require("react");
var styled_components_1 = require("styled-components");
var xterm_1 = require("xterm");
var fit_1 = require("xterm/lib/addons/fit/fit");
var theme_1 = require("../../theme");
var Grid_1 = require("../Grid");
var XTermDefaultStyle_1 = require("./XTermDefaultStyle");
var TtyContainer = styled_components_1.default(Grid_1.Box)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\tposition: relative;\n\theight: 100%;\n\n\t", "\n\n\t.xterm-viewport::-webkit-scrollbar-track {\n\t\tbackground-color: transparent;\n\t\tborder-bottom-right-radius: 0;\n\t}\n\n\t.xterm-viewport::-webkit-scrollbar {\n\t\twidth: 10px;\n\t\tbackground-color: transparent;\n\t\tborder-radius: 0;\n\t}\n\n\t.xterm-viewport::-webkit-scrollbar-thumb {\n\t\tbackground-color: #e9e9e9;\n\t}\n"], ["\n\tposition: relative;\n\theight: 100%;\n\n\t", "\n\n\t.xterm-viewport::-webkit-scrollbar-track {\n\t\tbackground-color: transparent;\n\t\tborder-bottom-right-radius: 0;\n\t}\n\n\t.xterm-viewport::-webkit-scrollbar {\n\t\twidth: 10px;\n\t\tbackground-color: transparent;\n\t\tborder-radius: 0;\n\t}\n\n\t.xterm-viewport::-webkit-scrollbar-thumb {\n\t\tbackground-color: #e9e9e9;\n\t}\n"])), XTermDefaultStyle_1.default);
var TtyInner = styled_components_1.default.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\tposition: absolute;\n\ttop: 0;\n\tleft: 0;\n\tright: 0;\n\tbottom: 0;\n"], ["\n\tposition: absolute;\n\ttop: 0;\n\tleft: 0;\n\tright: 0;\n\tbottom: 0;\n"])));
var Terminal = (function (_super) {
    __extends(Terminal, _super);
    function Terminal(props) {
        var _this = _super.call(this, props) || this;
        _this.termConfig = assign({}, props.config, {
            cols: 80,
            cursorBlink: false,
            rows: 24,
            fontFamily: props.theme ? props.theme.monospace : theme_1.default.monospace,
            lineHeight: 1.4,
            theme: {
                background: '#343434',
                cursor: props.nonInteractive ? '#343434' : undefined,
            },
        });
        if (_this.props.ttyInstance) {
            _this.tty = _this.props.ttyInstance;
        }
        else {
            _this.tty = new xterm_1.Terminal(cloneDeep(_this.termConfig));
        }
        _this.resize = _this.resize.bind(_this);
        return _this;
    }
    Terminal.prototype.componentDidMount = function () {
        var _this = this;
        this.open();
        if (this.props.ttyInstance) {
            this.tty.setOption('theme', this.termConfig.theme);
        }
        setTimeout(function () {
            if (_this.props.nonInteractive && document.execCommand) {
                _this.tty.attachCustomKeyEventHandler(function (e) {
                    if (e.ctrlKey && e.keyCode === 67) {
                        document.execCommand('copy');
                        return false;
                    }
                    return true;
                });
            }
            window.addEventListener('resize', _this.resize);
            _this.resize();
        }, 100);
    };
    Terminal.prototype.componentWillUnmount = function () {
        window.removeEventListener('resize', this.resize);
        if (!this.props.persistent) {
            this.tty.destroy();
        }
    };
    Terminal.prototype.destroy = function () {
        window.removeEventListener('resize', this.resize);
        this.tty.destroy();
    };
    Terminal.prototype.open = function () {
        this.tty.open(this.mountElement);
        this.tty.focus();
    };
    Terminal.prototype.resize = function () {
        fit_1.fit(this.tty);
    };
    Terminal.prototype.clear = function () {
        this.tty.clear();
    };
    Terminal.prototype.writeln = function (line) {
        this.tty.writeln(line);
    };
    Terminal.prototype.write = function (text) {
        this.tty.write(text);
    };
    Terminal.prototype.render = function () {
        var _this = this;
        return (React.createElement(TtyContainer, { color: this.props.color },
            React.createElement(TtyInner, { ref: function (el) { return (_this.mountElement = el); } })));
    };
    return Terminal;
}(React.Component));
exports.default = Terminal;
var templateObject_1, templateObject_2;
