import { each, isValidRegex } from './';
import { isArray, isFile, isUndefined } from './type-utils';
import { logger } from './logger';
import { document } from './globals';
var localDomains = ['localhost', '127.0.0.1'];
/**
 * IE11 doesn't support `new URL`
 * so we can create an anchor element and use that to parse the URL
 * there's a lot of overlap between HTMLHyperlinkElementUtils and URL
 * meaning useful properties like `pathname` are available on both
 */
export var convertToURL = function (url) {
    var location = document === null || document === void 0 ? void 0 : document.createElement('a');
    if (isUndefined(location)) {
        return null;
    }
    location.href = url;
    return location;
};
export var isUrlMatchingRegex = function (url, pattern) {
    if (!isValidRegex(pattern))
        return false;
    return new RegExp(pattern).test(url);
};
export var formDataToQuery = function (formdata, arg_separator) {
    if (arg_separator === void 0) { arg_separator = '&'; }
    var use_val;
    var use_key;
    var tph_arr = [];
    each(formdata, function (val, key) {
        // the key might be literally the string undefined for e.g. if {undefined: 'something'}
        if (isUndefined(val) || isUndefined(key) || key === 'undefined') {
            return;
        }
        use_val = encodeURIComponent(isFile(val) ? val.name : val.toString());
        use_key = encodeURIComponent(key);
        tph_arr[tph_arr.length] = use_key + '=' + use_val;
    });
    return tph_arr.join(arg_separator);
};
export var getQueryParam = function (url, param) {
    var withoutHash = url.split('#')[0] || '';
    var queryParams = withoutHash.split('?')[1] || '';
    var queryParts = queryParams.split('&');
    var keyValuePair;
    for (var i = 0; i < queryParts.length; i++) {
        var parts = queryParts[i].split('=');
        if (parts[0] === param) {
            keyValuePair = parts;
            break;
        }
    }
    if (!isArray(keyValuePair) || keyValuePair.length < 2) {
        return '';
    }
    else {
        var result = keyValuePair[1];
        try {
            result = decodeURIComponent(result);
        }
        catch (_a) {
            logger.error('Skipping decoding for malformed query param: ' + result);
        }
        return result.replace(/\+/g, ' ');
    }
};
// replace any query params in the url with the provided mask value. Tries to keep the URL as instant as possible,
// including preserving malformed text in most cases
export var maskQueryParams = function (url, maskedParams, mask) {
    if (!url || !maskedParams || !maskedParams.length) {
        return url;
    }
    var splitHash = url.split('#');
    var withoutHash = splitHash[0] || '';
    var hash = splitHash[1];
    var splitQuery = withoutHash.split('?');
    var queryString = splitQuery[1];
    var urlWithoutQueryAndHash = splitQuery[0];
    var queryParts = (queryString || '').split('&');
    // use an array of strings rather than an object to preserve ordering and duplicates
    var paramStrings = [];
    for (var i = 0; i < queryParts.length; i++) {
        var keyValuePair = queryParts[i].split('=');
        if (!isArray(keyValuePair)) {
            continue;
        }
        else if (maskedParams.includes(keyValuePair[0])) {
            paramStrings.push(keyValuePair[0] + '=' + mask);
        }
        else {
            paramStrings.push(queryParts[i]);
        }
    }
    var result = urlWithoutQueryAndHash;
    if (queryString != null) {
        result += '?' + paramStrings.join('&');
    }
    if (hash != null) {
        result += '#' + hash;
    }
    return result;
};
export var _getHashParam = function (hash, param) {
    var matches = hash.match(new RegExp(param + '=([^&]*)'));
    return matches ? matches[1] : null;
};
export var isLocalhost = function () {
    return localDomains.includes(location.hostname);
};
//# sourceMappingURL=request-utils.js.map