import { Children as e, useCallback as t, useEffect as n, useLayoutEffect as r, useRef as i, useState as a } from "react";
import { cn as o } from "@ryuzaki13/react-foundation-lib/utils";
import { jsx as s, jsxs as c } from "react/jsx-runtime";
var l = {
	splitter: "wAovST",
	vertical: "QVVMwe",
	separator: "H43AJ8",
	horizontal: "pgo8sW",
	topLeft: "_8fCvqA",
	bottomRight: "t6RFcI"
}, u = .1, d = .9, f = .7, p = .02;
function m(e, t, n) {
	return Math.min(n, Math.max(t, e));
}
function h(e, t) {
	return Number.isFinite(e) ? m(e, 0, 1) : t;
}
function g(e, t) {
	let n = h(e, u), r = h(t, d);
	return n <= r ? [n, r] : [r, n];
}
function _(e, t) {
	return e === "vertical" ? t.clientX : t.clientY;
}
function v(e, t) {
	return e === "vertical" ? t.width : t.height;
}
function y(e, t) {
	if (e === "vertical") {
		if (t === "ArrowLeft") return -.02;
		if (t === "ArrowRight") return p;
	}
	if (e === "horizontal") {
		if (t === "ArrowUp") return -.02;
		if (t === "ArrowDown") return p;
	}
	return null;
}
function b(e, t) {
	return t === "start" ? 0 : t === "end" ? 1 : e;
}
function x({ direction: e, initial: o, min: s, max: c, collapsedPane: l }) {
	let [u, d] = g(s, c), p = i(null), x = i(null), S = i(null), C = i(null), w = i(null), T = i(null), E = i(0), D = i(m(h(o, f), u, d)), O = i(o), [k, A] = a(() => m(h(o, f), u, d)), j = i(k), M = t((e) => {
		j.current = e, x.current?.style.setProperty("flex-basis", `${b(e, l) * 100}%`), S.current?.setAttribute("aria-valuenow", `${Math.round(e * 100)}`);
	}, [l]), N = () => {
		if (T.current !== null && (cancelAnimationFrame(T.current), T.current = null), w.current === null) return j.current;
		let e = w.current;
		return w.current = null, M(e), e;
	}, P = (e) => {
		w.current = e, T.current === null && (T.current = requestAnimationFrame(() => {
			T.current = null, w.current !== null && M(w.current);
		}));
	}, F = () => {
		C.current?.(), C.current = null;
	}, I = (e) => {
		N(), A(m(e, u, d));
	};
	return r(() => {
		M(k);
	}, [M, k]), n(() => {
		A((e) => {
			let t = O.current !== o;
			return O.current = o, m(t ? h(o, f) : e, u, d);
		});
	}, [
		o,
		u,
		d
	]), n(() => () => {
		F(), T.current !== null && cancelAnimationFrame(T.current);
	}, []), {
		containerRef: p,
		topLeftRef: x,
		separatorRef: S,
		ratio: k,
		minRatio: u,
		maxRatio: d,
		handleSeparatorPointerDown: (t) => {
			if (t.button !== 0) return;
			t.preventDefault();
			let n = p.current;
			if (!n) return;
			F();
			try {
				t.currentTarget.setPointerCapture?.(t.pointerId);
			} catch {}
			E.current = _(e, t), D.current = j.current;
			let r = (t) => {
				let r = v(e, n.getBoundingClientRect());
				if (r <= 0) return;
				let i = _(e, t) - E.current, a = m(D.current + i / r, u, d);
				P(a);
			}, i = () => {
				F(), A(N());
			};
			C.current = () => {
				document.removeEventListener("pointermove", r), document.removeEventListener("pointerup", i), document.removeEventListener("pointercancel", i);
			}, document.addEventListener("pointermove", r), document.addEventListener("pointerup", i), document.addEventListener("pointercancel", i);
		},
		handleSeparatorKeyDown: (t) => {
			let n = y(e, t.key);
			if (n !== null) {
				t.preventDefault(), I(j.current + n);
				return;
			}
			if (t.key === "Home") {
				t.preventDefault(), I(u);
				return;
			}
			t.key === "End" && (t.preventDefault(), I(d));
		}
	};
}
function S({ className: e, direction: t, left: n, right: r, collapsedPane: i, logic: a }) {
	let { containerRef: u, topLeftRef: d, separatorRef: f, ratio: p, minRatio: m, maxRatio: h, handleSeparatorPointerDown: g, handleSeparatorKeyDown: _ } = a, v = b(p, i), y = i === "start", x = i === "end", S = i === null;
	return /* @__PURE__ */ c("div", {
		ref: u,
		className: o(l.splitter, l[t], e),
		children: [
			/* @__PURE__ */ s("div", {
				ref: d,
				className: l.topLeft,
				style: { flexBasis: `${v * 100}%` },
				hidden: y,
				children: n
			}),
			/* @__PURE__ */ s("div", {
				ref: f,
				hidden: !S,
				role: "separator",
				"aria-orientation": t === "vertical" ? "vertical" : "horizontal",
				"aria-valuemin": Math.round(m * 100),
				"aria-valuemax": Math.round(h * 100),
				"aria-valuenow": Math.round(p * 100),
				tabIndex: S ? 0 : -1,
				onPointerDown: g,
				onKeyDown: _,
				className: l.separator
			}),
			/* @__PURE__ */ s("div", {
				className: l.bottomRight,
				hidden: x,
				children: r
			})
		]
	});
}
function C({ direction: t = "vertical", initial: n = f, min: r = u, max: i = d, className: a, collapsedPane: o = null, children: c }) {
	let [l, p] = e.toArray(c), m = x({
		direction: t,
		initial: n,
		min: r,
		max: i,
		collapsedPane: o
	});
	return p ? /* @__PURE__ */ s(S, {
		className: a,
		direction: t,
		left: l,
		right: p,
		collapsedPane: o,
		logic: m
	}) : l;
}
//#endregion
export { C as Splitter };

//# sourceMappingURL=index.js.map