import { HttpClient } from '@wirewheel/cmp/src/HttpClient' import { PrivacyProtocolsConfig } from '@wirewheel/cmp/src/entities/PrivacyProtocolsConfig' import { isBrowser } from '@wirewheel/cmp/src/utils/isBrowser' import { PersistentStore } from '@wirewheel/cmp/src/modules/PersistentStore' import { PersistentStoreFactory } from '@wirewheel/cmp/src/modules/PersistentStoreFactory' export class PrivacyProtocolsClient { private readonly httpClient: HttpClient private readonly attributes: Record private readonly store: PersistentStore constructor(config: PrivacyProtocolsConfig) { this.httpClient = HttpClient.create(config) this.attributes = config.attributes || {} this.store = PersistentStoreFactory.getStore(config.cookieConfig) } async registerPrivacyProtocols(): Promise { if (!isBrowser()) throw new Error('Operation only available for the browser') const subject = this.store.getOrCreateSubject() if (!subject.id || this.store.getGPCConfirmationBySubjectId(subject.id)) return await this.httpClient.post('/consents/gpc', { subject, attributes: this.attributes, }) this.store.setGPCConfirmationBySubjectId(subject.id, [{ gpc: 1 }]) } }