export type Interval = { start: number; end: number };export class SlotError extends Error { constructor(public readonly kind: "invalid_input") { super(kind); }}/** Returns the earliest start minute, or undefined when nothing fits. Throws SlotError. */export function findFreeSlot(busy: Interval[], dayStart: number, dayEnd: number, durationMin: number): number | undefined { const ints = [dayStart, dayEnd, durationMin, ...busy.flatMap((b) => [b.start, b.end])]; if (!ints.every((n) => Number.isInteger(n))) throw new SlotError("invalid_input"); if (durationMin <= 0 || dayEnd <= dayStart || busy.some((b) => b.end <= b.start)) { throw new SlotError("invalid_input"); } const sorted = [...busy].sort((a, b) => a.start - b.start); let cursor = dayStart; for (const b of sorted) { if (b.end <= cursor) continue; if (b.start >= cursor + durationMin) break; cursor = b.end; } return cursor + durationMin <= dayEnd ? cursor : undefined;}