import { ConsentProvider } from '@beslist-nl/beslist-tracking-sdk'; import { AbstractConsentHandler, ConsentStatusData } from './abstract.consent-handler'; /** * Adapts the plugin's AbstractConsentHandler to the SDK's * ConsentProvider interface so it can be passed into BeslistTracker options. */ export class ConsentProviderAdapter implements ConsentProvider { private granted: boolean = false; private listeners: Set<(granted: boolean) => void> = new Set(); constructor( consentHandler: AbstractConsentHandler, private readonly requiredConsentTypes: string[], ) { consentHandler.onConsentChange((consentData) => { const wasGranted = this.granted; this.granted = this.isConsentOfTypesGranted(consentData.consent); if (this.granted !== wasGranted) { this.listeners.forEach((callback) => callback(this.granted)); } }); } isGranted(): boolean { return this.granted; } onConsentChange(callback: (granted: boolean) => void): () => void { this.listeners.add(callback); return () => { this.listeners.delete(callback); }; } private isConsentOfTypesGranted(consentStatusData: ConsentStatusData): boolean { return this.requiredConsentTypes.every((consentType) => { const status = consentStatusData[consentType as keyof ConsentStatusData]; return status === 'granted'; }); } }