import { createServer } from 'node:http';
import { pathToFileURL } from 'node:url';
// example:imports
import { parseAPI } from '@parseapi/sdk';
// /example:imports
class InputError extends Error {}
const escape = value => String(value).replace(/[&<>"']/g, char => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[char]));
const text = (value, max = 160) => typeof value === 'string' && value.length > 0 && value.length <= max && !/[\u0000-\u001f\u007f]/u.test(value);
const DAY = 86400000;
function civilDay(value) {
const time = typeof value === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(value) ? Date.parse(`${value}T00:00:00Z`) : NaN;
if (!Number.isFinite(time) || value.startsWith('0000') || new Date(time).toISOString().slice(0, 10) !== value) throw new InputError('Choose a real date.');
return time / DAY;
}
const isoDay = day => new Date(day * DAY).toISOString().slice(0, 10);
function businessClock(now) {
if (!(now instanceof Date) || !Number.isFinite(now.getTime())) throw new Error('Clock unavailable.');
const parts = Object.fromEntries(new Intl.DateTimeFormat('en-CA', {
timeZone: 'America/New_York', year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', hourCycle: 'h23'
}).formatToParts(now).map(part => [part.type, part.value]));
return { date: `${parts.year}-${parts.month}-${parts.day}`, minutes: Number(parts.hour) * 60 + Number(parts.minute) };
}
function readHoliday(holiday, date) {
if (!holiday || holiday.date !== date || !text(holiday.name) || !['public', 'observance'].includes(holiday.type) ||
typeof holiday.substitute !== 'boolean' || !(holiday.regions === null || Array.isArray(holiday.regions) && holiday.regions.length <= 100 && holiday.regions.every(region => text(region, 20)))) throw new Error('Unexpected holiday.');
civilDay(holiday.date);
// This example's policy is US federal only, without state-specific holidays.
if (holiday.type === 'public' && holiday.regions !== null) throw new Error('Calendar scope does not match the policy.');
return holiday;
}
export const POLICY = { country: 'US', zone: 'America/New_York', closedDates: ['2026-12-24'], maxDays: 180 };
const TITLE = 'Choose an appointment date';
const INTRO = 'Weekday appointments, excluding US federal holidays and December 24, 2026. Dates are checked again when you submit.';
const FAILURE = 'The holiday calendar could not be checked. Your date has not been accepted. Try again.';
const FIELDS = ['date'];
export async function checkBookingDate(date, parse, signal, now = new Date()) {
const day = civilDay(date), today = civilDay(businessClock(now).date);
if (day <= today || day > today + POLICY.maxDays) throw new InputError('Choose a date from tomorrow through the next 180 days in New York.');
if ([0, 6].includes(new Date(day * DAY).getUTCDay())) return { accepted: false, date, reason: 'Appointments run Monday through Friday.' };
if (POLICY.closedDates.includes(date)) return { accepted: false, date, reason: 'The business is closed on this date.' };
// example:request
const calendar = await parse.holiday.date('US', date, { signal });
// /example:request
if (!calendar || calendar.country !== 'US' || calendar.date !== date || !Object.hasOwn(calendar, 'holiday')) throw new Error('Unexpected calendar response.');
if (calendar.holiday !== null) readHoliday(calendar.holiday, date);
// example:result
const holiday = calendar.holiday;
if (holiday?.type === 'public') {
return { accepted: false, date, reason: `Closed for ${holiday.name}.` };
}
return {
accepted: true, date,
reason: 'This date meets the business weekday, holiday, and closure policy.'
};
// /example:result
}
function renderForm(fields = {}, now = new Date()) {
const today = civilDay(businessClock(now).date);
// example:form
const form = `