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

private final class LocationButtonModel: ObservableObject {
  @Published var title: String = "currentLocation"
  @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 onAction: ((Int) -> Void)?
  private var actionCount = 0
  func action() {
    guard active else { return }
    actionCount += 1
    onAction?(actionCount)
  }
}
@objcMembers public final class OneNativeLocationButtonView: UIView, OneNativeComposable {
  public var onSDKEvent: ((String, String) -> Void)?

  public var onAction: ((Int) -> Void)?
  private var model = LocationButtonModel()
  public var onHeight: ((CGFloat) -> Void)?
  private var controller: OneNativeHostingController<OneNativeMeasuredStandalone<LocationButtonContent>>?
  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(_ title: String) {
    if model.title != title { model.title = title }
  }


  private weak var compositionParent: OneNativeCompositionParent?
  public func compositionContent() -> AnyView { AnyView(LocationButtonContent(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.onAction = { [weak self] actionCount in self?.onAction?(actionCount) }
  }
  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: LocationButtonContent(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.onAction = nil
    controller?.detach(); controller = nil; model = LocationButtonModel()
  }
}
private struct LocationButtonContent: View {
  @ObservedObject var model: LocationButtonModel
  var body: some View {
    LocationButton(oneNativeLocationButtonTitle(model.title), action: { model.action() })
      .oneNativeAccessibility(model.accessibility)
      .oneNativeStyle(model.swiftStyle, emit: model.emitSDKEvent)
  }
}
private func oneNativeLocationButtonTitle(_ value: String) -> _CoreLocationUI_SwiftUI.LocationButton.Title? {
  switch value {
  case "": return nil
  case "currentLocation": return _CoreLocationUI_SwiftUI.LocationButton.Title.currentLocation
  case "sendCurrentLocation": return _CoreLocationUI_SwiftUI.LocationButton.Title.sendCurrentLocation
  case "sendMyCurrentLocation": return _CoreLocationUI_SwiftUI.LocationButton.Title.sendMyCurrentLocation
  case "shareCurrentLocation": return _CoreLocationUI_SwiftUI.LocationButton.Title.shareCurrentLocation
  case "shareMyCurrentLocation": return _CoreLocationUI_SwiftUI.LocationButton.Title.shareMyCurrentLocation
  default: preconditionFailure("invalid LocationButton title: \(value)")
  }
}
