// 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 ImageModel: ObservableObject {
  @Published var systemName: String = ""
  @Published var uri: String = ""
  @Published var renderingMode: String = "original"
  @Published var symbolRenderingMode: String = ""
  @Published var symbolVariant: String = ""
  @Published var imageScale: String = ""
  @Published var variableValue: Double = 0
  @Published var hasVariableValue: Bool = false
  @Published var colorRole: String = ""
  @Published var resizable: Bool = false
  @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 OneNativeImageView: UIView, OneNativeComposable {
  public var onSDKEvent: ((String, String) -> Void)?

  private var model = ImageModel()
  public var onHeight: ((CGFloat) -> Void)?
  private var controller: OneNativeHostingController<OneNativeMeasuredStandalone<ImageContent>>?
  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(_ systemName: String, uri: String, renderingMode: String, symbolRenderingMode: String, symbolVariant: String, imageScale: String, variableValue: Double, hasVariableValue: Bool, colorRole: String, resizable: Bool) {
    if model.systemName != systemName { model.systemName = systemName }
    if model.uri != uri { model.uri = uri }
    if model.renderingMode != renderingMode { model.renderingMode = renderingMode }
    if model.symbolRenderingMode != symbolRenderingMode { model.symbolRenderingMode = symbolRenderingMode }
    if model.symbolVariant != symbolVariant { model.symbolVariant = symbolVariant }
    if model.imageScale != imageScale { model.imageScale = imageScale }
    if model.variableValue != variableValue { model.variableValue = variableValue }
    if model.hasVariableValue != hasVariableValue { model.hasVariableValue = hasVariableValue }
    if model.colorRole != colorRole { model.colorRole = colorRole }
    if model.resizable != resizable { model.resizable = resizable }
  }


  private weak var compositionParent: OneNativeCompositionParent?
  public func compositionContent() -> AnyView { AnyView(ImageContent(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: ImageContent(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 = ImageModel()
  }
}
private struct ImageContent: View {
  @ObservedObject var model: ImageModel
  var body: some View {
    Group {
        if !model.uri.isEmpty {
          OneNativeRemoteImage(uri: model.uri, template: model.renderingMode == "template")
        } else if model.hasVariableValue {
          if model.resizable {
            Image(systemName: model.systemName, variableValue: model.variableValue).resizable()
          } else {
            Image(systemName: model.systemName, variableValue: model.variableValue)
          }
        } else if model.resizable {
          Image(systemName: model.systemName).resizable()
        } else {
          Image(systemName: model.systemName)
        }
      }
      .oneNativeSymbolRenderingMode(model.symbolRenderingMode)
      .oneNativeSymbolVariant(model.symbolVariant)
      .oneNativeImageScale(model.imageScale)
      .oneNativeColorRole(model.colorRole)
      .oneNativeAccessibility(model.accessibility, decorativeWhenUnlabeled: true)
      .oneNativeStyle(model.swiftStyle, emit: model.emitSDKEvent)
  }
}
