'use client'; // In-memory draft order — the customer's in-flight commission as they // move between /builder and /checkout. Lives at module scope so the // state survives client-side navigation between routes (both pages // import from this module → same singleton). // // What's stored: every field needed to (re)POST /api/orders, including // the actual photo File objects. We intentionally do NOT serialize the // Files to sessionStorage — base64-encoding multi-megabyte photos is // slow and storage is bounded. Refresh on /checkout therefore loses the // in-memory state; the page bounces back to /builder in that case. // // Builder writes here on every relevant state change (debounced through // React state-flush). /checkout reads on mount. import type { ProductId, Orientation } from './pricing'; export type DraftPet = { /** Stable slot index (0..3). */ slot: number; file: File | null; name: string; }; export type DraftConfig = { styleId: string; productId: ProductId; size: string; /** Portrait (catalog default) or landscape — landscape transposes the * size's aspect for generation, crop, preview, and the Prodigi asset. */ orientation: Orientation; frame: string; matteId: string; /** Edge wrap for the frameless stretched canvas (Image / Mirror / * White / Black). Ignored by framed products. */ wrap: string; bgColorId: string; bgCustom: string; fontId: string; includeName: boolean; commissionName: string | null; /** Free-text note from the customer to the studio artist — captured * alongside a custom style upload ("make her ears perkier", "match * the mood of the reference but lighter", etc.). Empty string when * not provided. Stored in the order config; surfaced in the admin * editor. */ artistNote: string; }; export type Draft = { config: DraftConfig; pets: DraftPet[]; customStyle: File | null; }; let current: Draft | null = null; const subscribers = new Set<() => void>(); export function getDraft(): Draft | null { return current; } export function setDraft(draft: Draft | null): void { current = draft; subscribers.forEach((fn) => fn()); } export function subscribe(fn: () => void): () => void { subscribers.add(fn); return () => subscribers.delete(fn); } /** Build a FormData payload identical to what the old inline submit * used. Centralised so /checkout doesn't duplicate the field naming * rules from the existing /api/orders route. */ export function draftToFormData(draft: Draft, email: string): FormData { const fd = new FormData(); fd.append('email', email.trim()); fd.append('config', JSON.stringify(draft.config)); draft.pets.forEach((pet, i) => { if (pet.file) fd.append(`pet_${i}_file`, pet.file); if (pet.name) fd.append(`pet_${i}_name`, pet.name); }); if (draft.customStyle) fd.append('custom_style_file', draft.customStyle); return fd; } /** How many pets have an uploaded photo. Mirrors the `uploaded` count * the Builder uses to gate Submit. */ export function uploadedCount(draft: Draft | null): number { return draft?.pets.filter((p) => p.file).length ?? 0; }