const STORAGE_PREFIX = 'ghabilee:event-create-draft' const CURRENT_VERSION = 1 interface StoredEventDraft { version: number savedAt: string step: number data: T } const storageKey = (userId: string) => `${STORAGE_PREFIX}:${userId}` export function loadEventDraft(userId: string): StoredEventDraft | null { if (typeof window === 'undefined') return null try { const raw = localStorage.getItem(storageKey(userId)) if (!raw) return null const parsed = JSON.parse(raw) as Partial> if (parsed.version !== CURRENT_VERSION || !parsed.data || typeof parsed.data !== 'object') { localStorage.removeItem(storageKey(userId)) return null } return { version: CURRENT_VERSION, savedAt: typeof parsed.savedAt === 'string' ? parsed.savedAt : new Date().toISOString(), step: typeof parsed.step === 'number' && parsed.step >= 1 && parsed.step <= 4 ? parsed.step : 1, data: parsed.data, } } catch { localStorage.removeItem(storageKey(userId)) return null } } export function saveEventDraft(userId: string, data: StoredEventDraft['data'], step: number): void { if (typeof window === 'undefined') return const draft: StoredEventDraft = { version: CURRENT_VERSION, savedAt: new Date().toISOString(), step: Math.min(Math.max(step, 1), 4), data, } try { localStorage.setItem(storageKey(userId), JSON.stringify(draft)) } catch { // Storage may be unavailable or full; the in-memory wizard remains usable. } } export function clearEventDraft(userId: string): void { if (typeof window === 'undefined') return localStorage.removeItem(storageKey(userId)) }