import { a as e, r as t } from "./sanitize-uWpY18N9.js";
import { n } from "./utils-CSEWuKHo.js";
import { r, t as i } from "./a11y-ZYZ3T617.js";
import { n as a, t as o } from "./keyboard-BH4fCxA0.js";
//#region src/components/tree-view/lazy.ts
async function s(e) {
	if (e.children !== void 0 && e.children.length > 0) return e.children;
	if (e.lazyLoad === void 0) return [];
	let t = await e.lazyLoad();
	return e.children = t, t;
}
//#endregion
//#region src/components/tree-view/index.ts
var c = "http://www.w3.org/2000/svg";
function l(e, t) {
	let n = document.createElementNS(c, e);
	for (let [e, r] of Object.entries(t)) n.setAttribute(e, r);
	return n;
}
function u() {
	let e = l("svg", {
		viewBox: "0 0 24 24",
		fill: "none",
		stroke: "currentColor",
		"stroke-width": "2",
		"stroke-linecap": "round",
		"stroke-linejoin": "round",
		"aria-hidden": "true"
	});
	return e.classList.add("cmm-tree-node__chevron"), e.appendChild(l("path", { d: "m9 18 6-6-6-6" })), e;
}
function d(e) {
	if (typeof e == "string") {
		let t = document.querySelector(e);
		if (!t) throw Error(`TreeView container not found: ${e}`);
		return t;
	}
	return e;
}
var f = class {
	container;
	config;
	data = [];
	selectedNode = null;
	focusedNode = null;
	wrapperEl = null;
	highlightQuery = "";
	liveRegion = null;
	loadingNodes = /* @__PURE__ */ new Set();
	treeId;
	labelId;
	flatNodes = [];
	typeAhead = null;
	constructor(e) {
		this.container = d(e.container), this.config = e, this.treeId = n("cmm-tree"), this.labelId = n("cmm-tree-label"), e.data !== void 0 && (this.data = e.data), this.liveRegion = r("polite"), this.render();
	}
	setData(e) {
		this.data = e, this.selectedNode = null, this.focusedNode = null, this.render(), this.announceUpdate(`Tree updated with ${String(this.countNodes(e))} items`);
	}
	expandAll() {
		this.setExpandedRecursive(this.data, !0), this.render(), this.announceUpdate("All nodes expanded");
	}
	collapseAll() {
		this.setExpandedRecursive(this.data, !1), this.render(), this.announceUpdate("All nodes collapsed");
	}
	expandNode(e) {
		let t = this.findNodeById(this.data, e);
		t && (t.expanded = !0, this.render(), this.announceUpdate(`${t.label} expanded`));
	}
	collapseNode(e) {
		let t = this.findNodeById(this.data, e);
		t && (t.expanded = !1, this.render(), this.announceUpdate(`${t.label} collapsed`));
	}
	highlight(e) {
		this.highlightQuery = e.toLowerCase(), this.render();
	}
	getSelectedNode() {
		return this.selectedNode;
	}
	destroy() {
		this.container.replaceChildren(), this.wrapperEl = null, this.selectedNode = null, this.focusedNode = null, this.liveRegion?.destroy(), this.liveRegion = null;
	}
	announceUpdate(e) {
		this.liveRegion?.announce(e);
	}
	countNodes(e) {
		let t = 0;
		for (let n of e) t++, n.children !== void 0 && (t += this.countNodes(n.children));
		return t;
	}
	setExpandedRecursive(e, t) {
		for (let n of e) n.expandable !== !1 && (n.children !== void 0 || n.lazyLoad !== void 0) && (n.expanded = t), n.children !== void 0 && this.setExpandedRecursive(n.children, t);
	}
	findNodeById(e, t) {
		for (let n of e) {
			if (n.id === t) return n;
			if (n.children !== void 0) {
				let e = this.findNodeById(n.children, t);
				if (e) return e;
			}
		}
		return null;
	}
	nodeMatchesSearch(e, t) {
		if (t === "" || e.label.toLowerCase().includes(t)) return !0;
		if (e.children !== void 0) {
			for (let n of e.children) if (this.nodeMatchesSearch(n, t)) return !0;
		}
		return !1;
	}
	buildFlatNodes() {
		this.flatNodes = [], this.flattenNodes(this.data, 0, null);
		let e = this.flatNodes.map((e) => ({ label: e.node.label }));
		this.typeAhead = a({ items: e });
	}
	flattenNodes(e, t, n) {
		for (let r of e) this.highlightQuery !== "" && !this.nodeMatchesSearch(r, this.highlightQuery) || (this.flatNodes.push({
			node: r,
			depth: t,
			parent: n,
			index: this.flatNodes.length
		}), r.expanded === !0 && r.children !== void 0 && r.children.length > 0 && this.flattenNodes(r.children, t + 1, r));
	}
	getFlatNode(e) {
		return this.flatNodes.find((t) => t.node.id === e.id) ?? null;
	}
	getSiblingsAtLevel(e) {
		let t = this.getFlatNode(e);
		if (t === null) return [];
		let n = t.parent;
		return n === null ? this.data.filter((e) => this.highlightQuery === "" || this.nodeMatchesSearch(e, this.highlightQuery)) : (n.children ?? []).filter((e) => this.highlightQuery === "" || this.nodeMatchesSearch(e, this.highlightQuery));
	}
	handleKeyDown(e) {
		if (this.flatNodes.length === 0 || this.focusedNode === null) return;
		let t = this.getFlatNode(this.focusedNode);
		if (t === null) return;
		let n = t.index, r = this.focusedNode, i = r.children !== void 0 || r.lazyLoad !== void 0, a = r.expanded === !0;
		switch (e.key) {
			case o.ARROW_DOWN:
				if (e.preventDefault(), n < this.flatNodes.length - 1) {
					let e = this.flatNodes[n + 1];
					e !== void 0 && this.focusNode(e.node);
				}
				break;
			case o.ARROW_UP:
				if (e.preventDefault(), n > 0) {
					let e = this.flatNodes[n - 1];
					e !== void 0 && this.focusNode(e.node);
				}
				break;
			case o.ARROW_RIGHT:
				if (e.preventDefault(), i) {
					if (!a) this.handleToggle(r);
					else if (r.children !== void 0 && r.children.length > 0) {
						let e = r.children[0];
						e !== void 0 && this.focusNode(e);
					}
				}
				break;
			case o.ARROW_LEFT:
				e.preventDefault(), a && i ? this.handleToggle(r) : t.parent !== null && this.focusNode(t.parent);
				break;
			case o.HOME: {
				e.preventDefault();
				let t = this.flatNodes[0];
				t !== void 0 && this.focusNode(t.node);
				break;
			}
			case o.END: {
				e.preventDefault();
				let t = this.flatNodes[this.flatNodes.length - 1];
				t !== void 0 && this.focusNode(t.node);
				break;
			}
			case o.ENTER:
			case o.SPACE:
				e.preventDefault(), this.selectNode(r);
				break;
			case "*": {
				e.preventDefault();
				let t = this.getSiblingsAtLevel(r);
				for (let e of t) e.expandable !== !1 && e.children !== void 0 && (e.expanded = !0);
				this.render(), this.announceUpdate("All siblings expanded");
				break;
			}
			default:
				if (e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey) {
					let t = this.typeAhead?.handleKey(e.key);
					if (t !== void 0 && t >= 0) {
						let e = this.flatNodes[t];
						e !== void 0 && this.focusNode(e.node);
					}
				}
				break;
		}
	}
	focusNode(e) {
		this.focusedNode = e, this.updateFocusState();
		let t = e.children !== void 0 || e.lazyLoad !== void 0, n = e.expanded === !0, r = e.label;
		t && (r += n ? ", expanded" : ", collapsed"), i(r, "polite");
	}
	selectNode(e) {
		this.selectedNode = e, this.focusedNode = e, this.config.onSelect?.(e), this.render(), this.announceUpdate(`${e.label} selected`);
	}
	updateFocusState() {
		if (!this.wrapperEl) return;
		let e = this.wrapperEl.querySelectorAll("[role=\"treeitem\"]");
		for (let t of e) t.setAttribute("tabindex", "-1");
		if (this.focusedNode !== null) {
			let e = this.wrapperEl.querySelector(`[data-node-id="${this.focusedNode.id}"] > [role="treeitem"]`);
			e !== null && (e.setAttribute("tabindex", "0"), e.focus());
		}
	}
	render() {
		this.container.replaceChildren();
		let e = document.createElement("div");
		e.className = "cmm-tree-view", this.wrapperEl = e;
		let t = document.createElement("span");
		if (t.id = this.labelId, t.className = "cmm-sr-only", t.textContent = "Tree navigation. Use arrow keys to navigate. Press Enter or Space to select.", e.appendChild(t), this.config.searchable === !0) {
			let t = document.createElement("div");
			t.className = "cmm-tree-view__search";
			let r = document.createElement("label");
			r.className = "cmm-sr-only";
			let i = n("cmm-tree-search");
			r.setAttribute("for", i), r.textContent = "Filter tree", t.appendChild(r);
			let a = document.createElement("input");
			a.type = "text", a.id = i, a.className = "cmm-tree-view__search-input", a.placeholder = "Filter...", a.value = this.highlightQuery, a.setAttribute("aria-label", "Filter tree items"), a.addEventListener("input", () => {
				this.highlightQuery = a.value.toLowerCase(), this.renderNodes();
			}), t.appendChild(a), e.appendChild(t);
		}
		let r = document.createElement("div");
		r.className = "cmm-tree-view__nodes", r.id = this.treeId, r.setAttribute("role", "tree"), r.setAttribute("aria-labelledby", this.labelId), r.dataset.role = "nodes", r.addEventListener("keydown", (e) => {
			this.handleKeyDown(e);
		}), e.appendChild(r), this.container.appendChild(e), this.renderNodes();
	}
	renderNodes() {
		if (!this.wrapperEl) return;
		let e = this.wrapperEl.querySelector("[data-role=\"nodes\"]");
		if (!e) return;
		e.replaceChildren(), this.buildFlatNodes();
		let t = this.data.filter((e) => this.highlightQuery === "" || this.nodeMatchesSearch(e, this.highlightQuery)), n = 0;
		for (let r of this.data) {
			if (this.highlightQuery !== "" && !this.nodeMatchesSearch(r, this.highlightQuery)) continue;
			n++;
			let i = this.buildNodeElement(r, 0, n, t.length);
			e.appendChild(i);
		}
		if (this.focusedNode === null && this.flatNodes.length > 0) {
			let e = this.flatNodes[0]?.node;
			e !== void 0 && (this.focusedNode = e);
		}
		this.updateFocusState();
	}
	buildNodeElement(t, n, r, i) {
		let a = document.createElement("div");
		a.className = "cmm-tree-node", a.dataset.nodeId = t.id, t.expanded === !0 && a.classList.add("cmm-tree-node--expanded"), this.selectedNode !== null && this.selectedNode.id === t.id && a.classList.add("cmm-tree-node--selected");
		let o = document.createElement("div");
		o.className = "cmm-tree-node__row", o.setAttribute("role", "treeitem"), o.setAttribute("aria-level", String(n + 1)), o.setAttribute("aria-setsize", String(i)), o.setAttribute("aria-posinset", String(r)), o.style.paddingLeft = `${String(n * 16 + 8)}px`;
		let s = this.focusedNode !== null && this.focusedNode.id === t.id;
		o.setAttribute("tabindex", s ? "0" : "-1");
		let c = t.children !== void 0 || t.lazyLoad !== void 0, l = t.expandable !== !1 && c;
		l && o.setAttribute("aria-expanded", t.expanded === !0 ? "true" : "false"), this.selectedNode !== null && this.selectedNode.id === t.id ? o.setAttribute("aria-selected", "true") : o.setAttribute("aria-selected", "false");
		let d = document.createElement("span");
		if (d.className = "cmm-tree-node__toggle", d.setAttribute("aria-hidden", "true"), l) {
			if (this.loadingNodes.has(t.id)) {
				let e = document.createElement("span");
				e.className = "cmm-tree-node__spinner", d.appendChild(e);
			} else d.appendChild(u());
			d.addEventListener("click", (e) => {
				e.stopPropagation(), this.handleToggle(t);
			});
		} else d.style.visibility = "hidden";
		if (o.appendChild(d), t.icon !== void 0) {
			let e = document.createElement("span");
			e.className = "cmm-tree-node__icon", e.setAttribute("aria-hidden", "true"), e.textContent = t.icon, o.appendChild(e);
		}
		let f = document.createElement("span");
		if (f.className = "cmm-tree-node__label", this.highlightQuery !== "" && t.label.toLowerCase().includes(this.highlightQuery) ? e(f, this.highlightText(t.label, this.highlightQuery)) : f.textContent = t.label, o.appendChild(f), o.addEventListener("click", () => {
			this.selectNode(t);
		}), o.addEventListener("dblclick", () => {
			this.config.onDoubleClick?.(t);
		}), a.appendChild(o), t.expanded === !0 && t.children !== void 0 && t.children.length > 0) {
			let e = document.createElement("div");
			e.className = "cmm-tree-node__children", e.setAttribute("role", "group");
			let r = t.children.filter((e) => this.highlightQuery === "" || this.nodeMatchesSearch(e, this.highlightQuery)), i = 0;
			for (let a of t.children) this.highlightQuery !== "" && !this.nodeMatchesSearch(a, this.highlightQuery) || (i++, e.appendChild(this.buildNodeElement(a, n + 1, i, r.length)));
			a.appendChild(e);
		}
		return a;
	}
	async handleToggle(e) {
		if (e.expanded === !0) e.expanded = !1, this.announceUpdate(`${e.label} collapsed`);
		else {
			if (e.expanded = !0, e.lazyLoad !== void 0 && (e.children === void 0 || e.children.length === 0)) {
				this.announceUpdate(`Loading ${e.label}`), this.loadingNodes.add(e.id), this.render();
				try {
					await s(e);
				} finally {
					this.loadingNodes.delete(e.id);
				}
			}
			this.announceUpdate(`${e.label} expanded`), this.config.onExpand?.(e);
		}
		this.render();
	}
	highlightText(e, n) {
		let r = e.toLowerCase().indexOf(n);
		if (r < 0) return t(e);
		let i = e.slice(0, r), a = e.slice(r, r + n.length), o = e.slice(r + n.length);
		return `${t(i)}<mark class="cmm-tree-node__highlight">${t(a)}</mark>${t(o)}`;
	}
};
//#endregion
export { f as t };

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