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 __());
    };
})();
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "./Executor", "../common/util", "@dojo/core/async/Task", "@dojo/shim/global", "../reporters/Html", "../reporters/Dom", "../reporters/Console"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Executor_1 = require("./Executor");
    var util_1 = require("../common/util");
    var Task_1 = require("@dojo/core/async/Task");
    var global_1 = require("@dojo/shim/global");
    // Reporters
    var Html_1 = require("../reporters/Html");
    var Dom_1 = require("../reporters/Dom");
    var Console_1 = require("../reporters/Console");
    var console = global_1.default.console;
    /**
     * A Browser executor is used to run unit tests in a browser.
     */
    var Browser = (function (_super) {
        __extends(Browser, _super);
        function Browser(options) {
            var _this = _super.call(this, {
                basePath: '/',
                internPath: 'node_modules/intern/'
            }) || this;
            // Report uncaught errors
            global_1.default.addEventListener('unhandledRejection', function (event) {
                console.warn('Unhandled rejection:', event);
                _this.emit('error', event.reason);
            });
            global_1.default.addEventListener('error', function (event) {
                console.warn('Unhandled error:', event);
                var error = new Error(event.message);
                error.stack = event.filename + ":" + event.lineno + ":" + event.colno;
                _this.emit('error', error);
            });
            _this.registerReporter('html', Html_1.default);
            _this.registerReporter('dom', Dom_1.default);
            _this.registerReporter('console', Console_1.default);
            if (options) {
                _this.configure(options);
            }
            return _this;
        }
        Object.defineProperty(Browser.prototype, "environment", {
            get: function () {
                return 'browser';
            },
            enumerable: true,
            configurable: true
        });
        /**
         * Load a script or scripts via script injection.
         *
         * @param script a path to a script
         */
        Browser.prototype.loadScript = function (script) {
            var _this = this;
            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());
        };
        Browser.prototype._resolveConfig = function () {
            var _this = this;
            return _super.prototype._resolveConfig.call(this).then(function () {
                var config = _this.config;
                if (config.internPath[0] !== '/') {
                    config.internPath = "" + config.basePath + config.internPath;
                }
                // Filter out globs from suites and browser suites
                [config.suites, config.browser.suites].forEach(function (suites) {
                    suites.forEach(function (suite) {
                        if (/[*?]/.test(suite)) {
                            throw new Error("Globs may not be used for browser suites: \"" + suite + "\"");
                        }
                    });
                });
                ['basePath', 'internPath'].forEach(function (key) {
                    config[key] = util_1.normalizePathEnding(config[key]);
                });
            });
        };
        return Browser;
    }(Executor_1.default));
    exports.default = Browser;
    function injectScript(path) {
        return new Task_1.default(function (resolve, reject) {
            var doc = global_1.default.document;
            var scriptTag = doc.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;
            doc.body.appendChild(scriptTag);
        });
    }
});
//# sourceMappingURL=Browser.js.map