import { z } from 'zod' import type { Practitioner } from '@faunosteo/core-wasm/types' /** * Prefill of the practitioner form (D-R7: form validation stays TypeScript, the core is * not loaded on the new-report page): the cached value is used only when it still has the * shape of the report's practitioner (name required, the rest optional text). */ const CachedPractitionerSchema: z.ZodType = z.object({ name: z.string().min(1), cabinet: z.string().optional(), contact: z.string().optional(), siret: z.string().optional(), }) const STORAGE_KEY = 'osteo:lastPractitioner' export function readCachedPractitioner(): Practitioner | undefined { if (typeof localStorage === 'undefined') return undefined const raw = localStorage.getItem(STORAGE_KEY) if (!raw) return undefined try { const data = JSON.parse(raw) const parsed = CachedPractitionerSchema.safeParse(data) return parsed.success ? parsed.data : undefined } catch { return undefined } } export function writeCachedPractitioner(practitioner: Practitioner): void { if (typeof localStorage === 'undefined') return localStorage.setItem(STORAGE_KEY, JSON.stringify(practitioner)) } export function clearPractitionerCache(): void { if (typeof localStorage === 'undefined') return localStorage.removeItem(STORAGE_KEY) }