import { UnitFormat } from './unit-format';
import { passthroughUnitScale } from './unit-scale/passthrough-unit-scale';
/**
 * Format for decimal numbers with units.
 */
export class DecimalUnitFormat extends UnitFormat {
    constructor(locale, { minimumFractionDigits = 0, maximumFractionDigits = Math.max(3, minimumFractionDigits), unitScale = passthroughUnitScale } = {
        minimumFractionDigits: 0,
        maximumFractionDigits: 3,
        unitScale: passthroughUnitScale
    }) {
        super();
        this.scaledUnitFormatters = new Map();
        // Workaround to avoid a ts error about signDisplay not accepting the value 'negative'.
        // It has been supported by browsers since 8/23, but TypeScript still hasn't
        // added it to the type definitions. See https://github.com/microsoft/TypeScript/issues/56269
        const signDisplay = 'negative';
        const intlNumberFormatOptions = {
            maximumFractionDigits,
            minimumFractionDigits,
            signDisplay
        };
        for (const scaledUnit of unitScale.supportedScaledUnits) {
            this.scaledUnitFormatters.set(scaledUnit.scaleFactor, scaledUnit.scaledUnitFormatFactory({
                locale,
                intlNumberFormatOptions
            }));
        }
        this.unitScale = unitScale;
        this.minimumFractionDigits = minimumFractionDigits;
        this.maximumFractionDigits = maximumFractionDigits;
    }
    resolvedOptions() {
        return {
            unitScale: this.unitScale,
            maximumFractionDigits: this.maximumFractionDigits,
            minimumFractionDigits: this.minimumFractionDigits
        };
    }
    tryFormat(number) {
        const { scaledValue, scaledUnit } = this.unitScale.scaleNumber(number);
        const scaledUnitFormatter = this.scaledUnitFormatters.get(scaledUnit.scaleFactor);
        return scaledUnitFormatter.format(scaledValue);
    }
}
//# sourceMappingURL=decimal-unit-format.js.map