//
//  RCTLSTileCoordinator.m
//  ReactLightspeedSDK
//
//  Created by marc matta on 10/13/16.
//  Copyright © 2016 com.lightspeed. All rights reserved.
//

#import "RCTLSTileCoordinator.h"
#import <LightspeedSDK/LightspeedSDK-Swift.h>
#import "LSTile+RCTNativeModel.h"
#import "RCTLSUtils.h"

@implementation RCTLSTileCoordinator
RCT_EXPORT_MODULE(LSTileCoordinator);

static LSTileCoordinator *sharedInstance;
+ (LSTileCoordinator *)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [LSTileCoordinator new];
    });

    return sharedInstance;
}

RCT_REMAP_METHOD(getTiles, withResolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSTileCoordinator sharedInstance] getTiles:^(NSArray<LSTile *> * _Nullable tiles, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([RCTLSUtils LSArrayFromNativeModels:tiles]);
        }
    }];
}
@end
