import { t as e } from "../chunks/ui.module-DHVTOqZ0.js";
import { useEffect as t, useId as n, useLayoutEffect as r, useMemo as i, useRef as a, useState as o } from "react";
import { cn as s, findFirstEnabledIndex as c, findLastEnabledIndex as l, findNextEnabledIndex as u, handleKeyboardActivation as d } from "@ryuzaki13/react-foundation-lib/utils";
import { jsx as f, jsxs as p } from "react/jsx-runtime";
import { CheckIcon as m } from "lucide-react";
//#region src/listbox/Listbox.tsx
var h = (e) => e.disabled === !0;
function g(e, t, n) {
	if (n) {
		if (Array.isArray(t) && t.length > 0) {
			let n = e.findIndex((e) => t.includes(e.value) && !e.disabled);
			if (n >= 0) return n;
		}
		return c(e, h);
	}
	if (t !== void 0) {
		let n = e.findIndex((e) => e.value === t && !e.disabled);
		if (n >= 0) return n;
	}
	return c(e, h);
}
function _(_) {
	let { value: v, defaultValue: y, multiple: b, disabled: x, focusOnMount: S, renderItem: C, getKey: w, options: T, onChange: E, id: D, ...O } = _, k = v !== void 0, A = n(), j = D ?? `${A}-listbox`, [M, N] = o(y), P = a(null), [F, I] = o(() => g(T, v ?? y, b)), L = a(!1), R = i(() => (k ? v : M) ?? (b ? [] : null), [
		M,
		k,
		b,
		v
	]), z = F, B = (e) => {
		if (x || e.disabled) return;
		let t;
		if (_.multiple) {
			let n = Array.isArray(R) ? R : [];
			t = n.includes(e.value) ? n.filter((t) => t !== e.value) : [...n, e.value], _.onChange?.(t, e);
		} else t = e.value, _.onChange?.(t, e);
		k || N(t);
	};
	return t(() => {
		if (!L.current) return;
		L.current = !1;
		let e = P.current?.querySelectorAll("[role=\"option\"]")[z];
		e && e.scrollIntoView({ block: "nearest" });
	}, [z]), r(() => {
		S && P.current?.focus();
	}, [S]), /* @__PURE__ */ f("ul", {
		ref: P,
		id: j,
		role: "listbox",
		"aria-multiselectable": b || void 0,
		"aria-activedescendant": z >= 0 ? `${j}-option-${z}` : void 0,
		onKeyDown: (e) => {
			if (!x) {
				if (e.key === "ArrowDown") e.preventDefault(), L.current = !0, I((e) => u(T, e, 1, h));
				else if (e.key === "ArrowUp") e.preventDefault(), L.current = !0, I((e) => u(T, e, -1, h));
				else if (e.key === "Home") e.preventDefault(), L.current = !0, I(c(T, h));
				else if (e.key === "End") e.preventDefault(), L.current = !0, I(l(T, h));
				else if (d(e, () => {
					let e = T[z];
					e && B(e);
				})) return;
			}
		},
		...O,
		"aria-disabled": x || void 0,
		tabIndex: x ? -1 : 0,
		className: s(e.uiPanel, "scrollable", x && e.disabled),
		children: T.map((t, n) => {
			let r = b ? Array.isArray(R) && R.includes(t.value) : R === t.value, i = n === z;
			return /* @__PURE__ */ p("li", {
				id: `${j}-option-${n}`,
				role: "option",
				"aria-selected": r,
				"aria-disabled": x || t.disabled || void 0,
				onMouseDown: (e) => {
					e.preventDefault(), !(x || t.disabled) && (I(n), B(t));
				},
				tabIndex: -1,
				className: s(e.uiPopupOption, (x || t.disabled) && e.disabled, i && e.uiPopupOptionActive, r && e.selected),
				children: [f(r ? m : "span", { className: e.uiPopupOptionIcon }), /* @__PURE__ */ f("div", { children: C ? C(t, r, i) : t.label ?? String(t.value) })]
			}, w ? w(t, n) : n);
		})
	});
}
//#endregion
export { _ as Listbox };

//# sourceMappingURL=index.js.map