//
//  RCTLightspeedSDK.m
//  ReactLightspeedSDK
//
//  Created by marc matta on 2/15/17.
//  Copyright © 2017 com.lightspeed. All rights reserved.
//

#import "RCTLightspeedSDK.h"
#import <KMFSDK/KMFSDK.h>

/**
 *  The event sent to JS in case of a change in the device id change from KMF SDK
 */
NSString *const ReactLightspeedSDKDeviceIdChanged = @"kmf_device_id_changed";

@implementation ReactLightspeedSDK

RCT_EXPORT_MODULE(LightspeedSDK);

- (dispatch_queue_t)methodQueue
{
    return dispatch_get_main_queue();
}

- (void)startObserving {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleDeviceIdChangeNotification:)
                                                 name:[KMFSDK kKMFDeviceIdRetrievedNotification]
                                               object:nil];
}

- (void)stopObserving
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (NSArray<NSString *> *)supportedEvents {
    return @[ReactLightspeedSDKDeviceIdChanged];
}

- (void)handleDeviceIdChangeNotification:(NSNotification *)notification {
    [self sendEventWithName:ReactLightspeedSDKDeviceIdChanged body:notification.userInfo];
}

RCT_EXPORT_METHOD(deviceID:(RCTResponseSenderBlock)callback) {
    callback(@[[KMFSDK deviceId] ? [KMFSDK deviceId] : [NSNull null]]);
}

@end

