import { NativeModules } from 'react-native' import { createInteractor } from '../architecture/Interactor' import { check } from '../utility/check' import { log } from '../utility/log' import { checkJSONObject, createJSONObject } from '../utility/json' import { AirbridgeEventType } from '../constant/AirbridgeEventType' export const createDependency = () => {} type EventInteractor = { trackEvent( category: string, semanticAttributes?: Record, customAttributes?: Record, ): void setAdditionalEventAttributes( types: string[], semanticAttributes?: Record, customAttributes?: Record, ): void trackStoreInAppPurchaseEvent( purchase: Record, semanticAttributes?: Record, customAttributes?: Record, ): void } createDependency.EventModule = () => ({ interactor: createInteractor(NativeModules.EventInteractor), }) export type EventModule = ReturnType export const createEventModule = () => { // create dependency const { interactor } = createDependency.EventModule() // define method const isOptionalObject = (value: unknown): boolean => ( check.null(value) || check.undefined(value) || check.object(value) ) const toJSONObject = (name: string, value?: Record) => { if (!check.object(value) || checkJSONObject(value)) { return value } log.nonJSONValue(name) return createJSONObject(value) } const trackEvent = ( category: string, semanticAttributes?: Record, customAttributes?: Record, ): void => { if (!check.string(category)) { log.unmatchedType('category', 'string') return } if (!isOptionalObject(semanticAttributes)) { log.unmatchedType('semanticAttributes', 'object?') return } if (!isOptionalObject(customAttributes)) { log.unmatchedType('customAttributes', 'object?') return } interactor.trackEvent( category, toJSONObject('semanticAttributes', semanticAttributes), toJSONObject('customAttributes', customAttributes), ) } const setAdditionalEventAttributes = ( type: AirbridgeEventType | AirbridgeEventType[], semanticAttributes?: Record, customAttributes?: Record, ): void => { const types = check.array(type) ? type : [type] if (!types.every((type) => check.string(type))) { log.unmatchedType('type', 'string') return } if (!isOptionalObject(semanticAttributes)) { log.unmatchedType('semanticAttributes', 'object?') return } if (!isOptionalObject(customAttributes)) { log.unmatchedType('customAttributes', 'object?') return } interactor.setAdditionalEventAttributes( types, toJSONObject('semanticAttributes', semanticAttributes), toJSONObject('customAttributes', customAttributes), ) } const trackStoreInAppPurchaseEvent = ( purchase: Record, semanticAttributes?: Record, customAttributes?: Record, ): void => { if (!check.object(purchase)) { log.unmatchedType('purchase', 'object') return } if (!isOptionalObject(semanticAttributes)) { log.unmatchedType('semanticAttributes', 'object?') return } if (!isOptionalObject(customAttributes)) { log.unmatchedType('customAttributes', 'object?') return } interactor.trackStoreInAppPurchaseEvent( toJSONObject('purchase', purchase) ?? {}, toJSONObject('semanticAttributes', semanticAttributes), toJSONObject('customAttributes', customAttributes), ) } // create object return { trackEvent, setAdditionalEventAttributes, trackStoreInAppPurchaseEvent, } }