// Pure helpers for the terminal blocked screen, kept out of the .svelte component so they are
// unit-testable in the node vitest env.

import type { Locale } from './i18n/index.svelte';

/**
 * formatBlockUntil renders a temporary block's RFC3339 UTC expiry in the user's locale and
 * timezone, e.g. "1 Jul 2026, 15:00". It falls back to UTC formatting when the timezone is
 * unknown/invalid, and to the raw string when the instant cannot be parsed at all.
 */
export function formatBlockUntil(until: string, locale: Locale, timeZone: string): string {
  const d = new Date(until);
  if (Number.isNaN(d.getTime())) return until;
  try {
    return new Intl.DateTimeFormat(locale, {
      dateStyle: 'medium',
      timeStyle: 'short',
      timeZone: timeZone || undefined,
    }).format(d);
  } catch {
    try {
      return new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short' }).format(d);
    } catch {
      return until;
    }
  }
}
