// 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 GaugeModel: ObservableObject {
  @Published var label: String = ""
  @Published var disabled: Bool = false
  @Published var value: Double = 0
  @Published var minimumValue: Double = 0
  @Published var maximumValue: Double = 1
  @Published var currentValueLabel: String = ""
  @Published var minimumValueLabel: String = ""
  @Published var maximumValueLabel: String = ""
  @Published var gaugeStyle: String = "automatic"
  @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 OneNativeGaugeView: UIView, OneNativeComposable {
  public var onSDKEvent: ((String, String) -> Void)?

  private var model = GaugeModel()
  public var onHeight: ((CGFloat) -> Void)?
  private var controller: OneNativeHostingController<OneNativeMeasuredStandalone<GaugeContent>>?
  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(_ label: String, disabled: Bool, value: Double, minimumValue: Double, maximumValue: Double, currentValueLabel: String, minimumValueLabel: String, maximumValueLabel: String, gaugeStyle: String) {
    if model.label != label { model.label = label }
    if model.disabled != disabled { model.disabled = disabled }
    if model.value != value { model.value = value }
    if model.minimumValue != minimumValue { model.minimumValue = minimumValue }
    if model.maximumValue != maximumValue { model.maximumValue = maximumValue }
    if model.currentValueLabel != currentValueLabel { model.currentValueLabel = currentValueLabel }
    if model.minimumValueLabel != minimumValueLabel { model.minimumValueLabel = minimumValueLabel }
    if model.maximumValueLabel != maximumValueLabel { model.maximumValueLabel = maximumValueLabel }
    if model.gaugeStyle != gaugeStyle { model.gaugeStyle = gaugeStyle }
  }


  private weak var compositionParent: OneNativeCompositionParent?
  public func compositionContent() -> AnyView { AnyView(GaugeContent(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: OneNativeMeasuredStandalone(content: GaugeContent(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
    controller?.detach(); controller = nil; model = GaugeModel()
  }
}
private struct GaugeContent: View {
  @ObservedObject var model: GaugeModel
  var body: some View {
    Gauge(value: model.value, in: model.minimumValue...model.maximumValue) {
        Text(model.label)
      } currentValueLabel: {
        Text(model.currentValueLabel)
      } minimumValueLabel: {
        Text(model.minimumValueLabel)
      } maximumValueLabel: {
        Text(model.maximumValueLabel)
      }
      .oneNativeGaugeStyle(model.gaugeStyle)
      .disabled(model.disabled)
      .oneNativeAccessibility(model.accessibility)
      .oneNativeStyle(model.swiftStyle, emit: model.emitSDKEvent)
  }
}
