#ifdef RCT_NEW_ARCH_ENABLED

#import "MonthYearPickerViewComponentView.h"
#import "RNMonthYearPicker.h"

#import <React/RCTConversions.h>
#import <react/renderer/components/RNMonthYearPickerSpec/ComponentDescriptors.h>
#import <react/renderer/components/RNMonthYearPickerSpec/EventEmitters.h>
#import <react/renderer/components/RNMonthYearPickerSpec/Props.h>
#import <react/renderer/components/RNMonthYearPickerSpec/RCTComponentViewHelpers.h>

using namespace facebook::react;

@interface MonthYearPickerViewComponentView () <RCTMonthYearPickerViewViewProtocol>
@end

@implementation MonthYearPickerViewComponentView {
  RNMonthYearPicker *_pickerView;
}

+ (ComponentDescriptorProvider)componentDescriptorProvider
{
  return concreteComponentDescriptorProvider<MonthYearPickerViewComponentDescriptor>();
}

- (instancetype)initWithFrame:(CGRect)frame
{
  if (self = [super initWithFrame:frame]) {
    static const auto defaultProps = std::make_shared<const MonthYearPickerViewProps>();
    _props = defaultProps;

    _pickerView = [RNMonthYearPicker new];

    __weak __typeof(self) weakSelf = self;
    _pickerView.onChange = ^(NSDictionary *event) {
      __typeof(self) strongSelf = weakSelf;
      if (!strongSelf || !strongSelf->_eventEmitter) {
        return;
      }
      NSString *newDate = event[@"newDate"];
      auto eventEmitter = std::dynamic_pointer_cast<const MonthYearPickerViewEventEmitter>(strongSelf->_eventEmitter);
      if (eventEmitter) {
        MonthYearPickerViewEventEmitter::OnChange onChangeEvent = {
          .newDate = RCTStringFromNSString(newDate ?: @""),
        };
        eventEmitter->onChange(onChangeEvent);
      }
    };

    self.contentView = _pickerView;
  }
  return self;
}

- (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps
{ 
  const auto &newProps = *std::static_pointer_cast<const MonthYearPickerViewProps>(props);

  // value (milliseconds → NSDate)
  NSDate *value = [NSDate dateWithTimeIntervalSince1970:newProps.value / 1000.0];
  _pickerView.value = value;

  // minimumDate (0.0 means unset)
  _pickerView.minimumDate = newProps.minimumDate > 0
    ? [NSDate dateWithTimeIntervalSince1970:newProps.minimumDate / 1000.0]
    : nil;

  // maximumDate (0.0 means unset)
  _pickerView.maximumDate = newProps.maximumDate > 0
    ? [NSDate dateWithTimeIntervalSince1970:newProps.maximumDate / 1000.0]
    : nil;

  // locale must be set before reloadAllComponents so month names render correctly
  NSString *localeIdentifier = newProps.locale.empty()
    ? [[NSLocale preferredLanguages] objectAtIndex:0]
    : RCTNSStringFromString(newProps.locale);
  NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
  [_pickerView setLocale:locale];

  // textColor — spec passes hex string, parse via RNMonthYearPicker
  _pickerView.textColor = newProps.textColor.empty()
    ? nil
    : [RNMonthYearPicker colorFromString:RCTNSStringFromString(newProps.textColor)];
  [_pickerView reloadAllComponents];

  [super updateProps:props oldProps:oldProps];
}


Class<RCTComponentViewProtocol> MonthYearPickerViewCls(void)
{
  return MonthYearPickerViewComponentView.class;
}

@end

#endif // RCT_NEW_ARCH_ENABLED
