// BlurVibeViewFabric.mm
// Fabric (New Architecture) component view for BlurVibeView.
//
// This file is compiled ONLY when React Native's New Architecture is enabled
// (RCT_NEW_ARCH_ENABLED=1). The old arch path (BlurVibeViewManager.m +
// BlurVibeViewManager.swift + BlurVibeView.swift) continues to work unchanged
// on Old Architecture projects.
//
// Architecture routing:
//   New Arch (Fabric) → BlurVibeViewFabric (this file)
//   Old Arch (Paper)  → BlurVibeViewManager.m + BlurVibeView.swift
//
// Both render the same SwiftUI layer (BlurVibeSwiftUIView) so visual output
// is identical on both architectures.

#ifdef RCT_NEW_ARCH_ENABLED

#import <React/RCTViewComponentView.h>
#import <UIKit/UIKit.h>
#import <objc/runtime.h>

// Generated codegen headers (produced by `pod install` / yarn codegen)
#import <react/renderer/components/BlurVibeSpec/ComponentDescriptors.h>
#import <react/renderer/components/BlurVibeSpec/EventEmitters.h>
#import <react/renderer/components/BlurVibeSpec/Props.h>
#import <react/renderer/components/BlurVibeSpec/RCTComponentViewHelpers.h>
#import "RCTFabricComponentsPlugins.h"

// Import Swift types via the generated module header
// (react-native-blur-vibe-Swift.h is generated by Xcode for Swift/ObjC interop)
#import "react_native_blur_vibe-Swift.h"

using namespace facebook::react;

@interface BlurVibeViewFabric () <RCTBlurVibeViewViewProtocol>
@end

@implementation BlurVibeViewFabric {
  // The UIKit view that hosts the SwiftUI blur layer.
  // BlurVibeView is defined in BlurVibeView.swift — reused across both arches.
  BlurVibeView *_blurView;
}

// ── Fabric component descriptor ────────────────────────────────────────────

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

// ── Init ───────────────────────────────────────────────────────────────────

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

    _blurView = [[BlurVibeView alloc] initWithFrame:CGRectZero];
    _blurView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    self.contentView = _blurView;
    self.backgroundColor = UIColor.clearColor;
  }
  return self;
}

// ── Layout ─────────────────────────────────────────────────────────────────

- (void)layoutSubviews {
  [super layoutSubviews];
  _blurView.frame = self.bounds;
}

// ── Props update (called by Fabric on prop changes) ────────────────────────

- (void)updateProps:(Props::Shared const &)props
          oldProps:(Props::Shared const &)oldProps {

  const auto &p = *std::static_pointer_cast<const BlurVibeViewProps>(props);

  // Map codegen C++ props → Swift @objc vars on BlurVibeView
  _blurView.blurAmount  = @(p.blurAmount);
  _blurView.blurType    = [NSString stringWithUTF8String:p.blurType.c_str()];
  _blurView.overlayColor = [NSString stringWithUTF8String:p.overlayColor.c_str()];
  _blurView.reducedTransparencyFallbackColor =
    [NSString stringWithUTF8String:p.reducedTransparencyFallbackColor.c_str()];
  _blurView.blurRadius  = @(p.blurRadius);

  // enabled
  _blurView.enabled    = p.enabled  ? @YES : @NO;

  // Progressive blur
  _blurView.progressiveBlurDirection =
    [NSString stringWithUTF8String:p.progressiveBlurDirection.c_str()];
  _blurView.progressiveStartIntensity = @(p.progressiveStartIntensity);
  _blurView.progressiveEndIntensity   = @(p.progressiveEndIntensity);

  // Noise
  _blurView.noiseFactor = @(p.noiseFactor);

  [super updateProps:props oldProps:oldProps];
}

// ── Fabric component registration ──────────────────────────────────────────

Class<RCTComponentViewProtocol> BlurVibeViewFabricCls(void) {
  return BlurVibeViewFabric.class;
}

@end

#endif // RCT_NEW_ARCH_ENABLED