/**
 * Display helpers for numbers in the UI.
 *
 * Always uses Western Arabic digits (0–9) so ar-SA never switches to
 * Eastern Arabic–Indic digits (٠–٩).
 */

function resolveIntlLocale(locale?: string): string {
    if (locale === 'ar' || locale?.startsWith('ar')) {
        return 'ar-SA'
    }

    return 'en-US'
}

/**
 * Format a number for display (counts, days, non-currency quantities).
 * Undefined values render as "0".
 */
export function formatNumber(value: number | undefined, locale?: string): string {
    if (value === undefined) {
        return '0'
    }

    return value.toLocaleString(resolveIntlLocale(locale), {
        minimumFractionDigits: 0,
        maximumFractionDigits: 2,
        numberingSystem: 'latn',
    })
}
