/**
 * Shared date formatting helpers for the news module.
 *
 * We centralise these so the list page, detail page, and future widgets
 * all render dates in the same en-GB long form ("13 April 2026") without
 * re-importing `Intl.DateTimeFormat` everywhere.
 */

const longDateFormatter = new Intl.DateTimeFormat("en-GB", {
  year: "numeric",
  month: "long",
  day: "numeric",
});

export function formatLongDate(iso: string): string {
  const d = new Date(iso);
  if (Number.isNaN(d.getTime())) return "";
  return longDateFormatter.format(d);
}

/**
 * Returns a list of unique, descending-sorted years present in the
 * supplied items. Used by the filter component to build its dropdown
 * without having to re-traverse items client-side.
 */
export function uniqueYears(items: Array<{ year: number }>): number[] {
  const set = new Set<number>();
  for (const item of items) set.add(item.year);
  return Array.from(set).sort((a, b) => b - a);
}
