import { UnitFormat } from '@ni/unit-format';
import { unitScalePassthrough } from '@ni/unit-format/unit-scale/passthrough';
import { UnitFormatDefault } from '@ni/unit-format/default';
import { UnitFormatDecimal } from '@ni/unit-format/decimal';
import { NumberTextFormat } from '../types';
/**
 * Format for numbers (with optional units) in a number-text table column.
 */
export class UnitFormatNumberText extends UnitFormat {
    constructor(locale, options) {
        super();
        this._resolvedOptions = this.resolveOptions(options);
        this.resolvedUnitFormat = this.resolveUnitFormat(locale, this._resolvedOptions);
    }
    resolvedOptions() {
        return { ...this._resolvedOptions };
    }
    optionsMatch(targetOptions) {
        const targetResolvedOptions = this.resolveOptions(targetOptions);
        return (this._resolvedOptions.decimalDigits
            === targetResolvedOptions.decimalDigits
            && this._resolvedOptions.decimalMaximumDigits
                === targetResolvedOptions.decimalMaximumDigits
            && this._resolvedOptions.numberTextFormat
                === targetResolvedOptions.numberTextFormat
            && this._resolvedOptions.unitScale === targetResolvedOptions.unitScale);
    }
    tryFormat(number) {
        return this.resolvedUnitFormat.format(number);
    }
    resolveUnitFormat(locale, options) {
        const { numberTextFormat, decimalMaximumDigits, decimalDigits, unitScale } = options;
        if (numberTextFormat === NumberTextFormat.default) {
            return new UnitFormatDefault(locale, {
                unitScale
            });
        }
        if (typeof decimalDigits === 'number') {
            return new UnitFormatDecimal(locale, {
                minimumFractionDigits: decimalDigits,
                maximumFractionDigits: decimalDigits,
                unitScale
            });
        }
        return new UnitFormatDecimal(locale, {
            minimumFractionDigits: 0,
            maximumFractionDigits: decimalMaximumDigits,
            unitScale
        });
    }
    resolveOptions(options) {
        if (options?.numberTextFormat === NumberTextFormat.default) {
            return {
                numberTextFormat: NumberTextFormat.default,
                decimalDigits: undefined,
                decimalMaximumDigits: undefined,
                unitScale: options?.unitScale ?? unitScalePassthrough
            };
        }
        const hasDecimalDigits = typeof options.decimalDigits === 'number';
        const hasDecimalMaximumDigits = typeof options.decimalMaximumDigits === 'number';
        if (hasDecimalDigits && hasDecimalMaximumDigits) {
            throw new Error('decimalDigits and decimalMaximumDigits are mutually exclusive. Do not specify both.');
        }
        if (!hasDecimalDigits && !hasDecimalMaximumDigits) {
            return {
                numberTextFormat: NumberTextFormat.decimal,
                decimalDigits: UnitFormatNumberText.defaultDecimalDigits,
                decimalMaximumDigits: undefined,
                unitScale: options.unitScale ?? unitScalePassthrough
            };
        }
        return {
            numberTextFormat: NumberTextFormat.decimal,
            decimalDigits: options.decimalDigits,
            decimalMaximumDigits: options.decimalMaximumDigits,
            unitScale: options.unitScale ?? unitScalePassthrough
        };
    }
}
UnitFormatNumberText.defaultDecimalDigits = 2;
//# sourceMappingURL=unit-format-number-text.js.map