// 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 SliderModel: ObservableObject {
  @Published var controlled = OneNativeControlled<Double>(0)
  @Published var label: String = ""
  @Published var disabled: Bool = false
  @Published var minimumValue: Double = 0
  @Published var maximumValue: Double = 100
  @Published var step: Double = 1
  @Published var minimumValueLabel: String = ""
  @Published var maximumValueLabel: String = ""
  @Published var minimumValueImage: String = ""
  @Published var maximumValueImage: String = ""
  @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 syncStateId: Int = 0
  private var syncToken: Int = 0
  func bindSyncState(_ id: Int) {
    if id == syncStateId { return }
    if syncStateId != 0 { OneNativeSyncRegistry.unobserve(Int32(syncStateId), token: syncToken) }
    syncStateId = id
    syncToken = 0
    if id == 0 { return }
    if let current = OneNativeSyncRegistry.get(Int32(id)) as? Double { controlled.adopt(current) }
    syncToken = OneNativeSyncRegistry.observe(Int32(id)) { [weak self] value in
      guard let self, let next = value as? Double else { return }
      self.controlled.adopt(next)
    }
  }
  deinit {
    if syncStateId != 0 { OneNativeSyncRegistry.unobserve(Int32(syncStateId), token: syncToken) }
  }
  var onChange: ((Double, Int, Int) -> Void)?
  func change(_ value: Double) {
    guard active, !disabled, controlled.value != value else { return }
    controlled.change(value)
    if syncStateId != 0 { OneNativeSyncRegistry.set(Int32(syncStateId), value: value as NSObject) }
    onChange?(value, controlled.eventCount, controlled.revision)
  }
}
@objcMembers public final class OneNativeSliderView: UIView, OneNativeComposable {
  public var onSDKEvent: ((String, String) -> Void)?

  public var onChange: ((Double, Int, Int) -> Void)?
  private var model = SliderModel()
  public var onHeight: ((CGFloat) -> Void)?
  private var controller: OneNativeHostingController<OneNativeMeasuredStandalone<SliderContent>>?
  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: Double, acknowledgedEvent: Int, revision: Int, syncStateId: Int, label: String, disabled: Bool, minimumValue: Double, maximumValue: Double, step: Double, minimumValueLabel: String, maximumValueLabel: String, minimumValueImage: String, maximumValueImage: String) {
    if let next = model.controlled.applying(value, acknowledged: acknowledgedEvent, revision: revision) { model.controlled = next }
    model.bindSyncState(syncStateId)
    if model.label != label { model.label = label }
    if model.disabled != disabled { model.disabled = disabled }
    if model.minimumValue != minimumValue { model.minimumValue = minimumValue }
    if model.maximumValue != maximumValue { model.maximumValue = maximumValue }
    if model.step != step { model.step = step }
    if model.minimumValueLabel != minimumValueLabel { model.minimumValueLabel = minimumValueLabel }
    if model.maximumValueLabel != maximumValueLabel { model.maximumValueLabel = maximumValueLabel }
    if model.minimumValueImage != minimumValueImage { model.minimumValueImage = minimumValueImage }
    if model.maximumValueImage != maximumValueImage { model.maximumValueImage = maximumValueImage }
  }


  private weak var compositionParent: OneNativeCompositionParent?
  public func compositionContent() -> AnyView { AnyView(SliderContent(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: SliderContent(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 = SliderModel()
  }
}
private struct SliderContent: View {
  @ObservedObject var model: SliderModel
  var body: some View {
    Group {
        if model.minimumValueImage.isEmpty, model.minimumValueLabel.isEmpty,
          model.maximumValueImage.isEmpty, model.maximumValueLabel.isEmpty {
          Slider(value: oneNativeSliderBinding(model), in: model.minimumValue...model.maximumValue, step: model.step) {
            Text(model.label)
          } onEditingChanged: { _ in }
        } else {
          Slider(value: oneNativeSliderBinding(model), in: model.minimumValue...model.maximumValue, step: model.step) {
            Text(model.label)
          } minimumValueLabel: {
            oneNativeSliderValueLabel(image: model.minimumValueImage, label: model.minimumValueLabel)
          } maximumValueLabel: {
            oneNativeSliderValueLabel(image: model.maximumValueImage, label: model.maximumValueLabel)
          } onEditingChanged: { _ in }
        }
      }
      .disabled(model.disabled)
      .oneNativeAccessibility(model.accessibility)
      .oneNativeStyle(model.swiftStyle, emit: model.emitSDKEvent)
  }
}
private func oneNativeSliderBinding(_ model: SliderModel) -> Binding<Double> {
  Binding(get: { model.controlled.value }, set: { value in model.change(value) })
}

// an image wins over a label on the same side; neither one set draws nothing there.
@ViewBuilder private func oneNativeSliderValueLabel(image: String, label: String) -> some View {
  if !image.isEmpty { Image(systemName: image) }
  else if !label.isEmpty { Text(label) }
  else { EmptyView() }
}
