import { v4 as uuid } from 'uuid' ; import { CIDProviders } from './CIDProviders' ; import { Optional } from 'polar-shared/src/util/ts/Optional' ; import { CIDProvider } from './CIDProvider' ; import { isPresent } from 'polar-shared/src/Preconditions' ; declare var window ; const KEY = 'ga_cid' ; export class CIDs { /**
     * Get a unique CID from localStorage.
     */ public static get ( ) { let cid = this . fetch ( ) ; if ( ! cid ) { cid = this . create ( ) ; } this . set ( cid ) ; return cid ; } private static fetch ( ) { const mainCID = Optional . of ( CIDProviders . getInstance ( ) ) . filter ( current => isPresent ( current ) ) . map ( current => current . get ( ) ) . getOrUndefined ( ) ; const localCID = window . localStorage . getItem ( KEY ) ; return Optional . first ( mainCID , localCID ) . getOrUndefined ( ) ; } private static set ( cid ) { window . localStorage . setItem ( KEY , cid ) ; CIDProviders . setInstance ( new CIDProvider ( cid ) ) ; } private static create ( ) { return uuid ( ) ; } }