// generated by bun run generate from the SwiftUI SDK and codegen/catalog.ts.
// edit the generator or catalog, then regenerate.
import SwiftUI
import UIKit
struct OneNativeSheetRoot: View {
  @ObservedObject var model: OneNativeSheetModel
  private var presented: Binding<Bool> {
    Binding(get: { model.controlled.value && model.content != nil }, set: { model.change($0) })
  }
  @ViewBuilder private var content: some View {
    if let content = model.content {
      OneNativeSlot(content: content, mode: .presented, onLayout: { frame in
        if model.active && model.controlled.value { model.onLayout?(frame) }
      }, onWindowChange: { identity, attached in
        model.presentedWindowChanged(identity, attached: attached)
      })
      .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
  }
  var body: some View {
    if model.presentation == "fullScreenCover" {
      Color.clear
        .fullScreenCover(isPresented: presented, onDismiss: { model.dismissed() }) {
          content
        }
    } else {
      Color.clear
        .sheet(isPresented: presented, onDismiss: { model.dismissed() }) {
          content
            .oneNativeSheetPresentation(model, fitToContents: model.fitToContents)
        }
    }
  }
}

extension View {
  @ViewBuilder func oneNativeSheetPresentation(
    _ model: OneNativeSheetModel, fitToContents: Bool
  ) -> some View {
    if fitToContents {
      self
        .presentationDetents(model.fittedHeight > 0 ? [.height(model.fittedHeight)] : [.medium])
        .oneNativeSheetPresentationModifiers(model)
    } else {
      self
        .presentationDetents(model.detents, selection: Binding(
          get: { model.selectedPresentationDetent },
          set: { model.changeDetent($0) }
        ))
        .oneNativeSheetPresentationModifiers(model)
    }
  }

  @ViewBuilder func oneNativeSheetPresentationModifiers(
    _ model: OneNativeSheetModel
  ) -> some View {
    self
      .oneNativePresentationDragIndicator(model.presentationDragIndicator)
      .interactiveDismissDisabled(model.interactiveDismissDisabled)
      .oneNativePresentationBackground(model.presentationBackground)
      .oneNativePresentationBackgroundInteraction(
        model.presentationBackgroundInteraction,
        upThrough: model.presentationBackgroundInteractionDetent)
      .oneNativePresentationContentInteraction(model.presentationContentInteraction)
      .oneNativePresentationSizing(model.presentationSizing)
      .preferredColorScheme(model.colorScheme.isEmpty ? nil : OneNativeGenerated.colorScheme(model.colorScheme))
  }

  @ViewBuilder func oneNativePresentationBackground(_ value: UIColor?) -> some View {
    if let value { self.presentationBackground(Color(uiColor: value)) }
    else { self }
  }

  @ViewBuilder func oneNativePresentationBackgroundInteraction(
    _ value: String, upThrough detent: PresentationDetent
  ) -> some View {
    switch value {
    case "enabled": self.presentationBackgroundInteraction(.enabled)
    case "disabled": self.presentationBackgroundInteraction(.disabled)
    case "enabledUpThrough": self.presentationBackgroundInteraction(.enabled(upThrough: detent))
    default: self.presentationBackgroundInteraction(.automatic)
    }
  }

  @ViewBuilder func oneNativePresentationContentInteraction(_ value: String) -> some View {
    self.presentationContentInteraction(
      OneNativeGenerated.presentationContentInteraction(value))
  }

  @ViewBuilder func oneNativePresentationSizing(_ value: String) -> some View {
    if #available(iOS 18.0, *) {
      switch value {
      case "fitted": self.presentationSizing(.fitted)
      case "form": self.presentationSizing(.form)
      case "page": self.presentationSizing(.page)
      default: self.presentationSizing(.automatic)
      }
    } else {
      self
    }
  }
}
