import type { ConsentState } from './categories.js' /* eslint-disable @typescript-eslint/no-explicit-any -- vendor globals (fbq, clarity, ttq) are untyped */ const granted = (on: boolean) => (on ? 'granted' : 'denied') /** * Tells the tools that are already running on this page about a change of consent, so a withdrawal stops them at * once rather than at the next page load. Only tools that have a call for it, and only when they are loaded: * * - Microsoft Clarity (analytics): `clarity('consentv2', …)`; analytics off → `clarity('consent', false)`, which * erases its cookies and stops it. * - Meta Pixel (marketing): `fbq('consent', 'revoke' | 'grant')`. * - TikTok Pixel (marketing): `ttq.revokeConsent()` / `ttq.grantConsent()`. * * Google tags get the Consent Mode update separately (googleConsent.ts). LinkedIn Insight has no such call: it stops * with the next page load. Never throws. */ export function signalVendors(prev: ConsentState, next: ConsentState): void { if (typeof window === 'undefined') {return} const w = window as any try { if (prev.marketing !== next.marketing) { if (typeof w.fbq === 'function') {w.fbq('consent', next.marketing ? 'grant' : 'revoke')} const ttq = w.ttq const call = next.marketing ? ttq?.grantConsent : ttq?.revokeConsent if (typeof call === 'function') {call.call(ttq)} } if (typeof w.clarity === 'function' && (prev.analytics !== next.analytics || prev.marketing !== next.marketing)) { if (next.analytics) {w.clarity('consentv2', clarityConsent(next))} else {w.clarity('consent', false)} } } catch { // a vendor's call must never break the banner } } /** Clarity's consent API v2 for a state (`ad_Storage` follows marketing, `analytics_Storage` analytics). */ export function clarityConsent(state: Pick): { ad_Storage: string; analytics_Storage: string } { return { ad_Storage: granted(state.marketing), analytics_Storage: granted(state.analytics) } }