import { UnitFormat } from './unit-format';
import { passthroughUnitScale } from './unit-scale/passthrough-unit-scale';
// Workaround to avoid ts errors 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';
/**
 * Format for numbers with units to show in a tabular form.
 * Large and tiny numbers are shown exponentially and the rest as decimal.
 */
export class DefaultUnitFormat extends UnitFormat {
    constructor(locale, { unitScale = passthroughUnitScale } = {
        unitScale: passthroughUnitScale
    }) {
        super();
        // Format options to use by default. It renders the number with a maximum of 6 signficant digits (including zero before decimal point).
        this.defaultIntlNumberFormatOptions = {
            maximumSignificantDigits: DefaultUnitFormat.maximumDigits,
            maximumFractionDigits: DefaultUnitFormat.maximumDigits - 1,
            roundingPriority: 'lessPrecision',
            signDisplay
        };
        this.defaultScaledUnitFormatters = new Map();
        // Format options for numbers that should be displayed in exponential notation. This should be used
        // for numbers with magintudes over 'exponentialUpperBound' or under 'exponentialLowerBound'.
        this.exponentialIntlNumberFormatOptions = {
            maximumSignificantDigits: DefaultUnitFormat.maximumDigits,
            notation: 'scientific',
            signDisplay
        };
        for (const unit of unitScale.supportedScaledUnits) {
            this.defaultScaledUnitFormatters.set(unit.scaleFactor, unit.scaledUnitFormatFactory({
                locale,
                intlNumberFormatOptions: this.defaultIntlNumberFormatOptions
            }));
        }
        this.exponentialScaledUnitFormatter = unitScale.baseScaledUnit.scaledUnitFormatFactory({
            locale,
            intlNumberFormatOptions: this.exponentialIntlNumberFormatOptions
        });
        this.unitScale = unitScale;
    }
    resolvedOptions() {
        return {
            unitScale: this.unitScale
        };
    }
    tryFormat(number) {
        const { scaledValue, scaledUnit } = this.unitScale.scaleNumber(number);
        const absoluteValue = Math.abs(scaledValue);
        const useExponential = absoluteValue !== 0
            && (absoluteValue >= DefaultUnitFormat.exponentialUpperBound
                || absoluteValue < DefaultUnitFormat.exponentialLowerBound);
        return useExponential
            ? this.exponentialScaledUnitFormatter.format(number)
            : this.defaultScaledUnitFormatters
                .get(scaledUnit.scaleFactor)
                .format(scaledValue);
    }
}
// The maximum number of digits that should be rendered for any given value.
DefaultUnitFormat.maximumDigits = 6;
// Use exponential notation for numbers that will be rendered with 3 leading 0s or more.
// Because a maximum of 6 digits are rendered, showing more than 3 leading 0s is not ideal
// because then at least half of the displayed digits will be leading 0s.
DefaultUnitFormat.exponentialLowerBound = 0.000995;
// Use exponential formatting for numbers whose magnitude cannot otherwise be displayed
// with 6 digits or less.
DefaultUnitFormat.exponentialUpperBound = 999999.5;
//# sourceMappingURL=default-unit-format.js.map