'use client'; import { jsx as _jsx } from "react/jsx-runtime"; import { createContext, useCallback, useContext, useState } from 'react'; import { Turnstile } from './Turnstile.js'; const TurnstileContext = /*#__PURE__*/ createContext(null); /** * Provides the Turnstile site key once for the whole app, like ConsentProvider * for cookies. The project reads the key server-side and passes it here in the * layout; forms then use + useTurnstile() with NO per-form * key wiring. * * // layout.tsx (server) → read key, pass to provider * import { TurnstileProvider } from '@intecion/ipal-kit/client' * const siteKey = await getTurnstileSiteKey() // your server helper * {children} * * When siteKey is null (Turnstile not configured), widgets render nothing and * token stays null — forms should treat "no Turnstile" as allowed in dev. */ export function TurnstileProvider({ siteKey, children }) { const [token, setToken] = useState(null); return /*#__PURE__*/ _jsx(TurnstileContext.Provider, { value: { siteKey, token, setToken }, children: children }); } /** * Hook giving a form the Turnstile token + a widget bound to the provider's key. * No per-form siteKey plumbing — the provider supplies it. * * const { token, TurnstileWidget, reset } = useTurnstile() * // in JSX: * // at submit: submitForm({ ..., turnstileToken: token }) * // after submit: reset() // clear for the next submission */ export function useTurnstile() { const ctx = useContext(TurnstileContext); if (!ctx) { throw new Error('useTurnstile must be used within '); } const { siteKey, token, setToken } = ctx; const reset = useCallback(()=>setToken(null), [ setToken ]); const TurnstileWidget = useCallback((props)=>{ if (!siteKey) return null; return /*#__PURE__*/ _jsx(Turnstile, { siteKey: siteKey, onToken: setToken, theme: props?.theme }); }, [ siteKey, setToken ]); return { token, TurnstileWidget, reset, enabled: Boolean(siteKey) }; } //# sourceMappingURL=TurnstileProvider.js.map