import { AbstractConsentHandler, BeslistConsentType, ConsentStatusData, ConsentTypeMapping } from './abstract.consent-handler'; declare var wp_has_consent: (...args: any[]) => any; declare const window: { wp_consent_type: string; waitfor_consent_hook: any; } export class WPConsentAPIConsentHandler extends AbstractConsentHandler { public static consentHandlerName = 'wp-consent-api'; public static consentTypeMapping: ConsentTypeMapping = { necessary: undefined, functional: 'functional', analytics: 'statistics', performance: undefined, marketing: 'marketing', } public static cookieNameMapping: ConsentTypeMapping = { necessary: undefined, functional: undefined, analytics: undefined, performance: undefined, marketing: undefined, } protected getConsentHandlerName(): string { return WPConsentAPIConsentHandler.consentHandlerName; }; protected getConsentTypeMapping(): ConsentTypeMapping { return WPConsentAPIConsentHandler.consentTypeMapping; } protected getCookieNameMapping(): ConsentTypeMapping { return WPConsentAPIConsentHandler.cookieNameMapping; } constructor() { super(); window.wp_consent_type = 'optin'; } protected getInitialConsent(): ConsentStatusData { return { necessary: 'granted', functional: !window.waitfor_consent_hook && wp_has_consent('functional') ? 'granted' : 'denied', analytics: !window.waitfor_consent_hook && wp_has_consent('statistics') ? 'granted' : 'denied', performance: 'denied', marketing: !window.waitfor_consent_hook && wp_has_consent('marketing') ? 'granted' : 'denied', }; } public initializeConsentUpdateListener(): void { document.addEventListener('wp_listen_for_consent_change', (event: any) => { const consentStatusData: ConsentStatusData = {}; if (event.detail.hasOwnProperty(this.mapConsentType('functional'))) { consentStatusData.functional = event.detail[this.mapConsentType('functional')] === 'allow' ? 'granted' : 'denied'; } if (event.detail.hasOwnProperty(this.mapConsentType('analytics'))) { consentStatusData.analytics = event.detail[this.mapConsentType('analytics')] === 'allow' ? 'granted' : 'denied'; } if (event.detail.hasOwnProperty(this.mapConsentType('marketing'))) { consentStatusData.marketing = event.detail[this.mapConsentType('marketing')] === 'allow' ? 'granted' : 'denied'; } this.updateConsent(consentStatusData); }); } protected parseConsentCookieValue(consentType: BeslistConsentType, cookieValue: string): boolean | undefined { return undefined; } }