//
//  IntercomEventEmitter.m
//  TrusteeHelpCrunch
//
//  Created by yurdjan on 17.01.2025.
//

#import "TrusteeHelpCrunchEventEmitter.h"
#import <HelpCrunchSDK/HelpCrunch.h>
#import <React/RCTLog.h>


@implementation TrusteeHelpCrunchEventEmitter

RCT_EXPORT_MODULE();

+ (BOOL)requiresMainQueueSetup {
    return NO;
}

- (NSArray<NSString *> *)supportedEvents {
  return @[
      @"HCSURLNotification",
      @"HCSImageURLNotification",
      @"HCSFileURLNotification",
      @"HCSUserStartedChatNotification",
      @"HCSUserClosedChatNotification",
      @"HCSUnreadChatsNotification"
  ];
}


- (void)handleUpdateUnreadCount:(NSNotification *)notification {
    if ([HelpCrunch numberOfUnreadChats]) {
        [self sendEventWithName:HCSUnreadChatsNotification body:@{@"count": @([HelpCrunch numberOfUnreadChats])}];
    }
}


// Will be called when this module's first listener is added.
- (void)startObserving {
    for (NSString *event in [self supportedEvents]) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleNotification:)
                                         name:event
                                         object:nil];
    }
}

// Will be called when this module's last listener is removed, or on dealloc.
- (void)stopObserving {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)handleNotification:(NSNotification *)notification {
    [self sendEventWithName:notification.name body:notification.userInfo];
}


@end
