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

/**
 * Formats one life date (birth or death) at the precision the data claims.
 * Thin domain alias over the shared {@link formatDatePart}: carries no * / †
 * glyph — components that need the glyphs wrap them in their own `aria-hidden`
 * markup so screen readers only hear the date.
 */
export function formatLifeDate(
	date: string | null | undefined,
	precision: DatePrecision | null | undefined,
	locale?: string
): string {
	return formatDatePart(date, precision, 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 ?? '';
}
