/**
 * Date display helpers. Timestamps cross the wire as UTC ISO-8601; Shamsi (Persian
 * calendar) display is a client concern (see money-and-types.md). We render via the
 * Intl Persian calendar — no date library needed.
 */

const SHAMSI_LOCALE = 'fa-IR-u-ca-persian';

/** Resolves the Intl locale for date formatting from the app locale. */
function intlLocale(locale: string): string {
  return locale === 'fa' ? SHAMSI_LOCALE : 'en-US';
}

/** Formats a UTC ISO timestamp as a localized date (Shamsi for `fa`, Gregorian for `en`). */
export function formatShamsiDate(
  iso: string | Date,
  locale: string = 'fa',
  options: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric' }
): string {
  const date = iso instanceof Date ? iso : new Date(iso);
  if (Number.isNaN(date.getTime())) return '';
  return new Intl.DateTimeFormat(intlLocale(locale), options).format(date);
}

/** Formats a UTC ISO timestamp as a localized date + time. */
export function formatShamsiDateTime(iso: string | Date, locale: string = 'fa'): string {
  return formatShamsiDate(iso, locale, {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
  });
}
