import { formatDocumentDate, type DatePrecision } from '$lib/shared/utils/documentDate';

/**
 * Formats one life date (birth or death) at the precision the data claims,
 * delegating all rendering to {@link formatDocumentDate}. Returns '' for a
 * missing date. Carries no * / † glyph — components that need the glyphs wrap
 * them in their own `aria-hidden` markup so screen readers only hear the date.
 *
 * A missing precision falls back to YEAR: pre-V76 rows only knew a year, and
 * a bare year is the only safe rendering for a date without precision metadata.
 */
export function formatLifeDate(
	date: string | null | undefined,
	precision: DatePrecision | null | undefined,
	locale?: string
): string {
	if (!date) {
		return '';
	}
	return formatDocumentDate(date, precision ?? 'YEAR', null, null, locale);
}

/**
 * Formats the full life date range as plain text, e.g. for dropdown subtitles.
 * Examples:
 *   * 14. März 1901 – † 2. November 1944   (both, DAY precision)
 *   * 1882                                  (birth only, YEAR precision)
 *   † ca. 1944                              (death only, APPROX precision)
 *   ""                                      (neither)
 */
export function formatLifeDateRange(
	birthDate: string | null | undefined,
	birthDatePrecision: DatePrecision | null | undefined,
	deathDate: string | null | undefined,
	deathDatePrecision: DatePrecision | null | undefined,
	locale?: string
): string {
	const birth = birthDate ? `* ${formatLifeDate(birthDate, birthDatePrecision, locale)}` : null;
	const death = deathDate ? `† ${formatLifeDate(deathDate, deathDatePrecision, locale)}` : null;
	if (birth && death) {
		return `${birth} – ${death}`;
	}
	return birth ?? death ?? '';
}
