BestAvailableLocale = function (isAvailableLocale, locale) {
        if (locale === undefined) {
            return undefined;
        }

        let candidate = locale;
        const hyphen = "-";
        while (true) {
            if (isAvailableLocale(candidate)) {
                return candidate;
            }

            let pos = -1;
            for (let i = candidate.length - 1; i >= 0; i--) {
                if (candidate[i] === hyphen) {
                    pos = i;
                    break;
                }
            }

            if (pos === -1) {
                return undefined;
            } else if (pos >= 2 && candidate[pos - 2] === hyphen) {
                // This is spec code likely intended to skip over singletons,
                // such that if we just searched for "en-a-value",
                // pos would initially truncate the candidate to "en-a", which
                // is not a valid language tag.
                // See https://tools.ietf.org/html/rfc5646#section-4.4.2
                pos -= 2;
            }

            candidate = _.substring(candidate, 0, pos);
        }
    }
Determines the best possible locale available in the system

ECMA-402: #sec-bestavailablelocale

@param {Function} isAvailableLocale A function that takes a locale and returns if the locale is supported
@param {String} locale the locale (including its fallbacks) that will be searched for
@returns {String} the given locale or one of its fallbacks, or undefined