#import "HeapReactNativeBridgeModule.h"
#import "RNHeapModuleWrapper.h"

@implementation HeapReactNativeBridgeModule {
    BOOL hasListeners;
}
__strong RNHeapModuleWrapper *interface;

- (instancetype)init {
    self = [super init];
    if (self) {
        interface = [[RNHeapModuleWrapper alloc] init];
        interface.delegate = self;
    }
    return self;
}

RCT_EXPORT_MODULE(HeapReactNativeBridgeModule)

RCT_EXPORT_METHOD(handleInvocation:(nonnull NSString *)method
                  args:(NSDictionary<NSString *, id> *)args
                  resolve:(RCTPromiseResolveBlock)resolve
                  reject:(RCTPromiseRejectBlock)reject)
{
    [interface handleInvocation:method args:args resolve:resolve reject:reject];
}

RCT_EXPORT_METHOD(handleResult:(NSString *)callbackId
                  data:(id)data
                  error:(NSString *)error)
{
    [interface handleResult:callbackId data:data error:error];
}

// List of events this module can emit
- (NSArray<NSString *> *)supportedEvents {
    return @[@"Invocation"];
}

- (void)startObserving {
    hasListeners = YES;
}

- (void)stopObserving {
    hasListeners = NO;
}

// Legacy event emitting method
- (void)sendEvent:(NSString *)eventName value:(NSString *)value {
    if (hasListeners) {
        [self sendEventWithName:eventName body:@{@"value": value}];
    }
}

- (void)emitEvent:(NSString *)name data:(NSDictionary *)data {
     if (hasListeners) {
        [self sendEventWithName:name body:data];
    }
}

@end
