// Vulkan surface extensions per surface kind, resolved against what the loader
// actually reports.
//
// The previous `tycho_window.surface_extensions()` hard-coded a list per OS and
// drifted from reality: on Linux/X11 it advertised `VK_KHR_xcb_surface` while
// never creating an XCB surface. Asking for an extension the driver does not
// export makes `vkCreateInstance` fail, so candidates are now intersected with
// `vkEnumerateInstanceExtensionProperties` by the caller.

///|
/// Candidate instance extensions required to present through `kind`, in the
/// order they should be enabled. Empty when the kind has no Vulkan surface
/// (the web canvas).
pub fn surface_candidates(kind : SurfaceKind) -> Array[String] {
  match kind {
    Xlib => ["VK_KHR_surface", "VK_KHR_xlib_surface"]
    Xcb => ["VK_KHR_surface", "VK_KHR_xcb_surface"]
    Wayland => ["VK_KHR_surface", "VK_KHR_wayland_surface"]
    Win32 => ["VK_KHR_surface", "VK_KHR_win32_surface"]
    Metal => [
      "VK_KHR_surface", "VK_EXT_metal_surface", "VK_KHR_portability_enumeration",
    ]
    Android => ["VK_KHR_surface", "VK_KHR_android_surface"]
    CanvasGpu => []
    Headless => ["VK_EXT_headless_surface"]
  }
}

///|
/// The subset of `surface_candidates(kind)` that `available` actually contains.
/// Enable exactly this list, never the raw candidates.
pub fn surface_extensions_for(
  kind : SurfaceKind,
  available : Array[String],
) -> Array[String] {
  let out : Array[String] = []
  for candidate in surface_candidates(kind) {
    if contains(available, candidate) {
      out.push(candidate)
    }
  }
  out
}

///|
/// Whether this surface kind can present through Vulkan at all on a loader that
/// reports `available`. False both for kinds without a Vulkan surface (web) and
/// for loaders missing the platform extension.
pub fn surface_supported(kind : SurfaceKind, available : Array[String]) -> Bool {
  let candidates = surface_candidates(kind)
  if candidates.length() == 0 {
    return false
  }
  let usable = surface_extensions_for(kind, available)
  usable.length() == candidates.length()
}

fn contains(haystack : Array[String], needle : String) -> Bool {
  for item in haystack {
    if item == needle {
      return true
    }
  }
  false
}