<script lang="ts">
  // Invisible resize-handle overlays around each Lumotia window edge.
  // Needed because Lumotia runs with decorations off, so the WM
  // provides no resize affordance on KDE/GNOME Wayland.
  //
  // Architecture: eight fixed-position divs are the click target for
  // resize. Because the divs themselves are the event target (not any
  // ancestor), the delegated handler for data-tauri-drag-region walks
  // the ancestors of the div, finds no drag-region, and does not start
  // a window move. This gives us a clean separation: resize wins on
  // the edges, drag wins on the titlebar.
  //
  // Hit zone sizes come from CSS custom properties so every Lumotia window
  // across the app stays identical. See the lumotia-resize-edge and
  // lumotia-resize-corner custom properties in app.css.
  //
  // Placement requirement: instances of this component MUST be mounted
  // as a sibling of the animated/transformed layout root (not a child),
  // or fixed positioning becomes relative to that transformed ancestor
  // instead of the viewport.

  import { getCurrentWindow } from "@tauri-apps/api/window";
  import { hasTauriRuntime } from "$lib/utils/runtime.js";

  const enabled = hasTauriRuntime();

  async function startResize(
    e: PointerEvent,
    direction: "North" | "South" | "West" | "East" | "NorthWest" | "NorthEast" | "SouthWest" | "SouthEast",
  ) {
    if (!enabled) return;
    if (e.button !== 0) return;
    e.preventDefault();
    e.stopPropagation();
    // Lock the pointer to this handle BEFORE asking Wayland to begin a
    // resize. Without capture, KWin xdg_toplevel.resize grab arrives
    // between mousedown synthesis and pointer movement, often losing the
    // diagonal component and collapsing a corner drag to a single axis.
    // pointerdown + setPointerCapture keeps the direction pinned.
    try {
      (e.currentTarget as HTMLElement | null)?.setPointerCapture?.(e.pointerId);
    } catch {}
    try {
      await getCurrentWindow().startResizeDragging(direction);
    } catch {}
  }
</script>

{#if enabled}
  <!-- svelte-ignore a11y_no_static_element_interactions -->
  <div class="lumotia-rh lumotia-rh-n" onpointerdown={(e) => startResize(e,"North")}></div>
  <!-- svelte-ignore a11y_no_static_element_interactions -->
  <div class="lumotia-rh lumotia-rh-s" onpointerdown={(e) => startResize(e,"South")}></div>
  <!-- svelte-ignore a11y_no_static_element_interactions -->
  <div class="lumotia-rh lumotia-rh-w" onpointerdown={(e) => startResize(e,"West")}></div>
  <!-- svelte-ignore a11y_no_static_element_interactions -->
  <div class="lumotia-rh lumotia-rh-e" onpointerdown={(e) => startResize(e,"East")}></div>

  <!-- svelte-ignore a11y_no_static_element_interactions -->
  <div class="lumotia-rh lumotia-rh-nw" onpointerdown={(e) => startResize(e,"NorthWest")}></div>
  <!-- svelte-ignore a11y_no_static_element_interactions -->
  <div class="lumotia-rh lumotia-rh-ne" onpointerdown={(e) => startResize(e,"NorthEast")}></div>
  <!-- svelte-ignore a11y_no_static_element_interactions -->
  <div class="lumotia-rh lumotia-rh-sw" onpointerdown={(e) => startResize(e,"SouthWest")}></div>
  <!-- svelte-ignore a11y_no_static_element_interactions -->
  <div class="lumotia-rh lumotia-rh-se" onpointerdown={(e) => startResize(e,"SouthEast")}></div>
{/if}
