
@import AMapLocationKit;

#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#import "Coordinate.h"

@interface AMapUtil : RCTEventEmitter <RCTBridgeModule>
@end

@implementation AMapUtil

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(calculateLineDistance:(NSArray<Coordinate*> *)origins
                  destinations:(NSArray<Coordinate*> *)destinations
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
    NSMutableArray *results = [[NSMutableArray alloc] init];
    
    @try {
        for(int i = 0; i < origins.count; ++i) {
            if(i == destinations.count) {
                break;
            }
            MAMapPoint origin = MAMapPointForCoordinate(origins[i].coordinate);
            MAMapPoint destination = MAMapPointForCoordinate(destinations[i].coordinate);
            
            CLLocationDistance distance = MAMetersBetweenMapPoints(origin, destination);
            [results addObject:[NSNumber numberWithDouble:distance]];
        }
        resolve(results);
    } @catch (NSException *exception) {
        reject(@"1", exception.reason != nil ? exception.reason : @"", nil);
    }
}
- (NSArray<NSString *> *)supportedEvents {
    return @[];
}
@end
