import { formatRange } from '@/composables/useDateTime';
import { translate } from '@/composables/useI18n';
import type { ConflictEntry } from './types';

export function formatMoney(amount: string | number | null | undefined, currency?: string | null): string | null {
    if (amount === null || amount === undefined || amount === '') {
        return null;
    }

    const value = Number(amount);
    const code = currency || 'EUR';

    try {
        return new Intl.NumberFormat(undefined, { style: 'currency', currency: code }).format(value);
    } catch {
        return `${value} ${code}`;
    }
}

/** "Già confermato per «Serata» (sab 10 dic, 21:00 – 22:00)" */
export function explainConflict(item: ConflictEntry, fallbackZone?: string | null): string {
    const what = translate(`shows.conflicts.${item.type}`, { title: item.title ?? '' });

    return `${what} · ${formatRange(item.starts_at, item.ends_at, item.timezone ?? fallbackZone, 'short')}`;
}

/** Logical changed-field keys (date, venue, …) → "data, locale". */
export function changedFieldsText(fields: string[]): string {
    return fields.map((f) => translate(`shows.impact.fields.${f}`)).join(', ');
}

/** Whether a show-local end time ("01:30") falls on the next day relative to the start ("22:00"). */
export function endsNextDay(start?: string | null, end?: string | null): boolean {
    return !!start && !!end && end <= start;
}
