import type { NormalizedSchedule, Schedule, TimeUnit } from "./types.js"; const MIN_INTERVAL_MS = 60_000; const MAX_INTERVAL_MS = 7 * 24 * 60 * 60_000; export function parseSchedule(schedule: Schedule): NormalizedSchedule { if (schedule === "continuous" || schedule === "manual") return { type: schedule }; const match = schedule.match(/^(\d+)(m|h|d)$/); if (!match?.[1] || !match[2]) { throw new Error(`Invalid schedule format: "${schedule}"`); } const value = Number.parseInt(match[1], 10); const multipliers: Record = { m: 60_000, h: 60 * 60_000, d: 24 * 60 * 60_000, }; const intervalMs = value * multipliers[match[2] as TimeUnit]; if (intervalMs < MIN_INTERVAL_MS) throw new Error("Schedule interval must be at least 1 minute"); if (intervalMs > MAX_INTERVAL_MS) throw new Error("Schedule interval must be at most 7 days"); return { type: "interval", intervalMs }; }