// generated by bun run generate from the SwiftUI SDK and codegen/catalog.ts.
// edit the generator or catalog, then regenerate.
import SwiftUI
import UIKit

private final class RadialGradientModel: ObservableObject {
  @Published var colors: [String] = []
  @Published var center: String = "{\"x\":0.5,\"y\":0.5}"
  @Published var startRadius: Double = 0
  @Published var endRadius: Double = 0
  @Published var accessibility = OneNativeAccessibility()
  @Published var swiftStyle = OneNativeStyle()
  var active = false
  var onSDKEvent: ((String, String) -> Void)?
  func emitSDKEvent(_ name: String, _ value: String) { if active { onSDKEvent?(name, value) } }

}
@objcMembers public final class OneNativeRadialGradientView: UIView, OneNativeComposable {
  public var onSDKEvent: ((String, String) -> Void)?

  private var model = RadialGradientModel()
  private var controller: OneNativeHostingController<OneNativeStandalone<RadialGradientContent>>?
  public override init(frame: CGRect) { super.init(frame: frame) }
  required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") }
  public func configureAccessibility(_ label: String, hint: String, value: String, identifier: String) {
    let next = OneNativeAccessibility(label: label, hint: hint, value: value, identifier: identifier)
    if model.accessibility != next { model.accessibility = next }
  }
  public func configureStyle(_ style: [String: Any]) {
    let next = OneNativeStyle(dictionary: style)
    if model.swiftStyle != next {
      let previousRows = model.swiftStyle.sdkModifiers.filter { $0.first?.hasPrefix("listRow") == true }
      let nextRows = next.sdkModifiers.filter { $0.first?.hasPrefix("listRow") == true }
      model.swiftStyle = next
      if previousRows != nextRows { compositionParent?.refreshRow(for: self) }
    }
  }
  public func configure(_ center: String, startRadius: Double, endRadius: Double) {
    if model.center != center { model.center = center }
    if model.startRadius != startRadius { model.startRadius = startRadius }
    if model.endRadius != endRadius { model.endRadius = endRadius }
  }
  public func setColors(_ items: [String]) {
    guard items.allSatisfy({ oneNativeRadialGradientColor($0) != nil }) else {
      NSLog("OneNative RadialGradient received invalid colors")
      return
    }
    if model.colors != items { model.colors = items }
  }

  private weak var compositionParent: OneNativeCompositionParent?
  public func compositionContent() -> AnyView { AnyView(RadialGradientContent(model: model)) }
  // composed, activation follows the parent: a subtree mounted before root attachment
  // stays silent until the root attaches.
  public func composeInto(_ parent: OneNativeCompositionParent) {
    controller?.detach(); controller = nil
    compositionParent = parent
    bindCallbacks()
    model.active = parent.compositionActive
  }
  public func decompose() { compositionParent = nil; model.active = false }
  public func propagateActive(_ active: Bool) { model.active = active }
  public override func didMoveToWindow() { super.didMoveToWindow(); updateHost() }
  public override func layoutSubviews() { super.layoutSubviews(); updateHost() }
  private func bindCallbacks() {
    model.onSDKEvent = { [weak self] name, value in self?.onSDKEvent?(name, value) }

  }
  private func updateHost() {
    guard compositionParent == nil else { return }
    model.active = false
    guard window != nil else { controller?.detach(); return }
    if controller == nil {
      bindCallbacks()
      controller = OneNativeHostingController(rootView: OneNativeStandalone(content: RadialGradientContent(model: model)))
    }
    controller?.attach(to: self)
    model.active = controller?.isAttached == true
  }
  public func reset() {
    compositionParent = nil
    model.active = false; model.onSDKEvent = nil
    controller?.detach(); controller = nil; model = RadialGradientModel()
  }
}
private struct RadialGradientContent: View {
  @ObservedObject var model: RadialGradientModel
  var body: some View {
    RadialGradient(
      colors: model.colors.compactMap(oneNativeRadialGradientColor),
      center: oneNativeRadialGradientPoint(model.center),
      startRadius: model.startRadius,
      endRadius: model.endRadius
    )
      .oneNativeAccessibility(model.accessibility, decorativeWhenUnlabeled: true)
      .oneNativeStyle(model.swiftStyle, emit: model.emitSDKEvent)
  }
}
private struct OneNativeRadialGradientPoint: Decodable {
  let x: Double
  let y: Double
}

private func oneNativeRadialGradientPoint(_ raw: String) -> UnitPoint {
  guard let data = raw.data(using: .utf8),
    let point = try? JSONDecoder().decode(OneNativeRadialGradientPoint.self, from: data),
    point.x.isFinite, point.y.isFinite else { return .center }
  return UnitPoint(x: CGFloat(point.x), y: CGFloat(point.y))
}

private func oneNativeRadialGradientColor(_ value: String) -> Color? {
  guard value.first == "#", value.count == 7 || value.count == 9,
    let hex = UInt64(value.dropFirst(), radix: 16) else { return nil }
  let red, green, blue, alpha: UInt64
  if value.count == 7 {
    red = (hex >> 16) & 0xff
    green = (hex >> 8) & 0xff
    blue = hex & 0xff
    alpha = 0xff
  } else {
    red = (hex >> 24) & 0xff
    green = (hex >> 16) & 0xff
    blue = (hex >> 8) & 0xff
    alpha = hex & 0xff
  }
  return Color(.sRGB, red: Double(red) / 255, green: Double(green) / 255,
    blue: Double(blue) / 255, opacity: Double(alpha) / 255)
}
