import { Subject } from '@wirewheel/cmp/src/entities/Subject' export abstract class PersistentStore { static canSetCookies(): boolean { try { // Set the test cookie with a value of "test" document.cookie = 'test=test;SameSite=None;Secure' // Check if the cookie was successfully set by reading the cookies string const enabled = document.cookie.indexOf('test=test') >= 0 // Deleting cookie document.cookie = 'test=test; expires=Thu, 01 Jan 1970 00:00:00 UTC; SameSite=None; Secure;' return enabled } catch (cause) { console.warn(cause) // Fallback if it cannot write a cookie return false } } protected abstract write(value: any): void protected abstract read(): any private parentDomain: string | undefined = undefined protected constructor() {} getParentDomain(): string | undefined { return this.parentDomain } setParentDomain(newParentDomain: string) { this.parentDomain = newParentDomain } getOrCreateSubject(): Subject { const subject = this.getSubject() || Subject.anonymous() this.setSubject(subject) return subject } setSubject(subject: Subject): void { this.write({ subject }) } getSubject(): Subject | undefined { const store = this.read() if (!store.subject) return const { verifiedId, anonymousId } = store.subject return verifiedId ? Subject.verified(verifiedId) : Subject.anonymous(anonymousId) } getGPCConfirmationBySubjectId(subjectId: string) { const gpcConfirmation = this.read().privacyProtocols return gpcConfirmation && gpcConfirmation[subjectId] } setGPCConfirmationBySubjectId(subjectId: string, data: any) { const privacyProtocols: any = this.read().privacyProtocols || {} privacyProtocols[subjectId] = data this.write({ privacyProtocols }) } }