import { useCallback as e, useEffect as t, useMemo as n, useRef as r, useState as i } from "react";
import { cn as a } from "@ryuzaki13/react-foundation-lib/utils";
import { jsx as o, jsxs as s } from "react/jsx-runtime";
//#region src/wheel-picker/lib/wheelPickerMath.ts
function c(e, t) {
	return t <= 0 || e < 0 ? 0 : e > t - 1 ? t - 1 : e;
}
function l(e, t) {
	return !Number.isFinite(e) || e <= 0 ? t : Math.max(e, t);
}
function u(e, t) {
	return Math.max(1, Math.round(e / Math.max(1, t)));
}
function d(e, t) {
	return Math.max(0, Math.round(e / 2 - t / 2));
}
function f(e, t) {
	return Math.max(0, Math.round(e * t));
}
function p(e, t, n) {
	return n <= 0 || t <= 0 ? 0 : c(Math.round(e / t), n);
}
var m = {
	wheelPicker: "GLhdJ6",
	viewport: "_2xLNL2",
	scroller: "dgX32S",
	items: "WfOtQ9",
	item: "XIdSMi",
	selectionFrame: "_--Hsbk"
}, h = 192, g = 7, _ = 96;
function v({ items: v, value: y, onChange: b, ariaLabel: x, disabled: S, className: C, minHeight: w = h, visibleRows: T = g, formatItem: E, getItemKey: D }) {
	let O = r(null), k = r(null), A = r(null), j = n(() => Math.max(0, v.findIndex((e) => Object.is(e, y))), [v, y]), [M, N] = i(0), P = l(M, w), F = u(P, T), I = d(P, F), L = r(j), R = e((e) => Math.round(e.clientHeight), []), z = e(() => {
		let e = k.current;
		if (!e || v.length === 0) return;
		let t = p(e.scrollTop, F, v.length);
		e.scrollTo({
			top: f(t, F),
			behavior: "smooth"
		}), t !== L.current && (L.current = t, b(v[t]));
	}, [
		F,
		v,
		b
	]), B = e(() => {
		A.current !== null && window.clearTimeout(A.current), A.current = window.setTimeout(() => {
			A.current = null, z();
		}, _);
	}, [z]);
	t(() => {
		let e = O.current;
		if (!e || typeof ResizeObserver > "u") return;
		let t = (e) => {
			N((t) => t === e ? t : e);
		}, n = new ResizeObserver((n) => {
			let r = n[0], i = Math.round(r?.contentRect.height ?? R(e));
			t(i);
		});
		return n.observe(e), t(R(e)), () => n.disconnect();
	}, [R]), t(() => {
		let e = k.current;
		if (!e || v.length === 0) return;
		let t = c(j, v.length);
		L.current = t, e.scrollTo({
			top: f(t, F),
			behavior: "auto"
		});
	}, [
		F,
		v.length,
		j
	]), t(() => {
		let e = k.current;
		if (!e) return;
		let t = () => {
			S || B();
		}, n = (t) => {
			S || (t.preventDefault(), e.scrollTop += t.deltaY, B());
		};
		return e.addEventListener("scroll", t, { passive: !0 }), e.addEventListener("wheel", n, { passive: !1 }), () => {
			e.removeEventListener("scroll", t), e.removeEventListener("wheel", n);
		};
	}, [
		S,
		v,
		b,
		B
	]), t(() => () => {
		A.current !== null && window.clearTimeout(A.current);
	}, []);
	let V = (e) => {
		if (S || v.length === 0 || e.key !== "ArrowUp" && e.key !== "ArrowDown") return;
		e.preventDefault();
		let t = e.key === "ArrowUp" ? -1 : 1, n = c(j + t, v.length);
		n !== j && (L.current = n, b(v[n]));
	}, H = {
		"--wheel-picker-min-height": `${w}px`,
		"--wheel-picker-viewport-height": `${P}px`,
		"--wheel-picker-item-height": `${F}px`,
		"--wheel-picker-edge-padding": `${I}px`
	};
	return /* @__PURE__ */ o("div", {
		ref: O,
		className: a(m.wheelPicker, C),
		style: H,
		children: /* @__PURE__ */ s("div", {
			className: m.viewport,
			children: [/* @__PURE__ */ o("div", {
				ref: k,
				className: m.scroller,
				role: "listbox",
				tabIndex: S ? -1 : 0,
				"aria-label": x,
				"aria-disabled": S || void 0,
				onKeyDown: V,
				children: /* @__PURE__ */ o("div", {
					className: m.items,
					children: v.map((e, t) => {
						let n = t === j;
						return /* @__PURE__ */ o("button", {
							type: "button",
							role: "option",
							"aria-selected": n,
							className: m.item,
							"data-selected": n ? "" : void 0,
							disabled: S,
							onClick: () => {
								L.current = t, b(e);
							},
							children: E?.(e) ?? String(e)
						}, D?.(e, t) ?? t);
					})
				})
			}), /* @__PURE__ */ o("div", {
				className: m.selectionFrame,
				"aria-hidden": "true"
			})]
		})
	});
}
//#endregion
export { v as t };

//# sourceMappingURL=wheel-picker-FS58cRsQ.js.map