#import "RCTElementsPlayer.h"
#import "connatix_player_sdk_react_native-Swift.h"
@import ConnatixPlayerSDKObjc;

@implementation RCTElementsPlayer

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    self.elementsPlayer = [[CNElementsPlayer alloc] initWithFrame:CGRectZero delegate:nil];
    
    CNElementsPlayer * __weak weakSelf = self;
    self.elementsPlayer.onPlayerEvent = ^(CNPlayerEvent * _Nonnull event) {
        NSString* key = [ObjcHelper getEventKey:event];
        if (event.payload) {
            weakSelf.onPlayerEvent(@{key: event.payload});
        } else {
            weakSelf.onPlayerEvent(@{key: [NSNull null]});
        }
    };
    
    self.elementsPlayer.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:self.elementsPlayer];
    
    [NSLayoutConstraint activateConstraints:[NSArray arrayWithObjects:[self.elementsPlayer.topAnchor constraintEqualToAnchor:self.topAnchor], [self.elementsPlayer.bottomAnchor constraintEqualToAnchor:self.bottomAnchor], [self.elementsPlayer.leftAnchor constraintEqualToAnchor:self.leftAnchor], [self.elementsPlayer.rightAnchor constraintEqualToAnchor:self.rightAnchor], nil]];
    return self;
}

@end
