"use strict";
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 __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __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;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var react_1 = require("@storybook/react");
var storybook_chrome_screenshot_1 = require("storybook-chrome-screenshot");
var with_readme_1 = require("storybook-readme/with-readme");
var __1 = require("../");
var tty_output_1 = require("./assets/tty-output");
var repl_1 = require("./assets/repl");
var helpers_1 = require("./helpers");
var Terminal_md_1 = require("./README/Terminal.md");
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;
        if (helpers_1.isTakingScreenshot) {
            return;
        }
        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;
        _super.prototype.componentDidMount.call(this);
        if (!this.props.ttyInstance) {
            this.tty._repl = new repl_1.Repl(function (line) {
                if (_this.tty) {
                    _this.writeln(line);
                    _this.tty.prompt();
                }
            });
            this.tty.prompt = function () {
                _this.tty.write('\u001b[33m$ \u001b[0m');
            };
            this.tty.writeln('\u001b[32mThis is a basic interactive representation of a web browser javascript console\u001b[0m');
            this.tty.writeln('\u001b[32mType some keys and commands to play around\u001b[0m');
            this.tty.prompt();
            this.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.write('\r\n');
                    _this.tty._repl.process(_this.input);
                    _this.input = '';
                }
                else if (ev.keyCode === 8) {
                    if (_this.tty.buffer.x > 2) {
                        _this.tty.write('\b \b');
                        _this.input = _this.input.slice(0, -1);
                    }
                }
                else if (printable) {
                    _this.input += key;
                    _this.tty.write(key);
                }
            });
        }
    };
    InteractiveTerm.prototype.componentWillUnmount = function () {
        if (!this.props.persistent) {
            this.tty._repl.destroy();
        }
        _super.prototype.componentWillUnmount.call(this);
    };
    return InteractiveTerm;
}(__1.Terminal));
var PersistentTerm = (function (_super) {
    __extends(PersistentTerm, _super);
    function PersistentTerm(props) {
        var _this = _super.call(this, props) || this;
        var items = [
            {
                name: 'terminal 1',
                term: null
            },
            {
                name: 'terminal 2',
                term: null
            },
            {
                name: 'terminal 3',
                term: null
            }
        ];
        _this.state = {
            selectedTerm: items[0]
        };
        _this.items = items;
        return _this;
    }
    PersistentTerm.prototype.setTerm = function (name) {
        this.setState({ selectedTerm: this.items.find(function (x) { return x.name === name; }) });
    };
    PersistentTerm.prototype.setActiveTty = function (tty) {
        var _this = this;
        var selectedTerm = this.items.find(function (x) { return x.name === _this.state.selectedTerm.name; });
        selectedTerm.term = tty;
    };
    PersistentTerm.prototype.render = function () {
        var _this = this;
        var activeTerm = this.items.find(function (x) { return x.name === _this.state.selectedTerm.name; });
        return (React.createElement(__1.Box, null,
            React.createElement("select", { value: this.state.selectedTerm.name, onChange: function (e) { return _this.setTerm(e.target.value); } }, this.items.map(function (x) { return (React.createElement("option", { key: x.name }, x.name)); })),
            React.createElement(__1.Box, { style: { height: 500 } }, this.items.map(function (t) {
                if (t.name !== activeTerm.name) {
                    return null;
                }
                return (React.createElement(InteractiveTerm, { key: t.name, persistent: true, ttyInstance: activeTerm.term && activeTerm.term.tty, ref: function (term) { return _this.setActiveTty(term); } }));
            }))));
    };
    return PersistentTerm;
}(React.Component));
react_1.storiesOf('Core/Terminal', module)
    .addDecorator(with_readme_1.default(Terminal_md_1.default))
    .addDecorator(storybook_chrome_screenshot_1.withScreenshot())
    .add('Standard', function () {
    return (React.createElement(__1.Provider, null,
        React.createElement(__1.Box, { p: 30, style: { height: 500 } },
            React.createElement(InteractiveTerm, null))));
})
    .add('Non interactive', function () {
    return (React.createElement(__1.Provider, null,
        React.createElement(__1.Box, { p: 30, style: { height: 500 } },
            React.createElement(Logger, { termProps: { nonInteractive: true } }))));
})
    .add('Persistent', function () {
    return (React.createElement(__1.Provider, null,
        React.createElement(__1.Box, { p: 30 },
            React.createElement(PersistentTerm, null))));
});
