export const LOCAL_DRAFT_SCHEMA_VERSION = 1; export interface LocalDraftEnvelope { schemaVersion: typeof LOCAL_DRAFT_SCHEMA_VERSION; serverVersion: number; updatedAt: string; draft: Draft; } export function adminDraftKey(guideId: string, section: string): string { return `buzz-sheet:admin-draft:${guideId}:${section}`; } export function serializeLocalDraft(draft: Draft, serverVersion: number): string { return JSON.stringify({ schemaVersion: LOCAL_DRAFT_SCHEMA_VERSION, serverVersion, updatedAt: new Date().toISOString(), draft, } satisfies LocalDraftEnvelope); } export function parseLocalDraft(serialized: string, parseDraft: (value: unknown) => Draft | null): LocalDraftEnvelope | null { try { const value: unknown = JSON.parse(serialized); if (!value || typeof value !== "object") return null; const envelope = value as Record; if (envelope.schemaVersion !== LOCAL_DRAFT_SCHEMA_VERSION || typeof envelope.serverVersion !== "number" || typeof envelope.updatedAt !== "string") return null; const draft = parseDraft(envelope.draft); return draft === null ? null : { schemaVersion: LOCAL_DRAFT_SCHEMA_VERSION, serverVersion: envelope.serverVersion, updatedAt: envelope.updatedAt, draft, }; } catch { return null; } }