"use strict";
/**
 * Ported from https://github.com/optimizely/client-js/blob/7f84e6e779288d8870cac9e17309bb4a324ec5c2/src/core/plugins/helpers/match_url.js
 * which is in turn ported from [optly.match.matchUrl@06b84fe4c24d26712823dff967db3632093d64fd](https://github.com/optimizely/optimizely/blob/06b84fe4c24d26712823dff967db3632093d64fd/src/www/js/shared/match.js)
 * Minor modifications to avoid using client-fn
 *
 * Match URL using various match types
 * @module
 */
/**
 * Match URL to cond
 *
 * @param {string} url
 * @param {Object} cond
 * @param {string} cond.match
 * @param {string} cond.value
 * @returns {boolean}
 */
module.exports = function (url, cond) {
    url = stripVarFromQuery(url);
    var condValue = cond.value;
    switch (cond.match) {
        case 'exact':
            url = stripUrl(url);
            return url === stripUrl(condValue);
        case 'regex':
            try {
                return Boolean(url.match(condValue));
            }
            catch (error) { } // eslint-disable-line
            return false;
        case 'simple':
            url = simplifyUrl(url);
            condValue = simplifyUrl(condValue);
            return url === condValue;
        case 'substring':
            url = stripUrl(url, true);
            condValue = stripUrl(condValue, true);
            return url.indexOf(condValue) !== -1;
        default:
            return false;
    }
};
/**
 * Regexes to match against URL if excludeOptionalPrefixesToStrip not set
 * @private
 */
var OPTIONAL_PREFIXES_TO_STRIP = ['www.'];
var QUERY_PREFIX = 'optimizely_';
/**
 * Regexes to match against URL (always)
 * @private
 */
var REQUIRED_PREFIXES_TO_STRIP = [
    'https?://.*?.?optimizelyedit.(com|test)/',
    'https?://.*.?optimizelypreview.(com|test)/',
    'https?://(edit|preview)(-hrd|-devel)?.optimizely.(com|test)/',
    'https?://.*?.?optimizelyedit(-hrd)?.appspot.com/',
    'https?://',
];
/**
 * Remove query parameters from URL
 * @param {String} url
 * @returns {String}
 */
function removeParameters(url) {
    // TODO (andrewk) rewrite using utils/location
    var index = url.indexOf('?');
    if (index !== -1) {
        url = url.substring(0, index);
    }
    index = url.indexOf('#');
    if (index !== -1) {
        url = url.substring(0, index);
    }
    return url;
}
/**
 * Remove parameters and strip URL
 * @private
 * @param {String} url
 * @returns {String}
 */
function simplifyUrl(url) {
    return stripUrl(removeParameters(url));
}
/**
 * Remove protocol, edit/preview proxy URL and extraneous trailing
 * characters. Also remove 'www.' unless specified not to.
 * @private
 * @param {String} url
 * @param {Boolean=} excludeOptionalPrefixesToStrip
 * @return {String}
 */
function stripUrl(url, excludeOptionalPrefixesToStrip) {
    // TODO (andrewk) rewrite using utils/location
    url = url.replace('/?', '?');
    url = url.toLowerCase().replace(/[/&?]+$/, '');
    // Create a copy of the required prefixes
    var prefixesToStrip = REQUIRED_PREFIXES_TO_STRIP.slice(0);
    if (!excludeOptionalPrefixesToStrip) {
        prefixesToStrip = prefixesToStrip.concat(OPTIONAL_PREFIXES_TO_STRIP);
    }
    var length = prefixesToStrip.length;
    for (var i = 0; i < length; i++) {
        var prefixToStrip = prefixesToStrip[i];
        var prefixRegEx = new RegExp('^' + prefixToStrip);
        url = url.replace(prefixRegEx, '');
    }
    return url;
}
/**
 * Strip out any optimizely_... query parameters from a url.
 * @private
 * @param {String} url
 * @return {String}
 */
function stripVarFromQuery(url) {
    // TODO (andrewk) rewrite using utils/location
    var urlParts = url.split('?');
    if (urlParts[1]) {
        var fragmentParts = urlParts[1].split('#');
        var query = fragmentParts[0];
        var fragment = fragmentParts[1];
        var urlQueryParts = query.split('&');
        var newQueryParts = [];
        urlQueryParts.forEach(queryPart => {
            if (queryPart.indexOf(QUERY_PREFIX) !== 0) {
                newQueryParts.push(queryPart);
            }
        });
        urlParts[1] = '';
        if (newQueryParts.length > 0) {
            urlParts[1] = '?' + newQueryParts.join('&');
        }
        if (fragment) {
            urlParts[1] += '#' + fragment;
        }
        return urlParts.join('');
    }
    return url;
}
