import { watch as P, computed as D, watchEffect as _ } from "vue";
import { useEventListener as T } from "./useEventListener.js";
const m = {
  item: "sortable__item",
  animate: "sortable__animate"
}, p = (o) => o.getBoundingClientRect().y, x = (o) => {
  const { y: u, height: n } = o.getBoundingClientRect();
  return u + Math.ceil(n / 2);
}, $ = (o) => {
  const { y: u, height: n } = o.getBoundingClientRect();
  return u + n;
};
function F(o, u) {
  const n = {
    item: void 0,
    options() {
      var t;
      return [...((t = o.value) == null ? void 0 : t.children) ?? []];
    }
  };
  let v = 0;
  P(() => o.value, () => {
    setTimeout(() => {
      o.value && (o.value.scrollTop = v);
    }, 0);
  });
  const y = D(() => n.options()), g = u.shakeBuffer ?? 10, E = u.reorderDelay ?? 100;
  function C(t) {
    var i;
    const e = u.handle ? this.querySelector(u.handle) : null;
    e && (e && !e.contains(t.target) || (this.classList.remove(m.animate), this.classList.add(m.item), n.item = {
      el: this,
      y: t.y,
      dy: 0,
      initialScrollTop: ((i = o.value) == null ? void 0 : i.scrollTop) || 0,
      orderChanged: !1
    }));
  }
  function L(t) {
    const e = n.options();
    return e.slice(0, e.indexOf(t));
  }
  function B(t) {
    const e = n.options();
    return e.slice(e.indexOf(t) + 1);
  }
  function Y(t, e) {
    const i = n.options().filter((s) => s !== e), a = i.indexOf(t);
    return i.splice(a, 0, e), i;
  }
  function b(t, e) {
    const i = n.options().filter((s) => s !== e), a = i.indexOf(t);
    return i.splice(a + 1, 0, e), i;
  }
  function S(t, e) {
    var s;
    const a = (((s = o.value) == null ? void 0 : s.scrollTop) || 0) - (t.initialScrollTop || 0);
    t.dy = e - t.y + a, t.el.style.setProperty("transform", `translateY(${t.dy}px)`);
  }
  function w(t) {
    var f;
    if (!n.item)
      return;
    const { el: e } = n.item;
    if (!e.isConnected) {
      n.item = void 0;
      return;
    }
    const i = t.map((r) => p(r)), a = p(e);
    (f = o.value) == null || f.replaceChildren(...t);
    const s = p(e), l = t.map((r) => p(r)), c = [];
    for (let r = 0; r < l.length; r++) {
      const h = t[r];
      if (h === n.item.el)
        continue;
      const A = l[r], O = i[r] - A;
      h.style.transform = `translateY(${O}px)`, c.push(h);
    }
    const d = s - a;
    n.item.y = n.item.y + d, n.item.dy = n.item.dy - d, n.item.orderChanged = !0, n.item.el.style.setProperty("transform", `translateY(${n.item.dy}px)`), c.forEach((r) => r.classList.remove(m.animate)), requestAnimationFrame(function() {
      c.forEach((r) => {
        r.classList.add(m.animate), r.style.transform = "", r.addEventListener("transitionend", () => {
          r.classList.remove(m.animate);
        });
      });
    });
  }
  T(window, "mousemove", (t) => {
    if (!n.item)
      return;
    const { el: e } = n.item;
    S(n.item, t.y);
    const i = p(n.item.el), a = $(n.item.el), s = L(e), l = B(e);
    if (s.forEach((c) => {
      const d = x(c);
      i + g < d && w(Y(c, e));
    }), l.forEach((c) => {
      const d = x(c);
      a - g > d && w(b(c, e));
    }), o.value) {
      const c = o.value.getBoundingClientRect(), d = c.top + e.getBoundingClientRect().height / 2;
      t.clientY < d && (o.value.scrollTop += t.clientY - d);
      const f = c.bottom - e.getBoundingClientRect().height / 2;
      t.clientY > f && (o.value.scrollTop += t.clientY - f);
    }
  }), T(window, "mouseup", () => {
    var i;
    if (!n.item)
      return;
    v = ((i = o.value) == null ? void 0 : i.scrollTop) || 0;
    const { el: t, orderChanged: e } = n.item;
    t.classList.add(m.animate), t.style.removeProperty("transform"), t.addEventListener("transitionend", () => {
      t.classList.remove(m.animate, m.item);
    }), setTimeout(() => {
      if (!e)
        return;
      const a = n.options().map((l) => Number(l.getAttribute("data-index"))), s = o.value;
      if (s) {
        for (const l of n.options())
          s.removeChild(l);
        y.value.forEach((l) => {
          s.appendChild(l);
        });
      }
      u.onChange(a);
    }, E), n.item = void 0;
  }), _(() => {
    y.value.forEach((t, e) => {
      t.removeEventListener("mousedown", C), t.addEventListener("mousedown", C), t.setAttribute("data-index", String(e));
    });
  });
}
export {
  F as useSortable2
};
//# sourceMappingURL=useSortable2.js.map
