import v from "@leaflink/snitch";
import { c as I } from "./index-CLv1CFWG.js";
import { isRef as w, toValue as D, nextTick as A, computed as i, ref as y, watch as S, onMounted as $, onBeforeUnmount as G } from "vue";
function O({ list: n, from: s, to: e }) {
  if (s === e) return;
  const r = w(n), a = r ? [...D(n)] : D(n);
  if (e >= 0 && e < a.length) {
    const u = a.splice(s, 1)[0];
    A(() => {
      a.splice(e, 0, u), r && (n.value = a);
    });
  }
}
function B(n, s) {
  var a;
  const e = c(n.target).cloneNode(!0);
  e.classList.add(...s), e.style.position = "absolute", e.style.top = "-9999px", e.style.listStyle = "none", e.setAttribute("data-test", `${e.dataset.test}|ghost`), e.removeAttribute("draggable"), document.body.appendChild(e), (a = n.dataTransfer) == null || a.setDragImage(e, e.offsetWidth / 2, e.offsetHeight / 2);
  function r() {
    requestAnimationFrame(() => {
      document.body.removeChild(e);
    });
  }
  return {
    ghost: e,
    cleanupGhostElement: r
  };
}
function c(n) {
  const s = n == null ? void 0 : n.closest("[draggable=true]");
  if (!s) throw new Error("Draggable element not found");
  return s;
}
function q({
  isEnabled: n = !0,
  sortInPlace: s = !0,
  ...e
}) {
  const r = i(() => I(e.ref)), a = y(-1), u = y(-1), b = i(() => a.value !== -1), T = i(() => {
    var t;
    return ((t = e.ghostClass) == null ? void 0 : t.split(" ")) || [];
  }), f = i(() => {
    var t;
    return ((t = e.chosenClass) == null ? void 0 : t.split(" ")) || [];
  }), g = (t) => {
    var l;
    if (!(!t.dataTransfer || !r.value)) {
      t.dataTransfer.setData("text/plain", ""), t.dataTransfer.effectAllowed = "move";
      try {
        const o = c(t.target), d = Array.from(r.value.children).indexOf(o);
        if (d === -1) return;
        const { cleanupGhostElement: C } = B(t, T.value);
        u.value = a.value = d, o.classList.add(...f.value), C(), (l = e.onDragStart) == null || l.call(e, { ...t, oldIndex: d });
      } catch (o) {
        v.error(`Error in drag start: ${o}`);
      }
    }
  }, m = (t) => {
    if (!(!t.dataTransfer || !r.value)) {
      t.dataTransfer.dropEffect = "move";
      try {
        const l = c(t.target), o = Array.from(r.value.children).indexOf(l);
        if (o === -1 || o === a.value) return;
        const d = r.value.children[a.value];
        r.value.insertBefore(d, o < a.value ? l : l.nextSibling), a.value = o;
      } catch (l) {
        v.error(`Error in drag enter: ${l}`);
      }
    }
  };
  function h(t) {
    t.preventDefault();
  }
  function E(t) {
    if (t.preventDefault(), !!r.value)
      try {
        if (u.value === a.value || !s) return;
        O({ list: e.list, from: u.value, to: a.value });
      } catch (l) {
        v.error(`Error in drag drop: ${l}`);
      }
  }
  function L(t) {
    var l;
    try {
      c(t.target).classList.remove(...f.value), (l = e.onDragEnd) == null || l.call(e, {
        ...t,
        oldIndex: u.value,
        newIndex: a.value
      }), u.value = a.value = -1;
    } catch (o) {
      v.error(`Error in drag end: ${o}`);
    }
  }
  S(
    () => n,
    () => n ? p() : x()
  );
  function p() {
    r.value && (r.value.addEventListener("dragstart", g), r.value.addEventListener("dragend", L), r.value.addEventListener("dragenter", m), r.value.addEventListener("dragover", h), r.value.addEventListener("drop", E));
  }
  function x() {
    r.value && (r.value.removeEventListener("dragstart", g), r.value.removeEventListener("dragend", L), r.value.removeEventListener("dragenter", m), r.value.removeEventListener("dragover", h), r.value.removeEventListener("drop", E));
  }
  return $(() => {
    n && p();
  }), G(x), {
    isDragging: b,
    oldIndex: u,
    newIndex: a
  };
}
export {
  q as default,
  O as moveSortableElements
};
//# sourceMappingURL=useSortable.js.map
