(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", "@dojo/core/request/providers/xhr", "../common/util"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var xhr_1 = require("@dojo/core/request/providers/xhr");
    var util_1 = require("../common/util");
    /**
     * Resolve the user-supplied config data, which may include query args and a config file.
     */
    function getConfig(configFile) {
        var args = util_1.parseArgs(parseQuery());
        if (configFile) {
            args.config = configFile;
        }
        if (args.config) {
            // If a config parameter was provided, load it, mix in any other query params, then initialize the executor with
            // that
            var _a = util_1.splitConfigPath(args.config), configFile_1 = _a.configFile, childConfig = _a.childConfig;
            var path = resolvePath(configFile_1 || 'intern.json', args.basePath);
            return util_1.loadConfig(path, loadText, args, childConfig);
        }
        else {
            // If no config parameter was provided, try 'intern.json'. If that file doesn't exist, just return the args
            var path = resolvePath('intern.json', args.basePath);
            return util_1.loadConfig(path, loadText, args).catch(function (error) {
                if (error.message.indexOf('Request failed') === 0) {
                    return args;
                }
                throw error;
            });
        }
    }
    exports.getConfig = getConfig;
    /**
     * Normalize a path (e.g., resolve '..')
     */
    function normalizePath(path) {
        var parts = path.replace(/\\/g, '/').split('/');
        var result = [];
        for (var i = 0; i < parts.length; ++i) {
            var part = parts[i];
            if (!part || part === '.') {
                if (i === 0 || i === parts.length - 1) {
                    result.push('');
                }
                continue;
            }
            if (part === '..') {
                if (result.length && result[result.length - 1] !== '..') {
                    result.pop();
                }
                else {
                    result.push(part);
                }
            }
            else {
                result.push(part);
            }
        }
        return result.join('/');
    }
    exports.normalizePath = normalizePath;
    /**
     * Parse a query string and return a set of decoded name=value pairs
     */
    function parseQuery(query) {
        query = query || location.search.slice(1);
        return query.split('&').filter(function (arg) {
            return arg !== '' && arg[0] !== '=';
        }).map(function (arg) {
            var parts = arg.split('=');
            var name = decodeURIComponent(parts[0]);
            if (parts[1]) {
                return name + "=" + decodeURIComponent(parts[1]);
            }
            else if (parts.length > 1) {
                return name + "=";
            }
            return name;
        });
    }
    exports.parseQuery = parseQuery;
    function parseUrl(url) {
        if (url) {
            var match = /^(([^:\/?#]+):)?(\/\/(([^:\/?#]*)(:(\d+))?))?([^?#]*)(\?([^#]*))?(#(.*))?/.exec(url);
            if (match) {
                return {
                    protocol: match[2],
                    hostname: match[5],
                    port: match[7],
                    path: match[8],
                    query: match[10],
                    hash: match[12]
                };
            }
        }
    }
    exports.parseUrl = parseUrl;
    /**
     * Load a text resource
     */
    function loadText(path) {
        return xhr_1.default(path).then(function (response) {
            if (!response.ok) {
                throw new Error('Request failed: ' + response.status);
            }
            return response.text();
        });
    }
    /**
     * Resolve a path against a base path
     */
    function resolvePath(path, basePath) {
        if (path[0] !== '/') {
            basePath = basePath == null ? '/' : basePath;
            path = "" + basePath + path;
        }
        return path;
    }
});
//# sourceMappingURL=util.js.map