import { ScaledUnitFormat } from './scaled-unit-format';
/**
 * Representations of a unit in a particular language
 */
export class UnitTranslation {
    constructor(singular, plural, symbol) {
        this.singular = singular;
        this.plural = plural;
        this.symbol = symbol;
    }
}
/**
 * A formatter for units that are not supported by Intl.NumberFormat
 */
export class ManuallyTranslatedScaledUnitFormat extends ScaledUnitFormat {
    constructor(scaledUnitFormatFactoryOptions, { unitTranslations, scaledPrefixText }) {
        super(scaledUnitFormatFactoryOptions);
        if (!unitTranslations.get('en')) {
            throw new Error('English translations must exist with locale string "en"');
        }
        this.pluralRules = new Intl.PluralRules(this.locale);
        this.formatter = new Intl.NumberFormat(this.locale, this.intlNumberFormatOptions);
        this.unitTranslation = this.getTranslationToUse(unitTranslations, this.locale);
        this.scaledPrefixText = scaledPrefixText;
    }
    static createFactory(manuallyTranslatedScaledUnitFormatOptions) {
        return (scaledUnitFormatFactoryOptions) => new ManuallyTranslatedScaledUnitFormat(scaledUnitFormatFactoryOptions, manuallyTranslatedScaledUnitFormatOptions);
    }
    format(value) {
        const formatted = this.formatter.format(value);
        // For non-base units (which are a scaled prefix text of empty string)
        if (this.scaledPrefixText !== '') {
            return `${formatted} ${this.scaledPrefixText}${this.unitTranslation.symbol}`;
        }
        // Some languages have more than two forms (singular/plural) of cardinal
        // numbers, but we are treating anything other than the 'one' form as plural.
        // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules#description
        //
        // Because PluralRules.select() takes a number (not a string), it cannot differentiate
        // between 1 and 1.0. When NumberFormat is configured to format with a set number
        // of fractional digits, those fractional digits can have an effect on the pluralization
        // of the unit. E.g. in English, it formats "1 byte" vs "1.0 bytes". Thus there is
        // sometimes an inconsistency between unit pluralization for the same number, based
        // on whether it's supported by NumberFormat, or manually translated.
        const unitLabel = this.pluralRules.select(value) === 'one'
            ? this.unitTranslation.singular
            : this.unitTranslation.plural;
        return `${formatted} ${unitLabel}`;
    }
    getTranslationToUse(unitTranslations, locale) {
        const localeObject = new Intl.Locale(locale ?? 'en');
        const language = localeObject.language;
        const region = localeObject.region;
        const regionSpecificMatchedTranslations = region
            ? unitTranslations.get(`${language}-${region}`)
            : undefined;
        return (regionSpecificMatchedTranslations
            ?? unitTranslations.get(language)
            ?? unitTranslations.get('en'));
    }
}
/* eslint-enable max-classes-per-file */
//# sourceMappingURL=manually-translated-scaled-unit-format.js.map