#import "ModuleLoaderModule.h"
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>

/**
 * ModuleLoader iOS Native 模块实现
 * 
 * 注意：iOS 端的 bundle 加载方式与 Android 不同
 * 这里提供一个基础实现，你可能需要根据实际需求调整
 */
@implementation ModuleLoaderModule

RCT_EXPORT_MODULE(ModuleLoader);

// 记录已加载的 bundle
static NSMutableSet<NSString *> *loadedBundles;

+ (void)initialize {
    if (self == [ModuleLoaderModule class]) {
        loadedBundles = [NSMutableSet set];
    }
}

RCT_EXPORT_METHOD(loadBusinessBundle:(NSString *)bundleId
                  bundlePath:(NSString *)bundlePath
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    // 检查是否已加载
    if ([loadedBundles containsObject:bundleId]) {
        resolve(@{@"success": @YES});
        return;
    }
    
    // TODO: 实现 iOS bundle 加载逻辑
    // iOS 端的 bundle 加载方式与 Android 不同
    // 你可能需要使用 RCTBridge 的相关 API 来加载 bundle
    
    NSLog(@"[ModuleLoader] Loading bundle: %@ from %@", bundleId, bundlePath);
    
    // 标记为已加载
    [loadedBundles addObject:bundleId];
    
    resolve(@{@"success": @YES});
}

RCT_EXPORT_METHOD(isBundleLoaded:(NSString *)bundleId
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    resolve(@([loadedBundles containsObject:bundleId]));
}

RCT_EXPORT_METHOD(getLoadedBundles:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    resolve([loadedBundles allObjects]);
}

@end
