#import "InstalledAppsAndroid.h"
#import <Foundation/Foundation.h>

@implementation InstalledAppsAndroid
RCT_EXPORT_MODULE()

- (NSNumber *)multiply:(double)a b:(double)b {
    NSNumber *result = @(a * b);

    return result;
}

- (void)getInstalledApps:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject {
    // iOS doesn't allow querying installed apps due to sandboxing restrictions
    // We can only provide information about the current app
    NSMutableArray *result = [[NSMutableArray alloc] init];
    
    // Get current app info
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *appName = [mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"] 
                        ?: [mainBundle objectForInfoDictionaryKey:@"CFBundleName"] 
                        ?: @"Unknown";
    NSString *bundleId = [mainBundle bundleIdentifier] ?: @"Unknown";
    NSString *appVersion = [mainBundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"] ?: @"Unknown";
    
    NSDictionary *appInfo = @{
        @"packageName": bundleId,
        @"appName": appName,
        @"appVersion": appVersion
    };
    
    [result addObject:appInfo];
    
    resolve(result);
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
    (const facebook::react::ObjCTurboModule::InitParams &)params
{
    return std::make_shared<facebook::react::NativeInstalledAppsAndroidSpecJSI>(params);
}

@end
