package com.contentsquare.rn.csq import com.contentsquare.rn.ContentsquareModuleImpl import com.contentsquare.rn.csq.masking.CSQMaskedView import com.contentsquare.rn.csq.masking.CSQUnmaskedView import com.contentsquare.rn.csq.utils.CustomVarConverter import com.contentsquare.rn.csq.utils.ProductAnalyticsOptionsBuilder import com.contentsquare.rn.csq.utils.SourceInfoConverter import com.contentsquare.rn.csq.utils.TransactionConverter import com.contentsquare.rn.csq.utils.toMap import com.contentsquare.rn.eventEmitter.CSEventEmitterModuleImpl import com.contentsquare.rn.utils.ReactNativeTypesConverter import com.contentsquare.rn.utils.ReactNativeUiThreadUtil import com.contentsquare.rn.utils.ReactNativeViewFinder import com.contentsquare.rn.webview.WebViewInjector import com.facebook.react.bridge.Callback import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReadableArray import com.facebook.react.bridge.ReadableMap /** * Wrapper class for the CSQ SDK implementation. * This class is included when enableCsqSdk=true. */ class CSQTransitionWrapper( reactContext: ReactApplicationContext, webViewInjector: WebViewInjector, csEventEmitterModuleImpl: CSEventEmitterModuleImpl, reactNativeUiThreadUtil: ReactNativeUiThreadUtil, reactNativeViewFinder: ReactNativeViewFinder ) : TransitionWrapperInterface { private val contentsquareLegacy = ContentsquareModuleImpl( reactContext, webViewInjector, csEventEmitterModuleImpl, reactNativeUiThreadUtil ) private val csqInitialization = CSQInitialization(reactContext, reactNativeUiThreadUtil) private val csqPrivacy = CSQPrivacy(reactContext, reactNativeUiThreadUtil) private val csqTracking = CSQTracking(reactNativeUiThreadUtil) private val csqEventTracking = CSQEventTracking(reactNativeUiThreadUtil) private val csqMasking = CSQMasking(reactNativeUiThreadUtil) private val csqWebView = CSQWebView(reactContext, reactNativeUiThreadUtil, reactNativeViewFinder) private val csqPropertyTracking = CSQPropertyTracking(reactNativeUiThreadUtil) private val csqErrorTracking = CSQErrorTracking(reactNativeUiThreadUtil) private val csqMetadata = CSQMetadata(reactNativeUiThreadUtil, csEventEmitterModuleImpl) init { csqMasking.mask(CSQMaskedView::class.java) csqMasking.unmask(CSQUnmaskedView::class.java) } override fun getName(): String { return ContentsquareModuleImpl.NAME } override fun getConstants(): Map { return ContentsquareModuleImpl.getConstants() } override fun stopTracking() { contentsquareLegacy.stopTracking() } override fun pauseTracking() { csqTracking.pauseTracking() } override fun resumeTracking() { csqTracking.resumeTracking() } override fun start() { // Use the CSQ SDK implementation csqInitialization.start() } override fun stop() { csqTracking.stop() } override fun getUserId(cb: Callback) { contentsquareLegacy.getUserId(cb) } override fun optIn() { csqPrivacy.optIn() } override fun optOut() { csqPrivacy.optOut() } override fun setDefaultMasking(isMasked: Boolean) { csqMasking.setDefaultMasking(isMasked) } override fun initComponents(params: ReadableMap) { csqInitialization.initComponents(params) } override fun send(screenView: String, customVars: ReadableArray?) { contentsquareLegacy.send(screenView, customVars) } override fun sendTransaction(id: String?, value: Float, currency: Int) { contentsquareLegacy.sendTransaction(id, value, currency) } override fun sendTransactionWithStringCurrency(id: String?, value: Float, currency: String) { contentsquareLegacy.sendTransactionWithStringCurrency(id, value, currency) } override fun addDynamicVar(key: String, value: String) { csqTracking.addDynamicVar(key, value) } override fun addDynamicVar(key: String, value: Double) { csqTracking.addDynamicVar(key, value) } override fun sendDynamicStringVar(key: String, value: String) { contentsquareLegacy.sendDynamicStringVar(key, value) } override fun sendDynamicIntVar(key: String, value: Int) { contentsquareLegacy.sendDynamicIntVar(key, value) } override fun sendUserIdentifier(userIdentifier: String) { csqPrivacy.sendUserIdentifier(userIdentifier) } override fun setOnSessionReplayLinkChange() { contentsquareLegacy.setOnSessionReplayLinkChange() } override fun injectWebView(webViewTag: Int) { contentsquareLegacy.injectWebView(webViewTag) } override fun removeWebViewInjection(webViewTag: Int) { contentsquareLegacy.removeWebViewInjection(webViewTag) } override fun monitorWarn(params: ReadableMap) { contentsquareLegacy.monitorWarn(params) } override fun monitorError(params: ReadableMap) { contentsquareLegacy.monitorError(params) } override fun configureProductAnalytics(envId: String, productAnalyticsOptions: ReadableMap?) { val options = ProductAnalyticsOptionsBuilder.convertReadableMapToOptions(productAnalyticsOptions) csqInitialization.configureProductAnalytics(envId, options) } override fun identify(identity: String) { csqPrivacy.identify(identity) } override fun resetIdentity() { csqPrivacy.resetIdentity() } override fun trackScreenView(name: String, customVars: ReadableArray?, sourceInfo: ReadableMap?) { val customVarsList = customVars?.let { CustomVarConverter.convertReadableArrayToLegacyCustomVarList(it) } ?: emptyList() val sourceInfoObj = sourceInfo?.let { SourceInfoConverter.convertReadableMapToSourceInfo(it) } csqEventTracking.trackScreenView(name, customVarsList, sourceInfoObj) } override fun trackTransaction(id: String?, value: Float, currency: Int) { val transaction = TransactionConverter.createTransaction(value, currency, id) csqEventTracking.trackTransaction(transaction) } override fun trackTransaction(id: String?, value: Float, currency: String) { val transaction = TransactionConverter.createTransaction(value, currency, id) csqEventTracking.trackTransaction(transaction) } override fun trackEvent(event: String, properties: ReadableMap?, sourceInfo: ReadableMap?) { val propertiesMap = properties?.toMap() ?: emptyMap() val sourceInfoObj = sourceInfo?.let { SourceInfoConverter.convertReadableMapToSourceInfo(it) } csqEventTracking.trackEvent(event, propertiesMap, sourceInfoObj) } override fun registerWebView(webViewTag: Int) { csqWebView.registerWebView(webViewTag) } override fun unregisterWebView(webViewTag: Int) { csqWebView.unregisterWebView(webViewTag) } override fun addEventProperties(properties: ReadableMap) { val propertiesMap = properties.toMap() csqPropertyTracking.addEventProperties(propertiesMap) } override fun removeEventProperty(name: String) { csqPropertyTracking.removeEventProperty(name) } override fun clearEventProperties() { csqPropertyTracking.clearEventProperties() } override fun addUserProperties(properties: ReadableMap) { val propertiesMap = properties.toMap() csqPropertyTracking.addUserProperties(propertiesMap) } override fun setUrlMaskingPatterns(patterns: ReadableArray) { val patternsList = ReactNativeTypesConverter.readableArrayToStringList(patterns) csqErrorTracking.setUrlMaskingPatterns(patternsList) } override fun triggerNativeCrash() { csqErrorTracking.triggerNativeCrash() } override fun setOnMetadataChange() { csqMetadata.setOnMetadataChange() } }