// generated by bun run generate from the SwiftUI SDK and codegen/catalog.ts.
// edit the generator or catalog, then regenerate.
#import "OneNativeMapComponentView.h"
#import <React/RCTView.h>
#import "One-Swift.h"
#import <react/renderer/components/OneNativeSpec/ComponentDescriptors.h>
#import <react/renderer/components/OneNativeSpec/EventEmitters.h>
#import <React/RCTConversions.h>
#import "OneNativeStyleDictionary.h"
using namespace facebook::react;
@implementation OneNativeMapComponentView { OneNativeMapView *_nativeView; BOOL _markersDirty; }
+ (ComponentDescriptorProvider)componentDescriptorProvider { return concreteComponentDescriptorProvider<OneNativeMapComponentDescriptor>(); }
- (NSObject *)accessibilityElement { return _nativeView; }
- (instancetype)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
    _props = std::make_shared<const OneNativeMapProps>();
    _markersDirty = YES;
    _nativeView = [OneNativeMapView new]; self.contentView = _nativeView;
    __weak OneNativeMapComponentView *weakSelf = self;
    _nativeView.onSDKEvent = ^(NSString *name, NSString *value) {
      OneNativeMapComponentView *strongSelf = weakSelf;
      if (!strongSelf || !strongSelf->_eventEmitter) return;
      auto emitter = std::static_pointer_cast<const OneNativeMapEventEmitter>(strongSelf->_eventEmitter);
      emitter->onNativeSDKEvent({.name = std::string(name.UTF8String), .value = std::string(value.UTF8String)});
    };
    _nativeView.onRegionChange = ^(double latitude, double longitude, double distance, NSInteger eventCount) {
      OneNativeMapComponentView *strongSelf = weakSelf;
      if (!strongSelf || !strongSelf->_eventEmitter) return;
      auto emitter = std::static_pointer_cast<const OneNativeMapEventEmitter>(strongSelf->_eventEmitter);
      emitter->onNativeMapRegionChange({.latitude = (double)latitude, .longitude = (double)longitude, .distance = (double)distance, .eventCount = (int)eventCount});
    };
  }
  return self;
}
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps {
  const auto &next = *std::static_pointer_cast<const OneNativeMapProps>(props);
  const auto &previous = *std::static_pointer_cast<const OneNativeMapProps>(_props);
  bool markersChanged = _markersDirty || previous.markers.size() != next.markers.size();
  if (!markersChanged) for (size_t i = 0; i < next.markers.size(); i++) {
    if (previous.markers[i].id != next.markers[i].id || previous.markers[i].label != next.markers[i].label || previous.markers[i].latitude != next.markers[i].latitude || previous.markers[i].longitude != next.markers[i].longitude) { markersChanged = true; break; }
  }
  if (markersChanged) {
    NSMutableArray *markers = [NSMutableArray new];
    for (const auto &item : next.markers) [markers addObject:@{@"id": RCTNSStringFromString(item.id), @"label": RCTNSStringFromString(item.label), @"latitude": @(item.latitude), @"longitude": @(item.longitude)}];
    [_nativeView setMarkers:markers];
    _markersDirty = NO;
  }

  [_nativeView configureAccessibility:RCTNSStringFromString(next.accessibilityLabel)
    hint:RCTNSStringFromString(next.accessibilityHint)
    value:RCTNSStringFromString(next.accessibilityValue.text.value_or(""))
    identifier:RCTNSStringFromString(next.testId)];
  [_nativeView configureStyle:OneNativeStyleDictionary(next.swiftStyle)];
  [_nativeView configure:next.latitude
    longitude:next.longitude distance:next.distance];

  [super updateProps:props oldProps:oldProps];
  // the swiftui element that is the control carries testID (OneNativeAccessibility), so a test
  // taps the control and not the centre of its wider host. the host keeps it only while
  // that element is hidden.
  self.accessibilityIdentifier = nil;
  _nativeView.accessibilityIdentifier = nil;
}
- (void)prepareForRecycle { [super prepareForRecycle]; [_nativeView reset]; _markersDirty = YES; }
- (UIView *)composableFor:(UIView<RCTComponentViewProtocol> *)child {
  UIView *content = [child isKindOfClass:RCTViewComponentView.class] ? ((RCTViewComponentView *)child).contentView : nil;
  if (content == nil)
    [NSException raise:@"OneNativeInvalidChild" format:@"One.iOS.Map requires One Native children"];
  return content;
}
- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)child index:(NSInteger)index {
  [_nativeView insertChild:[self composableFor:child] at:index];
}
- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)child index:(NSInteger)index {
  [_nativeView removeChild:[self composableFor:child]];
}
@end
