//
//  RCTLSAudienceTrackingCoordinator.m
//  ReactLightspeedSDK
//
//  Created by marc matta on 5/18/17.
//  Copyright © 2017 com.lightspeed. All rights reserved.
//

#import "RCTLSAudienceTrackingCoordinator.h"
#import <LightspeedSDK/LightspeedSDK-Swift.h>
#import "LSCookieStatus+RCTNativeModel.h"

@implementation RCTLSAudienceTrackingCoordinator
    RCT_EXPORT_MODULE(LSAudienceTrackingCoordinator);
    
    static LSAudienceTrackingCoordinator *sharedInstance;
+ (LSAudienceTrackingCoordinator *)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [LSAudienceTrackingCoordinator new];
    });
    
    return sharedInstance;
}
    
    RCT_REMAP_METHOD(getCookieStatus, resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
        [[RCTLSAudienceTrackingCoordinator sharedInstance] getCookieStatus:^(LSCookieStatus * _Nullable cookieStatus, NSError * _Nullable error) {
            if (error) {
                rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
            }else {
                resolver([cookieStatus reactNativeRepresentation]);
            }
        }];
    }
    
    RCT_EXPORT_METHOD(setCookieStatus:(BOOL)status resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
        [[RCTLSAudienceTrackingCoordinator sharedInstance] setCookieStatus:status completion:^(BOOL success, NSError * _Nullable error) {
            if (error) {
                rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
            }else {
                resolver(@(success));
            }
        }];
    }

@end
