/** * Birthday helpers for the loyalty birthday gift. * * Brainerce stores a birthday as a MONTH and a DAY and never a year, so there * is no age on file and nothing here needs a date library or a date picker. * The platform mints a one-time coupon and emails it ahead of the day, which * only works if the storefront actually collects the two values. * * Both `updateMyProfile()` and `registerCustomer()` reject a month sent without * a day (and the reverse) with HTTP 400, and reject a day the month does not * have, so every form that collects a birthday validates with these helpers * before it submits. */ /** * Translation keys for the month names, index 0 = January. They live in the * `common` namespace so the account form and the signup form share one list * instead of each carrying its own twelve keys. */ export const BIRTH_MONTH_KEYS = [ 'monthJanuary', 'monthFebruary', 'monthMarch', 'monthApril', 'monthMay', 'monthJune', 'monthJuly', 'monthAugust', 'monthSeptember', 'monthOctober', 'monthNovember', 'monthDecember', ] as const; /** * How many days each month offers. * * February gets 29, not 28: the 29th IS a valid birthday and the platform * celebrates it on 28 February in years that do not have one. No year is ever * stored, so there is no leap-year arithmetic to do here. */ const DAYS_IN_BIRTH_MONTH = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; /** Highest valid day for a birthday month (1-12). */ export function daysInBirthMonth(month: number): number { return DAYS_IN_BIRTH_MONTH[month - 1] ?? 31; } /** * Day numbers to render in the day grid. With no month chosen yet the full * 1-31 range is offered; picking a month narrows it right away. */ export function birthDayOptions(month: number | null): number[] { const count = month ? daysInBirthMonth(month) : 31; return Array.from({ length: count }, (_, index) => index + 1); } /** * Translation key for a stored month, or null when the value is outside 1-12. * Guards the display path so a bad value renders nothing rather than a raw * `common.` key path. */ export function birthMonthKey(month: number): string | null { return BIRTH_MONTH_KEYS[month - 1] ?? null; } /** * Read a stored form value into the number the API wants, or null when no * birthday is set. */ export function toBirthdayNumber(value: string): number | null { const parsed = Number(value); return Number.isInteger(parsed) && parsed > 0 ? parsed : null; }