// ContentsquareInterfaceWrapper.m
#import <Foundation/Foundation.h>
#import <React/RCTUIManager.h>
#import <React/RCTBridgeModule.h>
#import "ContentsquareInterfaceWrapper.h"

#if __has_include("ContentsquareBridgeModule-Swift.h")
#import "ContentsquareBridgeModule-Swift.h"
#elif __has_include("ContentsquareBridgeModule/ContentsquareBridgeModule-Swift.h")
#import "ContentsquareBridgeModule/ContentsquareBridgeModule-Swift.h"
#endif

@implementation ContentsquareInterfaceWrapper {
    __strong CSQInterfaceInitialization *sdkInterfaceInitialization;
    __strong CSQInterfaceCSInApp *sdkInterfaceCSInApp;
    __strong CSQInterfacePrivacy *sdkInterfacePrivacy;
    __strong CSQInterfacePropertyTracking *sdkInterfacePropertyTracking;
    __strong CSQInterfaceProductAnalytics *sdkInterfaceProductAnalytics;
    __strong CSQInterfaceEventTracking *sdkInterfaceEventTracking;
    __strong CSQInterfaceMasking *sdkInterfaceMasking;
    __strong CSQInterfaceWebView *sdkInterfaceWebView;
    __strong CSQInterfaceErrorTracking *sdkInterfaceErrorTracking;
    __strong CSQInterfaceMetadata *sdkInterfaceMetadata;

    __strong ContentsquareSDKInterfaceLegacy *sdkInterfaceLegacy;
    BOOL csqEnabled;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        sdkInterfaceInitialization = [[CSQInterfaceInitialization alloc] init];
        sdkInterfaceCSInApp = [[CSQInterfaceCSInApp alloc] init];
        sdkInterfaceProductAnalytics = [[CSQInterfaceProductAnalytics alloc] init];
        sdkInterfacePrivacy = [[CSQInterfacePrivacy alloc] init];
        sdkInterfacePropertyTracking = [[CSQInterfacePropertyTracking alloc] init];
        sdkInterfaceEventTracking = [[CSQInterfaceEventTracking alloc] init];
        sdkInterfaceMasking = [[CSQInterfaceMasking alloc] init];
        sdkInterfaceLegacy = [[ContentsquareSDKInterfaceLegacy alloc] init];
        sdkInterfaceWebView = [[CSQInterfaceWebView alloc] init];
        sdkInterfaceMetadata = [[CSQInterfaceMetadata alloc] init];
        sdkInterfaceErrorTracking = [[CSQInterfaceErrorTracking alloc] init];
    }
    return self;
}

- (void)resumeTracking {
    [sdkInterfaceInitialization resumeTracking];
}

- (void)stopTracking {
    [sdkInterfaceLegacy stopTracking];
}

- (void)start {
    [sdkInterfaceInitialization start];
}

- (void)optIn {
    [sdkInterfacePrivacy optIn];
}

- (void)optOut {
    [sdkInterfacePrivacy optOut];
}

- (void)handleWithUrl:(NSURL *)url {
    [sdkInterfaceCSInApp handleWithUrl:url];
}

- (void)sendWithScreenViewWithName:(NSString *)name cvars:(NSArray *)cvars {
    [sdkInterfaceLegacy sendWithScreenViewWithName:name cvars:cvars];
}

- (void)trackScreenViewWithName:(NSString *)name cvars:(NSArray *)cvars sourceInfo:(NSDictionary* _Nullable)sourceInfo {
    [sdkInterfaceEventTracking trackScreenViewWithName:name cvars:cvars sourceInfo:sourceInfo];
}

- (void)trackEventWithName:(NSString *)name properties:(NSDictionary*)properties sourceInfo:(NSDictionary* _Nullable)sourceInfo {
    [sdkInterfaceEventTracking trackEventWithName:name properties:properties sourceInfo:sourceInfo];
}

- (void)pauseTracking {
    [sdkInterfaceInitialization pauseTracking];
}

- (void)setOnMetadataChange {
    [sdkInterfaceMetadata setOnMetadataChange];
}

- (void)stop {
    [sdkInterfaceInitialization stop];
}

- (void)addDynamicStringVar:(NSString *)key
                      value:(NSString *)value {
    [sdkInterfacePropertyTracking addDynamicStringVar:key value:value];
}

- (void)addDynamicIntVar:(NSString *)key
                   value:(NSInteger)value {
    [sdkInterfacePropertyTracking addDynamicIntVar:key value:value];
}

