"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Repl = (function () {
    function Repl(gotLineCallback, element) {
        var _this = this;
        this.state = {
            history: [],
            input: ''
        };
        this.gotLineCallback = gotLineCallback;
        this.replRoot = {};
        this.iframe = document.createElement('iframe');
        element.appendChild(this.iframe);
        this.iframe.contentWindow.REPL_LOG_CAPTURE = function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            args.forEach(function (arg) { return _this.state.history.push(arg); });
        };
    }
    Repl.prototype.process = function (input) {
        var history = this.state.history;
        history.push(input);
        var command = input.replace('console.log', 'REPL_LOG_CAPTURE', 'g');
        var result;
        var _iframe = this.iframe;
        try {
            result = (function () {
                return _iframe.contentWindow.eval(command);
            }.call(this.replRoot));
        }
        catch (err) {
            result = err;
        }
        history.push(result);
        this.state.history = history;
        if (this.gotLineCallback) {
            this.gotLineCallback(result);
        }
    };
    return Repl;
}());
exports.Repl = Repl;
