//
//  RNNotificationHandler.m
//  RNInsider
//
//  Created by Insider on 21.02.2019.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "RNNotificationHandler.h"
#if __has_include(<InsiderMobile/Insider.h>)
#import <InsiderMobile/Insider.h>
#elif __has_include("Insider.h")
#import "Insider.h"
#endif

@implementation RNNotificationHandler

NSString * const NOTIFICATION_OPEN = @"NOTIFICATION_OPEN";
NSString * const INAPP_BUTTON_CLICK = @"INAPP_BUTTON_CLICK";
NSString * const TEMP_STORE_PURCHASE = @"TEMP_STORE_PURCHASE";
NSString * const TEMP_STORE_ADDED_TO_CART = @"TEMP_STORE_ADDED_TO_CART";
NSString * const TEMP_STORE_CUSTOM_ACTION = @"TEMP_STORE_CUSTOM_ACTION";
NSString * const INAPP_SEEN = @"INAPP_SEEN";
NSString * const FOREGROUND_PUSH = @"FOREGROUND_PUSH";
NSString * const INSIDER_ID_LISTENER = @"INSIDER_ID_LISTENER";
NSString * const SESSION_STARTED = @"SESSION_STARTED";
NSString * const INSIDER_EVENT_RECORDED = @"INSIDER_EVENT_RECORDED";

static RNNotificationHandler *notificationHandler;

+ (id)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        notificationHandler = [super allocWithZone:zone];
    });

    return notificationHandler;
}

+ (BOOL)requiresMainQueueSetup {
    return NO;
}

RCT_EXPORT_MODULE();

- (NSArray<NSString *> *)supportedEvents {
    return @[NOTIFICATION_OPEN, INAPP_BUTTON_CLICK, TEMP_STORE_PURCHASE, TEMP_STORE_ADDED_TO_CART, TEMP_STORE_CUSTOM_ACTION, INAPP_SEEN, FOREGROUND_PUSH, INSIDER_ID_LISTENER, SESSION_STARTED, INSIDER_EVENT_RECORDED];
}

- (void)sendDataToJS:(NSString *)eventName data:(NSDictionary *)data {
    @try {
        [self sendEventWithName:eventName body:data];
    } @catch (NSException *e) {
        [Insider sendError:e desc:[NSString stringWithFormat:@"%s:%d", __func__, __LINE__]];
    }
}
@end