- (void)configureProductAnalyticsWithEnvironmentID:(NSString *)environmentID additionalOptions:(NSDictionary *)productAnalyticsOptions {
    [sdkInterfaceProductAnalytics configureProductAnalyticsWithEnvironmentID:environmentID additionalOptions:productAnalyticsOptions];
}

- (void)getUserId:(RCTResponseSenderBlock)callback {
    NSString *userID = sdkInterfaceLegacy.userID;
    if (userID) {
        callback(@[userID]);
    } else {
        callback(nil);
    }
}

- (void)sendTransactionWithIntCurrency:(NSString *)identifier
                                 value:(float)value
                              currency:(NSInteger)currency {
    [sdkInterfaceLegacy sendWithTransactionId:identifier
                                        value:value
                                     currency:currency];
}

- (void)initComponentsWithParams:(NSDictionary *)params
{
    [sdkInterfaceLegacy initComponentsWithParams:params];
}

- (void)sendTransactionWithStringCurrency:(NSString *)identifier
                                    value:(float)value
                                 currency:(NSString*)currency {
    [sdkInterfaceLegacy sendWithTransactionId:identifier
                                        value:value
                               stringCurrency:currency];
}

- (void)trackTransactionWithIntCurrency:(NSString *)identifier
                                  value:(float)value
                               currency:(NSInteger)currency {
    [sdkInterfaceEventTracking trackTransactionWithTransactionId:identifier
                                                           value:value
                                                        currency:currency];
}

- (void)trackTransactionWithStringCurrency:(NSString *)identifier
                                     value:(float)value
                                  currency:(NSString*)currency {
    [sdkInterfaceEventTracking trackTransactionWithTransactionId:identifier
                                                           value:value
                                                  stringCurrency:currency];
}

- (void)sendDynamicStringVar:(NSString *)key
                       value:(NSString *)value {
    [sdkInterfaceLegacy sendWithDynamicVar:key stringValue:value];
}

- (void)sendDynamicIntVar:(NSString *)key
                    value:(NSUInteger)value {
    [sdkInterfaceLegacy sendWithDynamicVar:key intValue:(UInt32) value];
}

- (void)setDefaultMaskingWithIsMasking:(BOOL)isMasking {
    [sdkInterfaceMasking setDefaultMasking:isMasking];
}

- (void)sendUserIdentifier:(NSString *)userIdentifier {
    [sdkInterfacePrivacy sendUserIdentifier:userIdentifier];
}

- (void)identify:(NSString *)identity {
    [sdkInterfacePrivacy identify:identity];
}

- (void)resetIdentity {
    [sdkInterfacePrivacy resetIdentity];
}

- (void)setOnSessionReplayLinkChange {
    [sdkInterfaceLegacy setOnSessionReplayLinkChange];
}

- (void)monitorWarn:(NSDictionary *)params {
    [sdkInterfaceLegacy monitorWarn:params];
}

- (void)monitorError:(NSDictionary *)params {
    [sdkInterfaceLegacy monitorError:params];
}

- (void)injectWebView:(NSNumber *)webViewTag
in:(RCTUIManager *)uimanager {
    [sdkInterfaceLegacy injectWebViewWithTag:webViewTag in:uimanager];
}

- (void)removeInjectionInWebView:(NSNumber *)webViewTag
in:(RCTUIManager *)uimanager {
    [sdkInterfaceLegacy removeInjectionInWebViewWithTag:webViewTag in:uimanager];
}

- (void)registerWebView:(NSNumber *)webViewTag
in:(RCTUIManager *)uiManager {
    [sdkInterfaceWebView registerWebviewWithTag:webViewTag in:uiManager];
}

- (void)unregisterWebView:(NSNumber *)webViewTag
                     from:(RCTUIManager *)uiManager {
    [sdkInterfaceWebView unregisterWebviewWithTag:webViewTag from:uiManager];
}

- (void)setURLMaskingPatterns:(NSArray<NSString *> *)patterns {
    [sdkInterfaceErrorTracking setUrlMaskingPatterns:patterns];
}

- (void)triggerNativeCrash {
    [sdkInterfaceErrorTracking triggerNativeCrash];
}

- (void)addEventProperties:(NSDictionary *)properties {
    [sdkInterfacePropertyTracking addEventProperties:properties];
}

- (void)removeEventProperty:(NSString *)key {
    [sdkInterfacePropertyTracking removeEventProperty:key];
}

- (void)clearEventProperties {
    [sdkInterfacePropertyTracking clearEventProperties];
}

- (void)addUserProperties:(NSDictionary *)properties {
    [sdkInterfacePropertyTracking addUserProperties:properties];
}

@end
