#import "EventEmitter.h"

@implementation CTPEventEmitter
{
  bool hasListeners;
}

RCT_EXPORT_MODULE(CTPEventEmitter);

+ (id)allocWithZone:(NSZone *)zone {
  static CTPEventEmitter *sharedInstance = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sharedInstance = [super allocWithZone:zone];
  });
  return sharedInstance;
}

// Will be called when this module's first listener is added.
-(void)startObserving {
  hasListeners = YES;
  // Set up any upstream listeners or background tasks as necessary
}

// Will be called when this module's last listener is removed, or on dealloc.
-(void)stopObserving {
  hasListeners = NO;
  // Remove upstream listeners, stop unnecessary background tasks
}

-(bool)hasListeners {
  return hasListeners;
}

// Returns list of all event Names this topic can export
-(NSArray<NSString *> *)supportedEvents{
  return @[@"DESTINATION_SELECTED",@"NAVIGATION_PATH_GENERATED",@"USER_POSITION_UPDATED", @"WINDOW_LOADED", @"MAP_TAPPED_GENERIC", @"ODP_READY", @"ODP_KEY_VALIDATION", @"ODP_NAVIGATION_UPDATE", @"ODP_FACILITY_UPDATE",@"GENERATED_SEARCH_DESTINATIONS",@"ERROR",@"VENUE_LOADED",@"GEOFENCES_ENTERED", @"SCREEN_ROTATED",@"GEOFENCES_CHANGED",@"REROUTE_NAVIGATION_PATH_GENERATED"];
}

-(bool)sendEvent:(NSString*) eventName body:(NSObject*) body {
//  [self sendEventName:@"onNavigationUpdate" body:@{@"key": @"value"}];
  
  if (hasListeners) {
    // NSLog(@"CustomEventsEmitter sendEventName emitting event: %@", eventName);
    [self sendEventWithName:eventName body:body];
  } else {
    NSLog(@"Event Emitter sendEventName called without listeners: %@", eventName);
  }
  return TRUE;
}

@end
