"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.currencies = exports.EXCHANGE_RATE_SOURCE = void 0;
exports.convertUsdCost = convertUsdCost;
exports.formatUsdCost = formatUsdCost;
exports.formatCurrency = formatCurrency;
exports.EXCHANGE_RATE_SOURCE = 'https://latest.currency-api.pages.dev/v1/currencies/usd.json';
exports.currencies = [
    { code: 'USD', label: 'USD' },
    { code: 'CNY', label: 'CNY' },
];
function convertUsdCost(value, currency, exchangeRate) {
    if (currency === 'USD')
        return value;
    if (exchangeRate?.rate && Number.isFinite(exchangeRate.rate))
        return value * exchangeRate.rate;
    return null;
}
function formatUsdCost(value, currency, locale, exchangeRate, compact = false) {
    const converted = convertUsdCost(value, currency, exchangeRate);
    return converted === null ? '--' : formatCurrency(converted, currency, locale, compact);
}
function formatCurrency(value, currency, locale, compact = false) {
    const notation = compact && Math.abs(value) >= 1000 ? 'compact' : 'standard';
    const fractionDigits = value >= 100 || notation === 'compact'
        ? 0
        : value >= 1
            ? 2
            : value > 0
                ? 3
                : 0;
    return new Intl.NumberFormat(locale, {
        style: 'currency',
        currency,
        currencyDisplay: 'narrowSymbol',
        notation,
        minimumFractionDigits: fractionDigits,
        maximumFractionDigits: fractionDigits,
    }).format(value);
}
