#import "RTKScreensharePickerView.h"
#import "RTKLogger.h"
#import <React/RCTLog.h>

@implementation RTKScreensharePickerView

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(showScreenSharePickerView)
{
    [RTKLogger debug:@"RTKScreensharePickerView: showScreenSharePickerView() called"];
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *screenShareExtensionId = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"RTCAppScreenSharingExtension"];
        [RTKLogger debug:@"RTKScreensharePickerView: Using extension ID: %@", screenShareExtensionId];
        
        RPSystemBroadcastPickerView *view = [[RPSystemBroadcastPickerView alloc] init];
        view.preferredExtension = screenShareExtensionId;
        view.showsMicrophoneButton = NO;
        
        SEL selector = NSSelectorFromString(@"buttonPressed:");
        if ([view respondsToSelector:selector]) {
            [RTKLogger debug:@"RTKScreensharePickerView: Triggering broadcast picker button"];
            #pragma clang diagnostic push
            #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
            [view performSelector:selector withObject:nil];
            #pragma clang diagnostic pop
        } else {
            [RTKLogger error:@"RTKScreensharePickerView: Broadcast picker view does not respond to buttonPressed: selector"];
        }
    });
}

@end
