#import <React/RCTUIManager.h>
#import <React/RCTBridgeModule.h>

#import "ErrorAnalysisBridgeModule.h"
#import "ErrorAnalysisInterfaceWrapper.h"

@implementation ErrorAnalysisBridgeModule {
    __strong ErrorAnalysisInterfaceWrapper* errorAnalysisInterfaceWrapper;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        errorAnalysisInterfaceWrapper = [[ErrorAnalysisInterfaceWrapper alloc] init];
    }
    return self;
}

#pragma mark - React Native requirements

@synthesize bridge = _bridge;

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

RCT_EXPORT_MODULE(ErrorAnalysisModule)

/*
 * Used for Bridge init
 * cf https://reactnative.dev/docs/native-modules-ios#exporting-constants
 */
+ (BOOL)requiresMainQueueSetup {
    return YES;
}

#pragma mark - Bridge API

RCT_EXPORT_METHOD(setURLMaskingPatterns:(NSArray<NSString *> *)patterns) {
    [errorAnalysisInterfaceWrapper setURLMaskingPatterns:patterns];
}

RCT_EXPORT_METHOD(reportJavascriptError:(NSDictionary *)error resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
    [errorAnalysisInterfaceWrapper reportJavascriptError:error resolver:resolve rejecter:reject];
}

RCT_EXPORT_METHOD(triggerNativeCrash) {
    [errorAnalysisInterfaceWrapper triggerNativeCrash];
}

#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
    (const facebook::react::ObjCTurboModule::InitParams &)params
{
    return std::make_shared<facebook::react::NativeErrorAnalysisModuleSpecJSI>(params);
}
#endif

@end
