import { t as e } from "../../utils-B7XIe3qq.js";
import { t } from "../../chevron-right-DuHRAn8w.js";
import * as n from "react";
import { jsx as r, jsxs as i } from "react/jsx-runtime";
//#region src/react/primitives/tree-view.tsx
function a({ className: t, data: a, onSelect: s, onDoubleClick: c, onExpand: l, selectedId: u, defaultExpandedIds: d, searchable: f = !1, itemClassName: p, ...m }) {
	let [h, g] = n.useState(() => new Set(d ?? [])), [_, v] = n.useState(null), y = u === void 0 ? _ : u, [b, x] = n.useState(""), [S, C] = n.useState(/* @__PURE__ */ new Set()), [w, T] = n.useState(/* @__PURE__ */ new Map()), E = n.useCallback((e) => {
		h.has(e.id) ? g((t) => {
			let n = new Set(t);
			return n.delete(e.id), n;
		}) : e.lazyLoad !== void 0 && (e.children === void 0 || e.children.length === 0) && !w.has(e.id) && !S.has(e.id) ? (C((t) => new Set(t).add(e.id)), e.lazyLoad().then((t) => {
			T((n) => new Map(n).set(e.id, t)), g((t) => new Set(t).add(e.id)), l?.(e);
		}).finally(() => {
			C((t) => {
				let n = new Set(t);
				return n.delete(e.id), n;
			});
		})) : (g((t) => new Set(t).add(e.id)), l?.(e));
	}, [
		h,
		l,
		w
	]), D = n.useCallback((e) => {
		u === void 0 && v(e.id), s?.(e);
	}, [u, s]), O = n.useCallback((e, t) => t === "" || e.label.toLowerCase().includes(t) ? !0 : e.children === void 0 ? !1 : e.children.some((e) => O(e, t)), []), k = b.toLowerCase();
	return /* @__PURE__ */ i("div", {
		"data-slot": "tree-view",
		className: e("flex flex-col gap-1", t),
		...m,
		children: [f && /* @__PURE__ */ r("div", {
			className: "px-1 pb-1",
			children: /* @__PURE__ */ r("input", {
				type: "text",
				placeholder: "Filter...",
				value: b,
				onChange: (e) => {
					x(e.target.value);
				},
				"aria-label": "Filter tree items",
				className: "h-8 w-full rounded-lg border border-border-glass bg-glass px-3 text-xs text-foreground placeholder:text-foreground-muted backdrop-blur-[20px] outline-none focus:border-primary focus:ring-1 focus:ring-primary/20"
			})
		}), /* @__PURE__ */ r("div", {
			role: "tree",
			"aria-label": "Tree navigation",
			className: "flex flex-col",
			children: a.map((e) => O(e, k) ? /* @__PURE__ */ r(o, {
				node: e,
				depth: 0,
				expanded: h,
				selectedId: y,
				onToggle: E,
				onSelect: D,
				onDoubleClick: c,
				matchesFilter: O,
				filterQuery: k,
				loadingNodes: S,
				lazyChildren: w,
				itemClassName: p
			}, e.id) : null)
		})]
	});
}
function o({ node: n, depth: a, expanded: s, selectedId: c, onToggle: l, onSelect: u, onDoubleClick: d, matchesFilter: f, filterQuery: p, loadingNodes: m, lazyChildren: h, itemClassName: g }) {
	let _ = h.get(n.id) ?? n.children, v = _ !== void 0 && _.length > 0 || n.lazyLoad !== void 0, y = n.expandable !== !1 && v, b = s.has(n.id), x = c === n.id, S = m.has(n.id);
	return /* @__PURE__ */ i("div", {
		"data-slot": "tree-item",
		"data-node-id": n.id,
		children: [/* @__PURE__ */ i("div", {
			role: "treeitem",
			"aria-expanded": y ? b : void 0,
			"aria-selected": x,
			"aria-level": a + 1,
			tabIndex: x ? 0 : -1,
			className: e("flex items-center gap-1 rounded-lg px-2 py-1 text-sm cursor-pointer select-none transition-colors", "text-foreground hover:bg-glass-hover", x && "bg-primary/10 text-primary", g),
			style: { paddingLeft: `${String(a * 16 + 8)}px` },
			onClick: () => {
				u(n), y && l(n);
			},
			onDoubleClick: () => {
				d?.(n);
			},
			onKeyDown: (e) => {
				(e.key === "Enter" || e.key === " ") && (e.preventDefault(), u(n)), e.key === "ArrowRight" && y && !b && (e.preventDefault(), l(n)), e.key === "ArrowLeft" && y && b && (e.preventDefault(), l(n));
			},
			children: [
				/* @__PURE__ */ r("span", {
					className: "flex size-4 shrink-0 items-center justify-center",
					children: y ? S ? /* @__PURE__ */ r("span", { className: "size-3 animate-spin rounded-full border-2 border-foreground-muted border-t-transparent" }) : /* @__PURE__ */ r(t, { className: e("size-3.5 text-foreground-muted transition-transform duration-200", b && "rotate-90") }) : null
				}),
				n.icon !== void 0 && /* @__PURE__ */ r("span", {
					className: "shrink-0",
					children: n.icon
				}),
				/* @__PURE__ */ r("span", {
					className: "truncate",
					children: n.label
				})
			]
		}), b && _ !== void 0 && _.length > 0 && /* @__PURE__ */ r("div", {
			role: "group",
			children: _.map((e) => f(e, p) ? /* @__PURE__ */ r(o, {
				node: e,
				depth: a + 1,
				expanded: s,
				selectedId: c,
				onToggle: l,
				onSelect: u,
				onDoubleClick: d,
				matchesFilter: f,
				filterQuery: p,
				loadingNodes: m,
				lazyChildren: h,
				itemClassName: g
			}, e.id) : null)
		})]
	});
}
//#endregion
export { o as TreeItem, a as TreeView };

//# sourceMappingURL=tree-view.js.map