/**
 * Parsing the date and clock strings a time slot is validated against.
 *
 * Both are lenient on purpose: the opportunity's window arrives from several
 * endpoints in slightly different shapes, and a slot editor that threw on an
 * unexpected one would block the organiser rather than just widening the range.
 */
export const parseDate = (dateString: string | undefined): Date | null => {
  if (!dateString) return null;
  /* A bare `YYYY-MM-DD` (what the API sends) is a calendar day, so it is built
     as local midnight. `new Date("2026-11-20")` is UTC midnight — 19:00 on the
     19th west of Greenwich — which moved the slot calendar's last allowed day
     (and first) back by one for those visitors. */
  const day = /^(\d{4})-(\d{2})-(\d{2})$/.exec(dateString);
  if (day) return new Date(Number(day[1]), Number(day[2]) - 1, Number(day[3]));
  const date = new Date(dateString);
  return isNaN(date.getTime()) ? null : date;
};

// Extract hours and minutes from time strings (format: "HH:MM:SS")
export const parseTimeString = (timeString: string) => {
  if (!timeString) return { hours: 0, minutes: 0 };
  const [hours, minutes] = timeString.split(":").map(Number);
  return { hours, minutes };
};
