'use client' import { createContext, type ReactNode, use } from 'react' import type { ConsentTexts } from './texts.js' import type { ConsentCookieMap } from './useConsent.js' import { useConsent } from './useConsent.js' type ConsentContextValue = { texts: ConsentTexts } & ReturnType const ConsentContext = createContext(null) type ConsentProviderProps = { children: ReactNode /** Extra cookies per category to delete when consent is withdrawn — merged with the built-in list. */ cookieMap?: ConsentCookieMap /** * Consent log endpoint (createConsentLogEndpoint, e.g. '/api/ipal/consent'): * every decision is POSTed — random id, choices, policy version, no personal * data — so the site can demonstrate consent (GDPR Art. 7(1)). Off when omitted. */ logEndpoint?: string texts: ConsentTexts } export function ConsentProvider({ children, cookieMap, logEndpoint, texts }: ConsentProviderProps) { const consent = useConsent({ cookieMap, onDecision: logEndpoint ? (record) => { void fetch(logEndpoint, { body: JSON.stringify(record), headers: { 'content-type': 'application/json' }, keepalive: true, method: 'POST', }).catch(() => {}) } : undefined, version: texts.version, }) return {children} } export function useConsentContext(): ConsentContextValue { const ctx = use(ConsentContext) if (!ctx) {throw new Error('useConsentContext must be used within ConsentProvider')} return ctx }