// Date/score formatting in the viewer's local timezone.

const time = new Intl.DateTimeFormat(undefined, { hour: '2-digit', minute: '2-digit' });
const dayLong = new Intl.DateTimeFormat(undefined, { weekday: 'short', day: 'numeric', month: 'short' });
const dayKey = new Intl.DateTimeFormat('en-CA', { year: 'numeric', month: '2-digit', day: '2-digit' });

export function kickoffTime(iso: string): string {
  return time.format(new Date(iso));
}

export function kickoffDay(iso: string): string {
  return dayLong.format(new Date(iso));
}

/** Stable local-day key (YYYY-MM-DD) for grouping fixtures by date. */
export function dayKeyOf(iso: string): string {
  return dayKey.format(new Date(iso));
}

export function isToday(iso: string): boolean {
  return dayKeyOf(iso) === dayKey.format(new Date());
}

/** "in 2h", "live", "today 19:00", "Thu 11 Jun" — a compact when-label. */
export function relativeKickoff(iso: string): string {
  const diffMin = Math.round((new Date(iso).getTime() - Date.now()) / 60000);
  if (diffMin < 0) return kickoffTime(iso);
  if (diffMin < 60) return `in ${diffMin}m`;
  if (isToday(iso)) return `today ${kickoffTime(iso)}`;
  if (diffMin < 60 * 24 * 7) return `${kickoffDay(iso)} · ${kickoffTime(iso)}`;
  return kickoffDay(iso);
}
