/** * Product modifier groups, option selection rules, and configured cart line helpers. * * Shared across web storefronts, native Craft apps, and post-order additions so * required/optional min/max rules and modifier pricing stay identical everywhere. * * Pure and runtime-agnostic: safe on server, web storefront, and native Craft. */ export interface ModifierOption { id: number name: string priceDeltaCents: number isDefault?: boolean } export interface ModifierGroup { id: number min: number max: number options: ModifierOption[] } export interface CatalogItem { id: number name: string priceCents: number photo?: string | null photoBlur?: string | null groups?: ModifierGroup[] } export interface CatalogSection { items: TItem[] } export interface ConfiguredLine { productId: number name: string photo?: string | null photoBlur?: string | null unitCents: number quantity: number modifierIds: number[] labels: string[] notes: string } export interface ItemConfiguration { item: TItem quantity: number choices: Record } /** Index every item across catalog sections by its numeric id. */ export function indexCatalogItems( catalog: { sections?: Array> } | null | undefined, ): Map { const index = new Map() for (const section of catalog?.sections ?? []) { for (const item of section.items) index.set(item.id, item) } return index } /** Collect every option marked `isDefault` across an item's modifier groups. */ export function defaultModifierChoices(item: Pick): Record { const chosen: Record = {} for (const group of item.groups ?? []) chosen[group.id] = group.options.filter(option => option.isDefault).map(option => option.id) return chosen } /** * Apply one option toggle to a modifier group's current selection. * Returns null if the toggle violates the group's min/max constraints. */ export function toggleModifierChoice( group: Pick, chosen: number[], optionId: number, ): number[] | null { if (chosen.includes(optionId)) return chosen.length <= group.min ? null : chosen.filter(id => id !== optionId) if (group.max === 1) return [optionId] return chosen.length >= group.max ? null : [...chosen, optionId] } /** Return the first required modifier group that has fewer than `min` choices, or null. */ export function firstUnansweredModifierGroup( item: { groups?: TGroup[] }, choices: Record, ): TGroup | null { return (item.groups ?? []).find(group => (choices[group.id] ?? []).length < group.min) ?? null } /** Build a display-and-checkout cart line from a catalog item and chosen modifier ids. */ export function buildConfiguredLine( item: CatalogItem, quantity: number, choices: Record, note: string, ): ConfiguredLine { const modifierIds: number[] = [] const labels: string[] = [] for (const group of item.groups ?? []) { for (const optionId of choices[group.id] ?? []) { modifierIds.push(optionId) const option = group.options.find(candidate => candidate.id === optionId) if (option) labels.push(option.name) } } return { productId: item.id, name: item.name, photo: item.photo, photoBlur: item.photoBlur, unitCents: item.priceCents, quantity, modifierIds, labels, notes: note, } } /** Compute a cart line's subtotal in cents against the item's current modifier option deltas. */ export function configuredLineCents( item: Pick | null | undefined, line: Pick, ): number { let cents = line.unitCents for (const group of item?.groups ?? []) { for (const optionId of line.modifierIds) { const option = group.options.find(candidate => candidate.id === optionId) if (option) cents += option.priceDeltaCents } } return cents * line.quantity } /** Compute the live total in cents for an item currently being configured. */ export function configuredSelectionCents( config: ItemConfiguration | null | undefined, ): number { if (!config) return 0 let cents = config.item.priceCents for (const group of config.item.groups ?? []) { for (const optionId of config.choices[group.id] ?? []) { const option = group.options.find(candidate => candidate.id === optionId) if (option) cents += option.priceDeltaCents } } return cents * config.quantity } /** Parse a user-typed decimal money amount (supports `.` or `,`) into integer cents, or null if invalid. */ export function parseMoneyTextToCents(text: string): number | null { const amount = String(text).trim().replace(',', '.') if (!/^\d{0,4}(?:\.\d{0,2})?$/.test(amount) || amount === '' || amount === '.') return null return Math.round(Number(amount) * 100) }