// 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 ColorPickerModel: ObservableObject {
  @Published var controlled = OneNativeControlled<String>("#000000")
  @Published var label: String = ""
  @Published var disabled: Bool = false
  @Published var supportsOpacity: Bool = true
  @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) } }

  var onChange: ((String, Int, Int) -> Void)?
  func change(_ value: String) {
    guard active, !disabled, controlled.value != value else { return }
    controlled.change(value)
    onChange?(value, controlled.eventCount, controlled.revision)
  }
}
@objcMembers public final class OneNativeColorPickerView: UIView, OneNativeComposable {
  public var onSDKEvent: ((String, String) -> Void)?

  public var onChange: ((String, Int, Int) -> Void)?
  private var model = ColorPickerModel()
  public var onHeight: ((CGFloat) -> Void)?
  private var controller: OneNativeHostingController<OneNativeMeasuredStandalone<ColorPickerContent>>?
  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(_ value: String, acknowledgedEvent: Int, revision: Int, label: String, disabled: Bool, supportsOpacity: Bool) {
    if let next = model.controlled.applying(value, acknowledged: acknowledgedEvent, revision: revision) { model.controlled = next }
    if model.label != label { model.label = label }
    if model.disabled != disabled { model.disabled = disabled }
    if model.supportsOpacity != supportsOpacity { model.supportsOpacity = supportsOpacity }
  }


  private weak var compositionParent: OneNativeCompositionParent?
  public func compositionContent() -> AnyView { AnyView(ColorPickerContent(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) }

    model.onChange = { [weak self] value, count, revision in self?.onChange?(value, count, revision) }
  }
  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: OneNativeMeasuredStandalone(content: ColorPickerContent(model: model), onHeight: { [weak self] height in self?.onHeight?(height) }))
    }
    controller?.attach(to: self)
    model.active = controller?.isAttached == true
  }
  public func reset() {
    compositionParent = nil
    model.active = false; model.onSDKEvent = nil; model.onChange = nil
    controller?.detach(); controller = nil; model = ColorPickerModel()
  }
}
private struct ColorPickerContent: View {
  @ObservedObject var model: ColorPickerModel
  var body: some View {
    ColorPicker(LocalizedStringKey(model.label), selection: Binding(
        get: { oneNativeDecodeColor(model.controlled.value) },
        set: { value in model.change(oneNativeEncodeColor(value, supportsOpacity: model.supportsOpacity)) }
      ), supportsOpacity: model.supportsOpacity)
      .disabled(model.disabled)
      .oneNativeAccessibility(model.accessibility)
      .oneNativeStyle(model.swiftStyle, emit: model.emitSDKEvent)
  }
}
private func oneNativeDecodeColor(_ value: String) -> Color {
  let hex = String(value.dropFirst())
  let number = UInt64(hex, radix: 16)!
  let hasAlpha = hex.count == 8
  return Color(
    red: Double((number >> (hasAlpha ? 24 : 16)) & 0xff) / 255,
    green: Double((number >> (hasAlpha ? 16 : 8)) & 0xff) / 255,
    blue: Double((number >> (hasAlpha ? 8 : 0)) & 0xff) / 255,
    opacity: hasAlpha ? Double(number & 0xff) / 255 : 1
  )
}

private func oneNativeEncodeColor(_ color: Color, supportsOpacity: Bool) -> String {
  var red: CGFloat = 0
  var green: CGFloat = 0
  var blue: CGFloat = 0
  var alpha: CGFloat = 0
  precondition(UIColor(color).getRed(&red, green: &green, blue: &blue, alpha: &alpha), "ColorPicker produced a non-RGB color")
  let channels = [red, green, blue] + (supportsOpacity ? [alpha] : [])
  return "#" + channels.map { String(format: "%02X", Int(($0 * 255).rounded())) }.joined()
}
