import { isNumber } from "remeda";
import { intl } from "../../../i18n/index.js";
const createNumFormatter = (format, locale = intl.getLocale())=>{
    const { type = 'number', ratio = 1, thousandSeparator = true, prefix = '', suffix = '', symbol = '', fractionDigits = 2, significantDigits, roundingMode = 'halfExpand', roundingPriority = 'auto' } = format || {};
    const numFormatterOptions = {
        style: 'decimal',
        notation: 'scientific' === type ? 'scientific' : 'standard',
        roundingMode,
        roundingPriority
    };
    if (isNumber(fractionDigits)) {
        if (fractionDigits >= 0) {
            numFormatterOptions.minimumFractionDigits = fractionDigits;
            numFormatterOptions.maximumFractionDigits = fractionDigits;
        }
    }
    if (isNumber(significantDigits) && significantDigits > 0) {
        if (significantDigits > 0) {
            numFormatterOptions.minimumSignificantDigits = significantDigits;
            numFormatterOptions.maximumSignificantDigits = significantDigits;
        }
    }
    const numFormatter = new Intl.NumberFormat(locale, {
        ...numFormatterOptions,
        useGrouping: thousandSeparator
    });
    return (value)=>{
        let num = Number(value);
        let typeSymbol = '';
        if (Number.isNaN(num)) return String(value);
        if ('percent' === type) {
            num *= 100;
            typeSymbol = '%';
        } else if ('permille' === type) {
            num *= 1000;
            typeSymbol = "\u2030";
        } else if ('number' === type) num /= ratio || 1;
        let numStr = numFormatter.format(num);
        if (thousandSeparator) {
            const parts = numStr.split('.');
            parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
            numStr = parts.join('.');
        }
        const validSymbol = symbol ?? '';
        return `${prefix}${numStr}${typeSymbol}${validSymbol}${suffix}`;
    };
};
const autoNumFormatter = (value, locale = intl.getLocale())=>{
    if (null == value) return String(value);
    const num = Number(value);
    if (Number.isNaN(num)) return String(value);
    const numFormatterOptions = {
        style: 'decimal',
        notation: 'compact',
        useGrouping: true
    };
    numFormatterOptions.minimumFractionDigits = 0;
    numFormatterOptions.maximumFractionDigits = 2;
    const numFormatter = new Intl.NumberFormat(locale, {
        ...numFormatterOptions
    });
    return numFormatter.format(Number(value));
};
export { autoNumFormatter, createNumFormatter };

//# sourceMappingURL=createNumFormatter.js.map