/** snake_case (or space-separated) → "Sentence case". Empty string passes through. */
export function prettify_key(key) {
    if (!key)
        return '';
    const normalized = key.replace(/_/g, ' ').toLowerCase().trim();
    if (!normalized)
        return '';
    return normalized.charAt(0).toUpperCase() + normalized.slice(1);
}
/** Keys that imply currency formatting in the absence of an explicit type hint. */
export const CURRENCY_KEY_REGEX = /amount|total|interest|tax|fee|rent|net|balance|gross/i;
/** Permissive ISO date detection: YYYY-MM-DD or full ISO 8601. */
export function is_iso_date(value) {
    if (typeof value !== 'string')
        return false;
    return /^\d{4}-\d{2}-\d{2}(T[\d:.+\-Z]+)?$/.test(value);
}
const DEFAULT_TRUNCATE = 80;
const DEFAULT_CURRENCY = 'AUD';
function format_currency(n, locale, currency) {
    try {
        return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(n);
    }
    catch {
        return String(n);
    }
}
function format_number(n, locale) {
    try {
        return new Intl.NumberFormat(locale).format(n);
    }
    catch {
        return String(n);
    }
}
function as_number(value) {
    if (typeof value === 'number')
        return Number.isFinite(value) ? value : null;
    if (typeof value === 'string') {
        const n = Number(value.replace(/[^\d.\-]/g, ''));
        return Number.isFinite(n) ? n : null;
    }
    return null;
}
function format_date(value, locale) {
    const d = new Date(value);
    if (Number.isNaN(d.getTime()))
        return null;
    try {
        return new Intl.DateTimeFormat(locale, { year: 'numeric', month: 'short', day: 'numeric' }).format(d);
    }
    catch {
        return null;
    }
}
function truncate(s, max) {
    if (s.length <= max)
        return { display: s };
    return { display: s.slice(0, max) + '…', title: s };
}
export function format_extracted_field(value, opts) {
    if (value === null || value === undefined || value === '') {
        return { display: '' };
    }
    const truncate_chars = opts.truncate_chars ?? DEFAULT_TRUNCATE;
    const currency_code = opts.currency_code ?? DEFAULT_CURRENCY;
    if (typeof value === 'boolean') {
        return { display: value ? 'Yes' : 'No' };
    }
    if (opts.type_hint === 'currency') {
        const n = as_number(value);
        if (n !== null)
            return { display: format_currency(n, opts.locale, currency_code) };
        return truncate(String(value), truncate_chars);
    }
    if (opts.type_hint === 'number') {
        const n = as_number(value);
        if (n !== null)
            return { display: format_number(n, opts.locale) };
        return { display: String(value) };
    }
    if (opts.type_hint === 'date') {
        if (typeof value !== 'string')
            return { display: String(value) };
        const d = format_date(value, opts.locale);
        return { display: d ?? value };
    }
    if (typeof value === 'number') {
        if (!Number.isFinite(value))
            return { display: String(value) };
        if (CURRENCY_KEY_REGEX.test(opts.key)) {
            return { display: format_currency(value, opts.locale, currency_code) };
        }
        return { display: format_number(value, opts.locale) };
    }
    if (is_iso_date(value)) {
        const d = format_date(value, opts.locale);
        if (d !== null)
            return { display: d };
        // fall through to string handling
    }
    if (typeof value === 'string') {
        return truncate(value, truncate_chars);
    }
    if (typeof value === 'object') {
        try {
            return truncate(JSON.stringify(value), truncate_chars);
        }
        catch {
            return truncate(String(value), truncate_chars);
        }
    }
    return truncate(String(value), truncate_chars);
}
//# sourceMappingURL=format_extracted_field.js.map