"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
/**
 * Retrieves a locale string from the navigator object.
 * Checks languages, userLanguage and language
 *
 * Uses the first valid language. If the first valid language doesn't contain a country code,
 * it will search for a locale string with the same language and a country code in the remaining options.
 *
 * @returns {(string | undefined)}
 */
exports.detectLocaleFromNavigator = function () {
    var e_1, _a;
    if (typeof navigator === "undefined") {
        return;
    }
    var candidates = new Set();
    if (navigator.languages && navigator.languages.length > 0) {
        navigator.languages.forEach(function (value) { return candidates.add(value); });
    }
    if ("userLanguage" in navigator && navigator["userLanguage"]) {
        candidates.add(navigator["userLanguage"]);
    }
    if ("language" in navigator && navigator.language) {
        candidates.add(navigator.language);
    }
    var firstCandidate = candidates.values().next().value;
    if (firstCandidate.indexOf("-") > -1) {
        return firstCandidate;
    }
    try {
        // first candidate is language only -> try to find the complete locale next
        for (var candidates_1 = tslib_1.__values(candidates), candidates_1_1 = candidates_1.next(); !candidates_1_1.done; candidates_1_1 = candidates_1.next()) {
            var locale = candidates_1_1.value;
            if (locale.indexOf(firstCandidate) === 0) {
                return locale;
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (candidates_1_1 && !candidates_1_1.done && (_a = candidates_1.return)) _a.call(candidates_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return firstCandidate;
};
