import { formatDocumentDate, type DatePrecision } from '$lib/shared/utils/documentDate';
import { getLocale } from '$lib/paraglide/runtime.js';

/**
 * Renders a timeline event's date by delegating to the shared
 * {@link formatDocumentDate} — so a timeline chip reads identically to the same
 * date on a document, in whichever locale is active. This module is a thin
 * façade: it owns NO precision or rendering logic (REQ-005), only the two
 * timeline-specific decisions below.
 *
 * @param eventDate the event's anchor day (`YYYY-MM-DD`). `null`, `undefined`
 *   and `''` are equivalent — all mean "undated" and yield `null` WITHOUT
 *   calling the formatter (REQ-004).
 * @param precision the event's precision metadata; `'UNKNOWN'` yields `null`
 *   (no chip) even when a date is present (REQ-003).
 * @param eventDateEnd the RANGE end day; `undefined` and `null` are equivalent
 *   and both mean an open-ended range.
 * @returns the localized label, or `null` for an UNKNOWN/undated event.
 */
export function timelineDateLabel(
	eventDate: string | null | undefined,
	precision: DatePrecision,
	eventDateEnd?: string | null
): string | null {
	if (precision === 'UNKNOWN' || !eventDate) return null;
	// raw is always null for timeline events — there is no verbatim spreadsheet
	// cell to interpolate; season words derive from the structured anchor month
	// (never from untrusted text). See documentDate.ts for the raw contract.
	return formatDocumentDate(eventDate, precision, eventDateEnd ?? null, null, getLocale());
}
