import type { EarlyBirdTier, EventOption, OptionVariant, RegistrationOption } from "@/types"; // Effective unit price for a registration option. Uses priceSnapshot when available // (authoritative backend price, variant-aware). Falls back to a deadline-only early-bird // calculation for legacy rows without a snapshot: the cheapest tier still open both at // `referenceTime` (e.g. the last payment) and at `atTime`. export function registrationOptionUnitPrice(opt: RegistrationOption, referenceTime: string | Date | null, atTime: Date): number { if (opt.priceSnapshot !== null && opt.priceSnapshot !== undefined) { return Number(opt.priceSnapshot); } const eo = opt.eventOption; const variantId: string | null = opt.variantId || null; // Use variant price as base if available, otherwise option base const base = (opt.variant?.price !== null && opt.variant?.price !== undefined) ? Number(opt.variant.price) : Number(eo?.price || 0); const allTiers = Array.isArray(eo?.earlyBirdTiers) ? eo.earlyBirdTiers.slice() : []; // Filter: variant options use only their variant-specific tiers; plain options use option-level tiers const tiers = variantId ? allTiers.filter((t) => t.variantId === variantId) : allTiers.filter((t) => !t.variantId); if (tiers.length === 0) return base; const t = atTime ? new Date(atTime) : new Date(); const ref = referenceTime ? new Date(referenceTime) : t; const applicable = tiers .map((x) => ({ ...x, deadlineAt: new Date(x.deadline) })) .filter((x) => (ref < x.deadlineAt) && (t < x.deadlineAt)) .sort((a, b) => a.deadlineAt.getTime() - b.deadlineAt.getTime() || (a.order || 0) - (b.order || 0) || a.price - b.price); if (applicable.length === 0) return base; const price = Number(applicable[0].price); return (price >= 0) ? price : base; } // The earliest-deadline early-bird tier that's still open, or undefined when none are. function currentTier(tiers: EarlyBirdTier[]): EarlyBirdTier | undefined { const now = new Date(); return tiers .map((t) => ({ ...t, deadlineAt: new Date(t.deadline) })) .filter((t) => now < t.deadlineAt) .sort((a, b) => a.deadlineAt.getTime() - b.deadlineAt.getTime() || (a.order || 0) - (b.order || 0) || a.price - b.price)[0]; } // What an event option costs right now: its current early-bird price, else its base price. export function optionUnitNow(opt: EventOption): number { const base = opt.price || 0; const tiers = (Array.isArray(opt.earlyBirdTiers) ? opt.earlyBirdTiers : []).filter((t) => !t.variantId); if (tiers.length === 0) return base; return currentTier(tiers)?.price ?? base; } // Same for a variant: its own tiers if it has any, otherwise the option's. export function variantUnitNow(opt: EventOption, variant: OptionVariant): number { const base = variant.price !== null && variant.price !== undefined ? variant.price : opt.price || 0; const allTiers = Array.isArray(opt.earlyBirdTiers) ? opt.earlyBirdTiers : []; const variantTiers = allTiers.filter((t) => t.variantId === variant.id); const tiers = variantTiers.length > 0 ? variantTiers : allTiers.filter((t) => !t.variantId); if (tiers.length === 0) return base; return currentTier(tiers)?.price ?? base; }