"use client"; import React from "react"; import { Users } from "lucide-react"; export type AttendeeOption = { id: string; name: string; isMainTicket?: boolean; variants?: { id: string; name: string }[] | null; }; export type AttendeeEntry = { name: string; email: string; phone: string }; export type AttendeeEntries = Record; export type TicketDelivery = "payer" | "each"; export type AttendeePayload = { eventOptionId: string; variantId?: string | null; name: string; email?: string; phone?: string }; type Slot = { key: string; eventOptionId: string; variantId: string | null; label: string }; /** * One slot per main-ticket place in the basket. `qtyFor(optionId, variantId)` reads the * caller's quantities (each page keys its quantity map differently). */ function slotsFor(options: AttendeeOption[], qtyFor: (optionId: string, variantId: string | null) => number): Slot[] { const slots: Slot[] = []; for (const o of options) { if (!o.isMainTicket) continue; const variants = o.variants || []; const lines = variants.length > 0 ? variants.map(v => ({ variantId: v.id, label: `${o.name} (${v.name})` })) : [{ variantId: null, label: o.name }]; for (const line of lines) { const qty = qtyFor(o.id, line.variantId); for (let i = 0; i < qty; i++) { slots.push({ key: `${o.id}::${line.variantId || ""}#${i}`, eventOptionId: o.id, variantId: line.variantId, label: `${line.label} ${i + 1}` }); } } } return slots; } /** The named places as the API expects them (unnamed places are left out). */ export function attendeesPayload(options: AttendeeOption[], qtyFor: (optionId: string, variantId: string | null) => number, entries: AttendeeEntries): AttendeePayload[] { return slotsFor(options, qtyFor) .map(s => ({ s, e: entries[s.key] })) .filter(({ e }) => e && e.name.trim()) .map(({ s, e }) => ({ eventOptionId: s.eventOptionId, ...(s.variantId ? { variantId: s.variantId } : {}), name: e.name.trim(), ...(e.email.trim() ? { email: e.email.trim() } : {}), ...(e.phone.trim() ? { phone: e.phone.trim() } : {}), })); } /** * "Registering for other people?" — a name (and optional email/phone) for each main * ticket place, plus who gets the tickets. Named places get their own ticket; any left * blank share one ticket as usual. */ export function AttendeeNamesEditor({ options, qtyFor, enabled, onEnabledChange, entries, onEntriesChange, delivery, onDeliveryChange, payerLabel = "me", }: { options: AttendeeOption[]; qtyFor: (optionId: string, variantId: string | null) => number; enabled: boolean; onEnabledChange: (v: boolean) => void; entries: AttendeeEntries; onEntriesChange: (v: AttendeeEntries) => void; delivery: TicketDelivery; onDeliveryChange: (v: TicketDelivery) => void; payerLabel?: string; }) { const slots = slotsFor(options, qtyFor); const set = (key: string, field: keyof AttendeeEntry, value: string) => { const prev = entries[key] || { name: "", email: "", phone: "" }; onEntriesChange({ ...entries, [key]: { ...prev, [field]: value } }); }; const inputCls = "w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-brand-400"; return (
{enabled && (
{slots.length === 0 ? (

Choose how many tickets first, then add a name for each person.

) : ( <>

Each named person gets their own ticket and their own attendee form. Leave a name blank to keep that ticket with the others.

{slots.map(s => { const e = entries[s.key] || { name: "", email: "", phone: "" }; return (
{s.label}
set(s.key, "name", ev.target.value)} aria-label={`${s.label} name`} /> set(s.key, "email", ev.target.value)} aria-label={`${s.label} email`} /> set(s.key, "phone", ev.target.value)} aria-label={`${s.label} phone`} />
); })}
Send the tickets
)}
)}
); }