/** * Scheduled catalog and menu availability windows. * * Models recurring daypart or timed availability (breakfast menus, lunch * specials, seasonal farm drops, service booking windows) using day-of-week * minute intervals, including overnight intervals that cross midnight. * * Pure and runtime-agnostic: safe on server, web storefront, and native Craft. */ import type { OpeningInterval } from './hours' import { MINUTES_PER_DAY, openMinutesRemaining, overnightClose } from './hours' export type AvailabilityWindow = Omit export interface AvailabilityClock { dayOfWeek: number minuteOfDay: number } export interface AvailabilityScheduleRow { productId?: number | null categoryId?: number | null windows: unknown } export interface CatalogAvailabilityIndex { available: (productId: number, categoryId: number) => boolean timed: (productId: number, categoryId: number) => boolean } /** * Validate, normalize, and sort recurring day-of-week availability windows. * Returns null if the input is malformed. */ export function parseAvailabilityWindows(input: unknown): AvailabilityWindow[] | null { if (!Array.isArray(input) || input.length > 7 * MINUTES_PER_DAY) return null const windows: AvailabilityWindow[] = [] for (const value of input) { if (!value || typeof value !== 'object') return null const { dayOfWeek, opensAt, closesAt } = value as Record if (typeof dayOfWeek !== 'number' || !Number.isInteger(dayOfWeek) || dayOfWeek < 0 || dayOfWeek > 6 || typeof opensAt !== 'number' || !Number.isInteger(opensAt) || opensAt < 0 || opensAt >= MINUTES_PER_DAY || typeof closesAt !== 'number' || !Number.isInteger(closesAt) || closesAt < 0 || closesAt > opensAt + MINUTES_PER_DAY || opensAt === closesAt) { return null } windows.push({ dayOfWeek, opensAt, closesAt: overnightClose(opensAt, closesAt) }) } return windows.sort((left, right) => left.dayOfWeek - right.dayOfWeek || left.opensAt - right.opensAt || left.closesAt - right.closesAt) } /** Decode a JSON-encoded availability window list, returning an empty list on invalid data. */ export function decodeAvailabilityWindows(value: unknown): AvailabilityWindow[] { if (typeof value !== 'string') return [] try { return parseAvailabilityWindows(JSON.parse(value)) ?? [] } catch { return [] } } /** Whether any window in the schedule is active at the given local clock. */ export function isAvailableAt(windows: AvailabilityWindow[], clock: AvailabilityClock): boolean { const intervals: OpeningInterval[] = windows.map(window => ({ ...window, isClosed: false })) return openMinutesRemaining(intervals, clock) > 0 } /** * Build an item and category availability lookup from schedule rows at a given clock. * Untimed items and categories default to available. */ export function evaluateCatalogAvailability( rows: AvailabilityScheduleRow[], clock: AvailabilityClock, ): CatalogAvailabilityIndex { const items = new Map() const sections = new Map() for (const row of rows) { const windows = typeof row.windows === 'string' ? decodeAvailabilityWindows(row.windows) : (parseAvailabilityWindows(row.windows) ?? []) const available = isAvailableAt(windows, clock) if (row.productId != null) items.set(Number(row.productId), available) if (row.categoryId != null) sections.set(Number(row.categoryId), available) } return { available: (productId: number, categoryId: number) => items.get(productId) !== false && sections.get(categoryId) !== false, timed: (productId: number, categoryId: number) => items.has(productId) || sections.has(categoryId), } }