import { describePublicFormChanges, PublicFormSnapshot } from './publicFormChanges'; export type UnansweredPublish = { outcome: 'live'; formHandle: string } | { outcome: 'unknown' }; // An HTTP refusal carries a status; a dropped connection does not, and may have published anyway. export const wasUnanswered = (error: unknown): boolean => typeof (error as { status?: unknown } | null | undefined)?.status !== 'number'; /** * Whether a publish that got no answer made this snapshot live. Only a positive match proves it: reads * trail writes and the write may still be landing, so an absent or older snapshot proves nothing. */ export async function resolveUnansweredPublish( questionnaireId: string, sent: PublicFormSnapshot, listForms: () => Promise>, readSnapshot: (formHandle: string) => Promise, ): Promise { try { const form = (await listForms()).find((f) => f.questionnaireId === questionnaireId && f.active); if (!form) return { outcome: 'unknown' }; const live = await readSnapshot(form.formHandle); return describePublicFormChanges(live, sent).length === 0 ? { outcome: 'live', formHandle: form.formHandle } : { outcome: 'unknown' }; } catch { return { outcome: 'unknown' }; } } export const UNANSWERED_PUBLISH_COPY: Record = { live: 'The connection dropped before Akashic confirmed, so we checked: this version is live and visitors see it now. Publish again to finish recording it in WordPress.', unknown: 'The connection dropped before Akashic confirmed, and we could not confirm whether this version went live. Your draft is kept. Reload the page to see what visitors get before trying again.', };