// Israeli public-holiday calendar for business-day arithmetic.
// Saturdays are excluded as the weekly day of rest (Shabbat).
// Sundays through Fridays are work days unless they fall on a public holiday.
//
// Dates derived from Hebcal authoritative Hebrew calendar API (https://www.hebcal.com/)
// using i=on (Israel calendar, not diaspora) for years 2026-2028.
// API call used: https://www.hebcal.com/hebcal?v=1&cfg=json&maj=on&min=off&mod=on&nx=off&start=2026-01-01&end=2028-12-31&i=on
// Included: yomtov:true holidays + Chol HaMoed (CH''M) days + Tisha B'Av + Yom HaZikaron + Yom HaAtzmaut.
// Fridays excluded from Set — the weekday check (day===6 for Saturday) already handles Shabbat;
// Friday (day===5) IS a work day in Israel.
//
// Update this file when new years are needed; re-run the Hebcal API call above.
//
// Format: 'YYYY-MM-DD' strings (UTC dates — use UTC methods throughout).

const IL_HOLIDAYS = new Set<string>([
  // ── 2026 ──────────────────────────────────────────────────────────────────
  // Pesach (Passover): Thu Apr 2 (Pesach I, 15 Nisan 5786)
  //   Chol HaMoed: Apr 3-7 · Pesach VII: Apr 8
  '2026-04-02', '2026-04-03', '2026-04-04', '2026-04-05', '2026-04-06', '2026-04-07', '2026-04-08',
  // Yom HaZikaron: Wed Apr 21 · Yom HaAtzmaut: Thu Apr 22 (Hebcal-resolved, i=on)
  '2026-04-21', '2026-04-22',
  // Shavuot: Fri May 22 (IL = 1-day; already Fri so also adjacent to Shabbat)
  '2026-05-22',
  // Tisha B'Av: Thu Jul 23
  '2026-07-23',
  // Rosh Hashanah 5787: Sat-Sun Sep 12-13 (Sep 12 Sat excluded by weekday check too; kept for completeness)
  '2026-09-12', '2026-09-13',
  // Yom Kippur: Mon Sep 21
  '2026-09-21',
  // Sukkot I: Sat Sep 26 (excluded by weekday check too; kept for completeness)
  //   Chol HaMoed Sukkot: Sep 27-Oct 2 (Sukkot II-VII + Hoshana Raba)
  //   Shmini Atzeret/Simchat Torah: Sat Oct 3 (excluded by weekday check too; kept)
  '2026-09-26', '2026-09-27', '2026-09-28', '2026-09-29', '2026-09-30', '2026-10-01', '2026-10-02', '2026-10-03',

  // ── 2027 ──────────────────────────────────────────────────────────────────
  // Pesach (Passover): Thu Apr 22 (Pesach I, 15 Nisan 5787)
  //   Chol HaMoed: Apr 23-27 · Pesach VII: Wed Apr 28
  '2027-04-22', '2027-04-23', '2027-04-24', '2027-04-25', '2027-04-26', '2027-04-27', '2027-04-28',
  // Yom HaZikaron: Tue May 11 · Yom HaAtzmaut: Wed May 12 (Hebcal-resolved, i=on, 5 Iyar 5787)
  '2027-05-11', '2027-05-12',
  // Shavuot: Fri Jun 11
  '2027-06-11',
  // Tisha B'Av: Thu Aug 12
  '2027-08-12',
  // Rosh Hashanah 5788: Sat-Sun Oct 2-3
  '2027-10-02', '2027-10-03',
  // Yom Kippur: Mon Oct 11
  '2027-10-11',
  // Sukkot I: Sat Oct 16
  //   Chol HaMoed Sukkot: Oct 17-22 (Sukkot II-VII + Hoshana Raba)
  //   Shmini Atzeret/Simchat Torah: Sun Oct 23
  '2027-10-16', '2027-10-17', '2027-10-18', '2027-10-19', '2027-10-20', '2027-10-21', '2027-10-22', '2027-10-23',

  // ── 2028 ──────────────────────────────────────────────────────────────────
  // Pesach (Passover): Tue Apr 11 (Pesach I, 15 Nisan 5788)
  //   Chol HaMoed: Apr 12-16 · Pesach VII: Mon Apr 17
  '2028-04-11', '2028-04-12', '2028-04-13', '2028-04-14', '2028-04-15', '2028-04-16', '2028-04-17',
  // Yom HaZikaron: Wed May 1 · Yom HaAtzmaut: Thu May 2 (Hebcal-resolved, i=on)
  '2028-05-01', '2028-05-02',
  // Shavuot: Thu May 31
  '2028-05-31',
  // Tisha B'Av: Wed Aug 1
  '2028-08-01',
  // Rosh Hashanah 5789: Thu-Fri Sep 21-22 (Sep 22 Fri also work day by default; kept for completeness)
  '2028-09-21', '2028-09-22',
  // Yom Kippur: Sun Sep 30
  '2028-09-30',
  // Sukkot I: Fri Oct 5
  //   Chol HaMoed Sukkot: Oct 6-11 (Sukkot II-VII + Hoshana Raba)
  //   Shmini Atzeret/Simchat Torah: Sat Oct 12 (excluded by weekday check too; kept)
  '2028-10-05', '2028-10-06', '2028-10-07', '2028-10-08', '2028-10-09', '2028-10-10', '2028-10-11', '2028-10-12',
]);

/** Returns true if `date` is a work day in Israel (Sun–Fri, not a public holiday). */
export function isBusinessDay(date: Date): boolean {
  const day = date.getUTCDay(); // 0=Sun,1=Mon,...,6=Sat
  if (day === 6) return false; // Saturday = Shabbat
  const key = date.toISOString().slice(0, 10);
  return !IL_HOLIDAYS.has(key);
}

/** Returns a new Date that is `n` Israeli business days after `from`. */
export function addBusinessDays(from: Date, n: number): Date {
  if (n < 0) throw new RangeError('addBusinessDays: n must be >= 0');
  let d = new Date(from.getTime());
  let remaining = n;
  while (remaining > 0) {
    d = new Date(d.getTime() + 86_400_000); // advance one day
    if (isBusinessDay(d)) remaining--;
  }
  return d;
}
