#import "NetworkInterceptorEmitter.h"

static RCTBridge *globalBridge = nil;
static NSString * const kNetworkInterceptorEvent = @"NetworkInterceptorEvent";

@implementation NetworkInterceptorEmitter

RCT_EXPORT_MODULE();

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

+ (BOOL)requiresMainQueueSetup {
  return YES;
}

#pragma mark - Observing Methods

- (void)startObserving {
  globalBridge = self.bridge;
}

- (void)emitNetworkEvent:(NSDictionary *)payload {
  [self sendEventWithName:kNetworkInterceptorEvent body:payload];
}

#pragma mark - Class Methods for Event Emission

+ (void)emitEventToJS:(NSDictionary *)payload {
  if (!globalBridge) return;
  
  NetworkInterceptorEmitter *emitter = [globalBridge moduleForClass:[NetworkInterceptorEmitter class]];
  if (!emitter) return;
  
  [emitter emitNetworkEvent:payload];
}

@end
