"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 __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Executor_1 = require("./Executor");
var util_1 = require("../common/util");
var lang_1 = require("@dojo/core/lang");
var Formatter_1 = require("../browser/Formatter");
var Task_1 = require("@dojo/core/async/Task");
var GenericBrowser = (function (_super) {
    __extends(GenericBrowser, _super);
    function GenericBrowser(config) {
        var _this = _super.call(this, config) || this;
        _this._formatter = new Formatter_1.default(config);
        return _this;
    }
    Object.defineProperty(GenericBrowser.prototype, "environment", {
        get: function () {
            return 'browser';
        },
        enumerable: true,
        configurable: true
    });
    GenericBrowser.prototype.loadScript = function (script) {
        var _this = this;
        if (script == null) {
            return Task_1.default.resolve();
        }
        if (typeof script === 'string') {
            script = [script];
        }
        return script.reduce(function (previous, script) {
            if (script[0] !== '/') {
                script = "" + _this.config.basePath + script;
            }
            return previous.then(function () { return injectScript(script); });
        }, Task_1.default.resolve());
    };
    GenericBrowser.prototype._beforeRun = function () {
        var _this = this;
        return _super.prototype._beforeRun.call(this).then(function () {
            var config = _this.config;
            for (var _i = 0, _a = config.suites.concat(config.browserSuites); _i < _a.length; _i++) {
                var suite = _a[_i];
                if (/[*?]/.test(suite)) {
                    throw new Error("Globs may not be used for browser suites: \"" + suite + "\"");
                }
            }
        });
    };
    GenericBrowser.prototype._loadSuites = function () {
        var config = this.config;
        return _super.prototype._loadSuites.call(this, lang_1.deepMixin({}, config, { suites: config.suites.concat(config.browserSuites) }));
    };
    GenericBrowser.prototype._processOption = function (name, value) {
        switch (name) {
            case 'basePath':
                this.config[name] = util_1.parseValue(name, value, 'string');
                break;
            case 'browserSuites':
                this.config[name] = util_1.parseValue(name, value, 'string[]');
                break;
            default:
                _super.prototype._processOption.call(this, name, value);
                break;
        }
    };
    GenericBrowser.prototype._resolveConfig = function () {
        var _this = this;
        return _super.prototype._resolveConfig.call(this).then(function () {
            var config = _this.config;
            if (!config.basePath) {
                config.basePath = '/';
            }
            if (!config.internPath) {
                config.internPath = config.basePath + 'node_modules/intern/';
            }
            if (!config.browserSuites) {
                config.browserSuites = [];
            }
            ['basePath', 'internPath'].forEach(function (key) {
                config[key] = util_1.normalizePath(config[key]);
            });
        });
    };
    return GenericBrowser;
}(Executor_1.GenericExecutor));
exports.GenericBrowser = GenericBrowser;
var Browser = (function (_super) {
    __extends(Browser, _super);
    function Browser() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Browser.initialize = function (config) {
        return Executor_1.initialize(Browser, config);
    };
    return Browser;
}(GenericBrowser));
exports.default = Browser;
function injectScript(path) {
    return new Task_1.default(function (resolve, reject) {
        var scriptTag = document.createElement('script');
        scriptTag.addEventListener('load', function () {
            resolve();
        });
        scriptTag.addEventListener('error', function (event) {
            console.error("Error loading " + path + ":", event);
            reject(new Error("Unable to load " + path));
        });
        scriptTag.src = path;
        document.body.appendChild(scriptTag);
    });
}
//# sourceMappingURL=Browser.js.map