"use client"; import { CONSENT_COOKIE, CONSENT_EVENT, CONSENT_MAX_AGE_S, CONSENT_OPEN_EVENT, readConsentCookie, serializeConsent, type Consent, } from "@/lib/consent"; /** Magasin minimal basé sur document.cookie + événements, pour useSyncExternalStore. */ export function subscribe(cb: () => void) { window.addEventListener(CONSENT_EVENT, cb); return () => window.removeEventListener(CONSENT_EVENT, cb); } let lastRaw: string | null = null; let lastParsed: Consent | null | undefined; /** Retourne un snapshot stable : `undefined` = pas de choix enregistré. */ export function getSnapshot(): Consent | undefined { const raw = document.cookie; if (raw !== lastRaw || lastParsed === undefined) { lastRaw = raw; lastParsed = readConsentCookie(raw); } return lastParsed ?? undefined; } export function getServerSnapshot(): Consent | undefined | null { return null; // null = inconnu côté serveur (rien n'est rendu) } export function writeConsent(c: Consent) { const secure = window.location.protocol === "https:" ? "; Secure" : ""; document.cookie = `${CONSENT_COOKIE}=${serializeConsent(c)}; Max-Age=${CONSENT_MAX_AGE_S}; Path=/; SameSite=Lax${secure}`; window.dispatchEvent(new Event(CONSENT_EVENT)); } export function openPreferences() { window.dispatchEvent(new Event(CONSENT_OPEN_EVENT)); }