'use strict' ; import { RNSnowplowTracker } from './native' ; import { logMessages } from './constants' ; import { isScreenSize , isValidSubjectConf } from './configurations' ; import type { ScreenSize , SubjectConfiguration } from './types' ; param namespace { string } param newUid { string | null } returns function setUserId ( namespace , newUid ) { if ( ! ( newUid === null || typeof newUid === 'string' ) ) { return Promise . reject ( new Error ( logMessages . setUserId ) ) ; } return < Promise < void > > Promise . resolve ( RNSnowplowTracker . setUserId ( { tracker : namespace , userId : newUid } ) ) ; } param namespace { string } param newNuid { string | null } returns function setNetworkUserId ( namespace , newNuid ) { if ( ! ( newNuid === null || typeof newNuid === 'string' ) ) { return Promise . reject ( new Error ( logMessages . setNetworkUserId ) ) ; } return < Promise < void > > Promise . resolve ( RNSnowplowTracker . setNetworkUserId ( { tracker : namespace , networkUserId : newNuid } ) ) ; } param namespace { string } param newDuid { string | null } returns function setDomainUserId ( namespace , newDuid ) { if ( ! ( newDuid === null || typeof newDuid === 'string' ) ) { return Promise . reject ( new Error ( logMessages . setDomainUserId ) ) ; } return < Promise < void > > Promise . resolve ( RNSnowplowTracker . setDomainUserId ( { tracker : namespace , domainUserId : newDuid } ) ) ; } param namespace { string } param newIp { string | null } returns function setIpAddress ( namespace , newIp ) { if ( ! ( newIp === null || typeof newIp === 'string' ) ) { return Promise . reject ( new Error ( logMessages . setIpAddress ) ) ; } return < Promise < void > > Promise . resolve ( RNSnowplowTracker . setIpAddress ( { tracker : namespace , ipAddress : newIp } ) ) ; } param namespace { string } param newUagent { string | null } returns function setUseragent ( namespace , newUagent ) { if ( ! ( newUagent === null || typeof newUagent === 'string' ) ) { return Promise . reject ( new Error ( logMessages . setUseragent ) ) ; } return < Promise < void > > Promise . resolve ( RNSnowplowTracker . setUseragent ( { tracker : namespace , useragent : newUagent } ) ) ; } param namespace { string } param newTz { string | null } returns function setTimezone ( namespace , newTz ) { if ( ! ( newTz === null || typeof newTz === 'string' ) ) { return Promise . reject ( new Error ( logMessages . setTimezone ) ) ; } return < Promise < void > > Promise . resolve ( RNSnowplowTracker . setTimezone ( { tracker : namespace , timezone : newTz } ) ) ; } param namespace { string } param newLang { string | null } returns function setLanguage ( namespace , newLang ) { if ( ! ( newLang === null || typeof newLang === 'string' ) ) { return Promise . reject ( new Error ( logMessages . setLanguage ) ) ; } return < Promise < void > > Promise . resolve ( RNSnowplowTracker . setLanguage ( { tracker : namespace , language : newLang } ) ) ; } param namespace { string } param newRes { ScreenSize | null } returns function setScreenResolution ( namespace , newRes ) { if ( ! ( newRes === null || isScreenSize ( newRes ) ) ) { return Promise . reject ( new Error ( logMessages . setScreenResolution ) ) ; } return < Promise < void > > Promise . resolve ( RNSnowplowTracker . setScreenResolution ( { tracker : namespace , screenResolution : newRes } ) ) ; } param namespace { string } param newView { ScreenSize | null } returns function setScreenViewport ( namespace , newView ) { if ( ! ( newView === null || isScreenSize ( newView ) ) ) { return Promise . reject ( new Error ( logMessages . setScreenViewport ) ) ; } return < Promise < void > > Promise . resolve ( RNSnowplowTracker . setScreenViewport ( { tracker : namespace , screenViewport : newView } ) ) ; } param namespace { string } param newColorD { number | null } returns function setColorDepth ( namespace , newColorD ) { if ( ! ( newColorD === null || typeof newColorD === 'number' ) ) { return Promise . reject ( new Error ( logMessages . setColorDepth ) ) ; } return < Promise < void > > Promise . resolve ( RNSnowplowTracker . setColorDepth ( { tracker : namespace , colorDepth : newColorD } ) ) ; } const setterMap = { userId : setUserId , networkUserId : setNetworkUserId , domainUserId : setDomainUserId , ipAddress : setIpAddress , useragent : setUseragent , timezone : setTimezone , language : setLanguage , screenResolution : setScreenResolution , screenViewport : setScreenViewport , colorDepth : setColorDepth } as Record < keyof SubjectConfiguration , < Type > ( namespace : string , x : Type | null ) => Promise < void > > ; param namespace { string } param config { SubjectConfiguration } returns function setSubjectData ( namespace , config ) { if ( ! isValidSubjectConf ( config ) ) { return Promise . reject ( new Error ( `${ logMessages . setSubjectData } ${ logMessages . subject }` ) ) ; } const promises = Object . keys ( config ) . map ( ( k ) => { const fun = setterMap [ k ] ; return fun ? fun ( namespace , config [ k ] ) : undefined ; } ) . filter ( ( f ) => f !== undefined ) ; const safePromises = promises . map ( ( p ) => ( p as Promise < void > ) . then ( ( x ) => Object . assign ( { status : 'fulfilled' , value : x } ) as PromiseFulfilledResult < void > ) . catch ( ( err ) => Object . assign ( { status : 'rejected' , reason : err . message as string } ) as PromiseRejectedResult ) ) ; return < Promise < void > > Promise . all ( safePromises ) . then ( ( outcomes ) => { const anyReasons = outcomes . filter ( ( res ) => res . status === 'rejected' ) as PromiseRejectedResult [ ] ; if ( anyReasons . length > 0 ) { const allReasons = anyReasons . reduce ( ( acc , curr ) => acc + ':' + curr . reason , logMessages . setSubjectData ) ; throw new Error ( allReasons ) ; } return true ; } ) ; } export { setUserId , setNetworkUserId , setDomainUserId , setIpAddress , setUseragent , setTimezone , setLanguage , setScreenResolution , setScreenViewport , setColorDepth , setSubjectData } ;