import { ScrollArea } from "../../../components/ui/scroll-area.js";
import { useCodeExplorerContext } from "../providers/CodeExplorerProvider.js";
import { CodeExplorerTreeNode } from "./CodeExplorerTreeNode.js";
import { useMemo } from "react";
import { jsx } from "react/jsx-runtime";
function filterTree(e, i) {
	if (!i.trim()) return e;
	let a = i.trim().toLowerCase();
	return e.map((e) => filterNode(e, a)).filter((e) => e !== null);
}
function filterNode(e, i) {
	if (e.type === "file") return e.name.toLowerCase().includes(i) ? e : null;
	let a = e.children ? e.children.map((e) => filterNode(e, i)).filter((e) => e !== null) : [];
	return e.name.toLowerCase().includes(i) || a.length > 0 ? {
		...e,
		children: a
	} : null;
}
function CodeExplorerTree({ nodes: o, searchQuery: s = "", onSelectFile: c, onClearSelection: l, onCloseFile: u, selectedFileId: d }) {
	let f = useMemo(() => filterTree(o, s), [o, s]), { expandedFolders: p, toggleFolder: m } = useCodeExplorerContext();
	return /* @__PURE__ */ jsx(ScrollArea, {
		className: "h-full w-full",
		children: /* @__PURE__ */ jsx("div", {
			className: "w-full py-1",
			role: "tree",
			"aria-label": "File tree",
			children: f.length === 0 ? /* @__PURE__ */ jsx("p", {
				className: "px-2 py-4 text-sm text-muted-foreground",
				children: "No files match"
			}) : f.map((e) => /* @__PURE__ */ jsx(CodeExplorerTreeNode, {
				node: e,
				depth: 0,
				onSelectFile: c,
				onClearSelection: l,
				onCloseFile: u,
				selectedFileId: d,
				expandedFolders: p,
				toggleFolder: m
			}, e.id))
		})
	});
}
export { CodeExplorerTree };
