import { useEventListener as e } from "./useEventListener.js";
import { computed as t, watch as n, watchEffect as r } from "vue";
var i = {
	item: "sortable__item",
	animate: "sortable__animate"
}, a = (e) => e.getBoundingClientRect().y, o = (e) => {
	let { y: t, height: n } = e.getBoundingClientRect();
	return t + Math.ceil(n / 2);
}, s = (e) => {
	let { y: t, height: n } = e.getBoundingClientRect();
	return t + n;
};
/**
* Description: Scroll support has been added to the container where sorting takes place.
* Some functionality duplicates the behavior of useScrollable.
*
* Purpose: To enable automatic scrolling when dragging items beyond the visible area of the container.
* Future Plan:
* - Verify the behavior of the new scrolling functionality within blocks and text elements.
* - Merge the current implementation with useScrollable to eliminate code duplication and create a unified scrolling solution.
*/
function c(c, l) {
	let u = {
		item: void 0,
		options() {
			return [...c.value?.children ?? []];
		}
	}, d = 0;
	n(() => c.value, () => {
		setTimeout(() => {
			c.value && (c.value.scrollTop = d);
		}, 0);
	});
	let f = t(() => u.options()), p = l.shakeBuffer ?? 10, m = l.reorderDelay ?? 100;
	function h(e) {
		let t = l.handle ? this.querySelector(l.handle) : null;
		t && (t && !t.contains(e.target) || (this.classList.remove(i.animate), this.classList.add(i.item), u.item = {
			el: this,
			y: e.y,
			dy: 0,
			initialScrollTop: c.value?.scrollTop || 0,
			orderChanged: !1
		}));
	}
	function g(e) {
		let t = u.options();
		return t.slice(0, t.indexOf(e));
	}
	function _(e) {
		let t = u.options();
		return t.slice(t.indexOf(e) + 1);
	}
	function v(e, t) {
		let n = u.options().filter((e) => e !== t), r = n.indexOf(e);
		return n.splice(r, 0, t), n;
	}
	function y(e, t) {
		let n = u.options().filter((e) => e !== t), r = n.indexOf(e);
		return n.splice(r + 1, 0, t), n;
	}
	function b(e, t) {
		let n = (c.value?.scrollTop || 0) - (e.initialScrollTop || 0);
		e.dy = t - e.y + n, e.el.style.setProperty("transform", `translateY(${e.dy}px)`);
	}
	function x(e) {
		if (!u.item) return;
		let { el: t } = u.item;
		if (!t.isConnected) {
			u.item = void 0;
			return;
		}
		let n = e.map((e) => a(e)), r = a(t);
		c.value?.replaceChildren(...e);
		let o = a(t), s = e.map((e) => a(e)), l = [];
		for (let t = 0; t < s.length; t++) {
			let r = e[t];
			if (r === u.item.el) continue;
			let i = s[t], a = n[t] - i;
			r.style.transform = `translateY(${a}px)`, l.push(r);
		}
		let d = o - r;
		u.item.y = u.item.y + d, u.item.dy = u.item.dy - d, u.item.orderChanged = !0, u.item.el.style.setProperty("transform", `translateY(${u.item.dy}px)`), l.forEach((e) => e.classList.remove(i.animate)), requestAnimationFrame(function() {
			l.forEach((e) => {
				e.classList.add(i.animate), e.style.transform = "", e.addEventListener("transitionend", () => {
					e.classList.remove(i.animate);
				});
			});
		});
	}
	e(window, "mousemove", (e) => {
		if (!u.item) return;
		let { el: t } = u.item;
		b(u.item, e.y);
		let n = a(u.item.el), r = s(u.item.el), i = g(t), l = _(t);
		if (i.forEach((e) => {
			let r = o(e);
			n + p < r && x(v(e, t));
		}), l.forEach((e) => {
			let n = o(e);
			r - p > n && x(y(e, t));
		}), c.value) {
			let n = c.value.getBoundingClientRect(), r = n.top + t.getBoundingClientRect().height / 2;
			e.clientY < r && (c.value.scrollTop += e.clientY - r);
			let i = n.bottom - t.getBoundingClientRect().height / 2;
			e.clientY > i && (c.value.scrollTop += e.clientY - i);
		}
	}), e(window, "mouseup", () => {
		if (!u.item) return;
		d = c.value?.scrollTop || 0;
		let { el: e, orderChanged: t } = u.item;
		e.classList.add(i.animate), e.style.removeProperty("transform"), e.addEventListener("transitionend", () => {
			e.classList.remove(i.animate, i.item);
		}), setTimeout(() => {
			if (!t) return;
			let e = u.options().map((e) => Number(e.getAttribute("data-index"))), n = c.value;
			if (n) {
				for (let e of u.options()) n.removeChild(e);
				f.value.forEach((e) => {
					n.appendChild(e);
				});
			}
			l.onChange(e);
		}, m), u.item = void 0;
	}), r(() => {
		f.value.forEach((e, t) => {
			e.removeEventListener("mousedown", h), e.addEventListener("mousedown", h), e.setAttribute("data-index", String(t));
		});
	});
}
export { c as useSortable2 };

//# sourceMappingURL=useSortable2.js.map