"use strict";
/**
 * Matcher for IP Geolocation ("location")
 *
 * Ported from [feature_location@ea4ca3c627c5e9683f58e018da7f2ee923c3e32b](https://github.com/optimizely/client-js/blob/ea4ca3c627c5e9683f58e018da7f2ee923c3e32b/src/plugins/feature_location/matchers/location.js#L18).
 * Changelog:
 *   - Return undefined on unknown location
 * @module
 */
exports.fieldsNeeded = ['location'];
/**
 * @private
 * @param {Object} profile
 *        The visitor profile.
 * @param {Object} cond
 * @param {string} cond.value Expected value as [COUNTRY]|[REGION]|[CITY]|[CONTINENT]|[DMA]
 * @returns {boolean}
 */
exports.match = function (profile, cond) {
    if (!profile.location) {
        return undefined;
    }
    var visitorLocation = profile.location;
    var loc = cond.value, locSplit = loc.split('|'), country = (locSplit[0] || '').trim(), region = (locSplit[1] || '').trim(), city = (locSplit[2] || '').trim(), continent = (locSplit[3] || '').trim(), dma = ((locSplit.length > 4 && locSplit[4]) || '').trim();
    // BUG-7439 temporary fix, remove once geotargeting locations updated to CF
    region = region === 'EN' ? 'ENG' : region;
    switch (locSplit.length) {
        case 1:
            if (visitorLocation.country === country) {
                return true;
            }
            break;
        case 2:
            if (visitorLocation.region === region &&
                visitorLocation.country === country) {
                return true;
            }
            break;
        case 3:
            if (visitorLocation.city === city &&
                (visitorLocation.region === region || region === '') &&
                visitorLocation.country === country) {
                return true;
            }
            break;
        case 4:
            // If "continent" is specified then all other fields are irrelevant
            if (visitorLocation.continent === continent) {
                return true;
            }
            break;
        case 5:
            // If dma is specified then all other fields are irrelevant
            if (visitorLocation.dma === dma) {
                return true;
            }
    }
    return false;
};
