"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 = require("react");
var ReactDOM = require("react-dom");
var react_1 = require("@storybook/react");
var _1 = require("../");
var tty_output_1 = require("./assets/tty-output");
var repl_1 = require("./assets/repl");
var outputArray = tty_output_1.output1.split('\n');
var Logger = (function (_super) {
    __extends(Logger, _super);
    function Logger(props) {
        var _this = _super.call(this, props) || this;
        _this.lines = outputArray.slice();
        return _this;
    }
    Logger.prototype.componentDidMount = function () {
        var _this = this;
        this.interval = setInterval(function () {
            if (!_this.term) {
                return;
            }
            if (_this.lines.length) {
                _this.term.writeln(_this.lines.shift());
            }
            else {
                window.clearInterval(_this.interval);
                _this.interval = null;
            }
        }, 300);
    };
    Logger.prototype.componentWillUnmount = function () {
        if (this.interval) {
            window.clearInterval(this.interval);
        }
    };
    Logger.prototype.render = function () {
        var _this = this;
        return (React.createElement(_1.Terminal, __assign({ ref: function (term) { return (_this.term = term); } }, this.props.termProps)));
    };
    return Logger;
}(React.Component));
var InteractiveTerm = (function (_super) {
    __extends(InteractiveTerm, _super);
    function InteractiveTerm(props) {
        var _this = _super.call(this, props) || this;
        _this.input = '';
        return _this;
    }
    InteractiveTerm.prototype.componentDidMount = function () {
        var _this = this;
        this._repl = new repl_1.Repl(function (line) {
            if (_this.term) {
                _this.term.writeln(line);
                _this.term.tty.prompt();
            }
        }, ReactDOM.findDOMNode(this));
        this.term.tty.prompt = function () {
            _this.term.tty.write('\u001b[33m$ \u001b[0m');
        };
        this.term.tty.writeln('\u001b[32mThis is a basic interactive representation of a web browser javascript console\u001b[0m');
        this.term.tty.writeln('\u001b[32mType some keys and commands to play around\u001b[0m');
        this.term.tty.prompt();
        this.term.tty.on('key', function (key, ev) {
            var printable = !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey;
            if (ev.code === 'ArrowUp' ||
                ev.code === 'ArrowDown' ||
                ev.code === 'ArrowLeft' ||
                ev.code === 'ArrowRight') {
                return;
            }
            if (ev.keyCode === 13) {
                _this.term.write('\r\n');
                _this._repl.process(_this.input);
                _this.input = '';
            }
            else if (ev.keyCode === 8) {
                if (_this.term.tty.buffer.x > 2) {
                    _this.term.tty.write('\b \b');
                    _this.input = _this.input.slice(0, -1);
                }
            }
            else if (printable) {
                _this.input += key;
                _this.term.tty.write(key);
            }
        });
    };
    InteractiveTerm.prototype.render = function () {
        var _this = this;
        return React.createElement(_1.Terminal, { ref: function (term) { return (_this.term = term); } });
    };
    return InteractiveTerm;
}(React.Component));
react_1.storiesOf('Terminal', module)
    .addWithInfo('Standard', function () {
    return (React.createElement(_1.Box, { p: 30, style: { height: 500 } },
        React.createElement(InteractiveTerm, null)));
})
    .addWithInfo('Non interactive', function () {
    return (React.createElement(_1.Box, { p: 30, style: { height: 500 } },
        React.createElement(Logger, { termProps: { nonInteractive: true } })));
});
