/** * The holiday/off-peak calendar, assembled from three layers: * * 1. static plugin config (`holidays` / `makeupWorkdays`) — highest priority * 2. the cached result of an optional remote fetch * 3. the seed file shipped inside the package * * The remote source is **off by default**: it is a second outbound origin, and * that is a trust boundary the user should open deliberately. Two adapters cover * the published JSON shapes, both verified against live data. * * The load-bearing rule here is that an **empty result means "this year is * unknown", never "this year has no holidays"**: both known providers answer an * unpublished year with HTTP 200 and an empty set, so recording that as "no * holidays" would silently price every weekday of that year at peak rates and * never retry. Absence is therefore detected from content, not from the status * code, and never written to the cache. * * @module dsh-balance/holidays */ import { type CalendarLookup, type CalendarYear } from './schedule.js'; /** Supported upstream JSON shapes. */ export type HolidayFormat = 'auto' | 'timor' | 'holiday-cn'; /** Remote calendar source configuration. */ export interface HolidaySourceConfig { readonly enabled: boolean; /** URL template containing `{year}`. */ readonly url: string; readonly format: HolidayFormat; readonly timeoutMs: number; } /** One year's dates, non-empty by construction (an empty year is reported as absent). */ export interface ParsedYear { readonly offDays: readonly string[]; readonly makeupWorkdays: readonly string[]; } /** Injectable transport so tests never touch the network. */ export interface HolidayFetchDeps { readonly fetchImpl: (input: string, init?: RequestInit) => Promise; readonly signal: AbortSignal; /** Response size cap; defaults to 256 KiB. */ readonly maxBytes?: number; } /** Whether a string is a real calendar date. */ export declare function isCalendarDate(value: unknown): value is string; /** * Parse the `timor.tech` shape: `{ code, holiday: { "MM-DD": { holiday, date } } }`. * @param payload - the parsed body. * @returns the year's sets, or null when the container shape is unrecognized. * @throws when an entry is malformed, which indicates an upstream schema change. */ export declare function parseTimor(payload: unknown): ParsedYear | null; /** * Parse the `holiday-cn` shape: `{ year, papers, days: [{ name, date, isOffDay }] }`. * @param payload - the parsed body. * @returns the year's sets, or null when the container shape is unrecognized. * @throws when an entry is malformed, which indicates an upstream schema change. */ export declare function parseHolidayCn(payload: unknown): ParsedYear | null; /** * Parse a remote payload under an explicit or sniffed format. * @param payload - the parsed JSON body. * @param format - the configured format, or `auto` to sniff both known shapes. * @returns the year's sets, or null when the year is unpublished (an empty set). * @throws when no adapter recognizes the payload; the message names the received * top-level keys so a schema change is diagnosable from the log alone. */ export declare function parseHolidayPayload(payload: unknown, format: HolidayFormat): ParsedYear | null; /** Replace the `{year}` placeholder in a configured template. */ export declare function urlForYear(template: string, year: number): string; /** * Validate a configured holiday URL template at activation time. * @param template - the configured template. * @param allowLoopbackHttp - whether a loopback `http` endpoint is acceptable. * @throws when the template is missing `{year}`, is not https, or carries credentials. */ export declare function validateHolidayTemplate(template: string, allowLoopbackHttp: boolean): void; /** * Fetch and parse one year. * * @param deps - transport, lifetime signal and size cap. * @param config - the source configuration. * @param year - the year to request. * @returns the year's sets, or **null when the source has no data for that year**. * @throws on transport failure, a non-2xx status, invalid JSON, or an unrecognized shape. */ export declare function fetchHolidayYear(deps: HolidayFetchDeps, config: HolidaySourceConfig, year: number): Promise; /** The layers a calendar is merged from, lowest priority first. */ export interface CalendarLayers { readonly seed?: Readonly>; readonly remote?: Readonly>; /** Flat static lists from plugin config. */ readonly config?: { readonly offDays: readonly string[]; readonly makeupWorkdays: readonly string[]; }; } /** Merged calendar plus the highest layer that contributed. */ export interface MergedCalendar { readonly years: Readonly>; readonly origin: 'seed' | 'remote' | 'config'; readonly lookup: CalendarLookup; } /** Group flat static date lists into per-year entries. */ export declare function groupByYear(offDays: readonly string[], makeupWorkdays: readonly string[]): Record; /** * Merge the three layers into one per-date calendar. * * A date is classified by the highest-priority layer that mentions it, so a static * entry can move a date between "holiday" and "make-up workday" rather than only * adding to it. Years that no layer mentions stay absent, which the schedule * reports as "unknown" rather than "no holidays". * * @param layers - seed, remote cache and static config. * @returns the merged calendar and the origin layer that contributed last. */ export declare function mergeCalendars(layers: CalendarLayers): MergedCalendar;