#import "DxaReactNative.h"

#ifdef RCT_NEW_ARCH_ENABLED
#import "RNDxaReactNativeSpec.h"
#endif

/**
 * Interface for the Swift implementation class.
 */
@interface DxaReactNativeImpl : NSObject

// Synchronous methods
- (NSArray *)setLoggingTags:(NSArray *)tags;
- (NSDictionary *)initializeProject:(NSString *)brandId
                      projectId:(NSString *)projectId
                      sdkVersion:(NSString *)sdkVersion
                      reactNativeVersion:(NSString *)reactNativeVersion;
- (NSDictionary *)logError:(NSString *)message;
- (NSDictionary *)reportScreenChangeOnNative:(NSDictionary *)screenChangePayload;

// Asynchronous methods
- (void)startRecording:(NSDictionary *)config
                      :(RCTPromiseResolveBlock)resolve
                      :(RCTPromiseRejectBlock)reject;
- (void)stopRecording:(RCTPromiseResolveBlock)resolve
                      :(RCTPromiseRejectBlock)reject;
- (void)resumeRecording:(RCTPromiseResolveBlock)resolve
                       :(RCTPromiseRejectBlock)reject;
- (void)pauseRecording:(RCTPromiseResolveBlock)resolve
                      :(RCTPromiseRejectBlock)reject;

@end

@implementation DxaReactNative {
  DxaReactNativeImpl *_swiftImpl;
}

// Register the module with React Native
RCT_EXPORT_MODULE()

+ (BOOL)requiresMainQueueSetup
{
  return YES; // Return YES since we're initializing Swift implementation in init
}

- (instancetype)init {
  if (self = [super init]) {
    _swiftImpl = [DxaReactNativeImpl new];
  }
  return self;
}

#pragma mark - Synchronous Methods

// Note: Synchronous methods block the JS thread until completion
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(initializeProject:(NSString *)brandId
                                      projectId:(NSString *)projectId
                                     sdkVersion:(NSString *)sdkVersion
                             reactNativeVersion:(NSString *)reactNativeVersion)
{
  return [_swiftImpl initializeProject:brandId
                            projectId:projectId
                           sdkVersion:sdkVersion
                   reactNativeVersion:reactNativeVersion];
}

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(setLoggingTags:(NSArray *)tags)
{
  return [_swiftImpl setLoggingTags:tags];
}

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(logError:(NSString *)message)
{
  return [_swiftImpl logError:message];
}

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(reportScreenChangeOnNative:(NSDictionary *)screenChangePayload)
{
    [_swiftImpl reportScreenChangeOnNative:screenChangePayload];
    return nil;
}

#pragma mark - Asynchronous Methods

RCT_REMAP_METHOD(startRecording,
                 startRecordingWithConfig:(NSDictionary *)config
                     withResolver:(RCTPromiseResolveBlock)resolve
                     withRejecter:(RCTPromiseRejectBlock)reject)
{
    [_swiftImpl startRecording:config:resolve:reject];
}

RCT_REMAP_METHOD(stopRecording,
                 stopRecordingWithResolver:(RCTPromiseResolveBlock)resolve
                     withRejecter:(RCTPromiseRejectBlock)reject)
{
    [_swiftImpl stopRecording:resolve:reject];
}

RCT_REMAP_METHOD(resumeRecording,
                 resumeRecordingWithResolver:(RCTPromiseResolveBlock)resolve
                     withRejecter:(RCTPromiseRejectBlock)reject)
{
  return [_swiftImpl resumeRecording:resolve:reject];
}

RCT_REMAP_METHOD(pauseRecording,
                 pauseRecordingWithResolver:(RCTPromiseResolveBlock)resolve
                     withRejecter:(RCTPromiseRejectBlock)reject)
{
  return [_swiftImpl pauseRecording:resolve:reject];
}

#pragma mark - New Architecture Support

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

@end
