/**
 * Converts a Numbro.js format pattern to an Excel `numFmt` string.
 *
 * Applies the minimum transformation needed for common patterns.
 * Numbro uses `0,0` to mean "thousands-grouped, at least one digit";
 * Excel uses `#,##0` for the same. All other pattern characters are
 * compatible between the two systems.
 *
 * Examples:
 * - `'$0,0.00'` → `'$#,##0.00'`
 * - `'0,0.00'`  → `'#,##0.00'`
 * - `'0,0'`     → `'#,##0'`
 * - `'0.00'`    → `'0.00'` (unchanged)
 * - `'0%'`      → `'0%'` (unchanged)
 *
 * @private
 * @param {*} pattern Numbro format pattern string (e.g. `'$0,0.00'`).
 * @returns {string|null}
 */ "use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get intlNumFormatToExcelNumFmt () {
        return intlNumFormatToExcelNumFmt;
    },
    get numbroPatternToExcelNumFmt () {
        return numbroPatternToExcelNumFmt;
    }
});
function numbroPatternToExcelNumFmt(pattern) {
    if (!pattern || typeof pattern !== 'string') {
        return null;
    }
    return pattern.replace(/0,0/g, '#,##0');
}
/**
 * Resolves the currency symbol and its position (prefix or suffix) for a given
 * ISO 4217 currency code and locale using the `Intl.NumberFormat` API.
 *
 * @private
 * @param {string} currency ISO 4217 currency code (e.g. `'USD'`, `'EUR'`).
 * @param {string|undefined} locale BCP 47 locale tag (e.g. `'en-US'`).
 * @returns {{ symbol: string, isPrefix: boolean }}
 */ function getCurrencyInfo(currency, locale) {
    try {
        const parts = new Intl.NumberFormat(locale || 'en', {
            style: 'currency',
            currency,
            minimumFractionDigits: 0,
            maximumFractionDigits: 0
        }).formatToParts(0);
        const symbol = parts.find((p)=>p.type === 'currency')?.value ?? currency;
        const currencyIndex = parts.findIndex((p)=>p.type === 'currency');
        const integerIndex = parts.findIndex((p)=>p.type === 'integer');
        const isPrefix = currencyIndex < integerIndex;
        return {
            symbol,
            isPrefix
        };
    } catch  {
        // Intl.NumberFormat throws for unrecognised currency codes. Fall back to
        // the raw currency string placed as a prefix so the export still produces
        // a usable result.
        return {
            symbol: currency,
            isPrefix: true
        };
    }
}
function intlNumFormatToExcelNumFmt(numericFormat, locale) {
    if (!numericFormat) {
        return null;
    }
    // Legacy: numbro.js pattern (deprecated since Handsontable 17).
    if (numericFormat.pattern) {
        return numbroPatternToExcelNumFmt(numericFormat.pattern);
    }
    // Current: Intl.NumberFormat options.
    const { style, currency, minimumFractionDigits = 0, maximumFractionDigits, useGrouping = true } = numericFormat;
    const fractionDigits = maximumFractionDigits ?? minimumFractionDigits;
    const decimalPart = fractionDigits > 0 ? `.${'0'.repeat(fractionDigits)}` : '';
    const intPart = useGrouping !== false ? '#,##0' : '0';
    if (style === 'percent') {
        return `${intPart}${decimalPart}%`;
    }
    if (style === 'currency' && currency) {
        const { symbol, isPrefix } = getCurrencyInfo(currency, locale);
        // @TODO: handle locale-specific spacing between symbol and number
        // (e.g. non-breaking space in fr-FR). Excel numFmt requires "\ " for a
        // literal space, which varies per locale and is left for a follow-up.
        return isPrefix ? `${symbol}${intPart}${decimalPart}` : `${intPart}${decimalPart}${symbol}`;
    }
    return `${intPart}${decimalPart}`;
}
