var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};
import { resolveSupportedLocales } from '@formatjs/intl-utils';
export function isUnitSupported(unit) {
    try {
        new Intl.NumberFormat(undefined, {
            style: 'unit',
            unit: unit,
        });
    }
    catch (e) {
        return false;
    }
    return true;
}
var NativeNumberFormat = Intl.NumberFormat;
function findUnitData(locale, unit) {
    var data = UnifiedNumberFormat.__unitLocaleData__;
    var parentLocale = '';
    locale = locale.toLowerCase();
    if (!data[locale]) {
        parentLocale = locale.split('-')[0];
    }
    else {
        if (data[locale].units[unit]) {
            return data[locale].units[unit];
        }
        if (data[locale].parentLocale) {
            parentLocale = data[locale].parentLocale;
        }
        else {
            throw new RangeError("Cannot find data for " + locale);
        }
    }
    return findUnitData(parentLocale, unit);
}
function intersection(arr1, arr2) {
    return arr1.filter(function (s) { return ~arr2.indexOf(s); });
}
var DEFAULT_LOCALE = new NativeNumberFormat().resolvedOptions().locale;
var UnifiedNumberFormat = /** @class */ (function () {
    function UnifiedNumberFormat(locales, _a) {
        if (_a === void 0) { _a = {}; }
        var style = _a.style, unit = _a.unit, unitDisplay = _a.unitDisplay, options = __rest(_a, ["style", "unit", "unitDisplay"]);
        this.unit = undefined;
        this.unitDisplay = undefined;
        if (style === 'unit') {
            if (!unit) {
                throw new TypeError('Unit is required for `style: unit`');
            }
            this.unit = unit;
            this.unitDisplay = unitDisplay || 'short';
            var resolvedLocale = resolveSupportedLocales(intersection(NativeNumberFormat.supportedLocalesOf(locales), Intl.PluralRules.supportedLocalesOf(locales)).concat([
                DEFAULT_LOCALE,
            ]), UnifiedNumberFormat.__unitLocaleData__)[0];
            this.patternData = findUnitData(resolvedLocale, this.unit);
        }
        this.nf = new NativeNumberFormat(locales, __assign({}, options, { style: style === 'unit' ? 'decimal' : style }));
        this.locale = this.nf.resolvedOptions().locale;
    }
    UnifiedNumberFormat.prototype.format = function (num) {
        var formattedNum = this.nf.format(num);
        if (this.patternData) {
            var pl = new Intl.PluralRules(this.locale).select(num);
            var pattern = this.patternData[this.unitDisplay][pl === 'one' ? 'one' : 'other'];
            return pattern.replace('{0}', formattedNum);
        }
        return formattedNum;
    };
    UnifiedNumberFormat.prototype.formatToParts = function (num) {
        return this.nf.formatToParts(num);
    };
    UnifiedNumberFormat.prototype.resolvedOptions = function () {
        var ro = this.nf.resolvedOptions();
        if (this.unit) {
            ro.style = 'unit';
            ro.unit = this.unit;
            ro.unitDisplay = this.unitDisplay;
        }
        return ro;
    };
    UnifiedNumberFormat.supportedLocalesOf = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        return resolveSupportedLocales(args[0], UnifiedNumberFormat.__unitLocaleData__);
    };
    UnifiedNumberFormat.__addUnitLocaleData = function (data) {
        Object.keys(data).forEach(function (locale) {
            var datum = data[locale];
            if (!(datum && datum.locale)) {
                throw new Error('Locale data provided to UnifiedNumberFormat is missing a ' +
                    '`locale` property value');
            }
            UnifiedNumberFormat.__unitLocaleData__[datum.locale.toLowerCase()] = datum;
        });
    };
    UnifiedNumberFormat.polyfilled = true;
    UnifiedNumberFormat.__unitLocaleData__ = {};
    return UnifiedNumberFormat;
}());
export { UnifiedNumberFormat };
