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

//# sourceMappingURL=useSortable.js.map