import { addDaysIso } from './weekDates';

// Jours fériés légaux français à date fixe (clé = MM-DD, indépendant de l'année).
const FIXED_HOLIDAYS: Record<string, string> = {
  '01-01': "Jour de l'an",
  '05-01': 'Fête du travail',
  '05-08': 'Victoire 1945',
  '07-14': 'Fête nationale',
  '08-15': 'Assomption',
  '11-01': 'Toussaint',
  '11-11': 'Armistice',
  '12-25': 'Noël',
};

// Dimanche de Pâques (calendrier grégorien) — algorithme de Gauss, base des jours fériés
// mobiles (Lundi de Pâques/Ascension/Lundi de Pentecôte, tous décalés d'un nombre fixe de
// jours par rapport à Pâques).
function easterIso(year: number): string {
  const a = year % 19;
  const b = Math.floor(year / 100);
  const c = year % 100;
  const d = Math.floor(b / 4);
  const e = b % 4;
  const f = Math.floor((b + 8) / 25);
  const g = Math.floor((b - f + 1) / 3);
  const h = (19 * a + b - d - g + 15) % 30;
  const i = Math.floor(c / 4);
  const k = c % 4;
  const l = (32 + 2 * e + 2 * i - h - k) % 7;
  const m = Math.floor((a + 11 * h + 22 * l) / 451);
  const month = Math.floor((h + l - 7 * m + 114) / 31);
  const day = ((h + l - 7 * m + 114) % 31) + 1;
  return `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
}

/** Nom du jour férié légal français à cette date ISO (YYYY-MM-DD), ou null si aucun. */
export function frenchHolidayLabel(iso: string): string | null {
  const monthDay = iso.slice(5);
  if (FIXED_HOLIDAYS[monthDay]) return FIXED_HOLIDAYS[monthDay];

  const easter = easterIso(Number(iso.slice(0, 4)));
  if (iso === addDaysIso(easter, 1)) return 'Lundi de Pâques';
  if (iso === addDaysIso(easter, 39)) return 'Ascension';
  if (iso === addDaysIso(easter, 50)) return 'Lundi de Pentecôte';
  return null;
}
