import type { Confirmation, ConfirmationResult } from '@/types/confirmations' /** * Map server-resolved confirmation payload to the shape used by the public form. * Submission handling must use only this — ConfirmationResolver on the backend is the source of truth. */ export function confirmationFromResult(result: ConfirmationResult): Confirmation { return { id: result.id ?? 0, formId: 0, name: '', type: result.type, enabled: true, showForm: result.showForm ?? false, message: result.message ?? '', url: result.url ?? '', page: '', pageUrl: result.pageUrl, isDefault: result.isDefault, } } /** * Returns the first enabled confirmation by list order (position, then id). * Builder/admin fallback only (e.g. Lite single-confirmation edit routing) — not for post-submit UX. */ export function getActiveConfirmation(confirmations: Confirmation[]): Confirmation | null { if (!confirmations?.length) { return null } const sorted = sortConfirmations(confirmations) return sorted.find((c) => c.enabled) ?? null } function sortConfirmations(confirmations: Confirmation[]): Confirmation[] { return [...confirmations].sort((a, b) => { const posA = a.position ?? 0 const posB = b.position ?? 0 if (posA !== posB) { return posA - posB } return (a.id ?? 0) - (b.id ?? 0) }) }