import { t as e } from "./sanitize-CDxuPfMB.js";
import { n as t } from "./utils-C_mWfcFX.js";
import { r as n, t as r } from "./a11y-CuHKgVSx.js";
import { n as i, t as a } from "./keyboard-wOEoCCWj.js";
//#region src/components/tree-view/lazy.ts
async function o(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
function s(e) {
	if (typeof e == "string") {
		let t = document.querySelector(e);
		if (!t) throw Error(`TreeView container not found: ${e}`);
		return t;
	}
	return e;
}
var c = class {
	container;
	config;
	data = [];
	selectedNode = null;
	focusedNode = null;
	wrapperEl = null;
	highlightQuery = "";
	liveRegion = null;
	treeId;
	labelId;
	flatNodes = [];
	typeAhead = null;
	constructor(e) {
		this.container = s(e.container), this.config = e, this.treeId = t("cmm-tree"), this.labelId = t("cmm-tree-label"), e.data !== void 0 && (this.data = e.data), this.liveRegion = n("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.innerHTML = "", 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), this.typeAhead = i({ items: this.flatNodes.map((e) => ({ label: e.node.label })) });
	}
	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, o = r.expanded === !0;
		switch (e.key) {
			case a.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 a.ARROW_UP:
				if (e.preventDefault(), n > 0) {
					let e = this.flatNodes[n - 1];
					e !== void 0 && this.focusNode(e.node);
				}
				break;
			case a.ARROW_RIGHT:
				if (e.preventDefault(), i) {
					if (!o) 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 a.ARROW_LEFT:
				e.preventDefault(), o && i ? this.handleToggle(r) : t.parent !== null && this.focusNode(t.parent);
				break;
			case a.HOME: {
				e.preventDefault();
				let t = this.flatNodes[0];
				t !== void 0 && this.focusNode(t.node);
				break;
			}
			case a.END: {
				e.preventDefault();
				let t = this.flatNodes[this.flatNodes.length - 1];
				t !== void 0 && this.focusNode(t.node);
				break;
			}
			case a.ENTER:
			case a.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, i = e.label;
		t && (i += n ? ", expanded" : ", collapsed"), r(i, "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.innerHTML = "";
		let e = document.createElement("div");
		e.className = "cmm-tree-view", this.wrapperEl = e;
		let n = document.createElement("span");
		if (n.id = this.labelId, n.className = "cmm-sr-only", n.textContent = "Tree navigation. Use arrow keys to navigate. Press Enter or Space to select.", e.appendChild(n), this.config.searchable === !0) {
			let n = document.createElement("div");
			n.className = "cmm-tree-view__search";
			let r = document.createElement("label");
			r.className = "cmm-sr-only";
			let i = t("cmm-tree-search");
			r.setAttribute("for", i), r.textContent = "Filter tree", n.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();
			}), n.appendChild(a), e.appendChild(n);
		}
		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.innerHTML = "", 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(e, t, n, r) {
		let i = document.createElement("div");
		i.className = "cmm-tree-node", i.dataset.nodeId = e.id, e.expanded === !0 && i.classList.add("cmm-tree-node--expanded"), this.selectedNode !== null && this.selectedNode.id === e.id && i.classList.add("cmm-tree-node--selected");
		let a = document.createElement("div");
		a.className = "cmm-tree-node__row", a.setAttribute("role", "treeitem"), a.setAttribute("aria-level", String(t + 1)), a.setAttribute("aria-setsize", String(r)), a.setAttribute("aria-posinset", String(n)), a.style.paddingLeft = `${String(t * 16 + 8)}px`;
		let o = this.focusedNode !== null && this.focusedNode.id === e.id;
		a.setAttribute("tabindex", o ? "0" : "-1");
		let s = e.children !== void 0 || e.lazyLoad !== void 0, c = e.expandable !== !1 && s;
		c && a.setAttribute("aria-expanded", e.expanded === !0 ? "true" : "false"), this.selectedNode !== null && this.selectedNode.id === e.id ? a.setAttribute("aria-selected", "true") : a.setAttribute("aria-selected", "false");
		let l = document.createElement("span");
		if (l.className = "cmm-tree-node__toggle", l.setAttribute("aria-hidden", "true"), c ? (l.textContent = e.expanded === !0 ? "▾" : "▸", l.addEventListener("click", (t) => {
			t.stopPropagation(), this.handleToggle(e);
		})) : (l.textContent = " ", l.style.visibility = "hidden"), a.appendChild(l), e.icon !== void 0) {
			let t = document.createElement("span");
			t.className = "cmm-tree-node__icon", t.setAttribute("aria-hidden", "true"), t.textContent = e.icon, a.appendChild(t);
		}
		let u = document.createElement("span");
		if (u.className = "cmm-tree-node__label", this.highlightQuery !== "" && e.label.toLowerCase().includes(this.highlightQuery) ? u.innerHTML = this.highlightText(e.label, this.highlightQuery) : u.textContent = e.label, a.appendChild(u), a.addEventListener("click", () => {
			this.selectNode(e);
		}), a.addEventListener("dblclick", () => {
			this.config.onDoubleClick?.(e);
		}), i.appendChild(a), e.expanded === !0 && e.children !== void 0 && e.children.length > 0) {
			let n = document.createElement("div");
			n.className = "cmm-tree-node__children", n.setAttribute("role", "group");
			let r = e.children.filter((e) => this.highlightQuery === "" || this.nodeMatchesSearch(e, this.highlightQuery)), a = 0;
			for (let i of e.children) this.highlightQuery !== "" && !this.nodeMatchesSearch(i, this.highlightQuery) || (a++, n.appendChild(this.buildNodeElement(i, t + 1, a, r.length)));
			i.appendChild(n);
		}
		return i;
	}
	async handleToggle(e) {
		e.expanded === !0 ? (e.expanded = !1, this.announceUpdate(`${e.label} collapsed`)) : (e.expanded = !0, e.lazyLoad !== void 0 && (e.children === void 0 || e.children.length === 0) && (this.announceUpdate(`Loading ${e.label}`), await o(e)), this.announceUpdate(`${e.label} expanded`), this.config.onExpand?.(e)), this.render();
	}
	highlightText(t, n) {
		let r = t.toLowerCase().indexOf(n);
		if (r < 0) return e(t);
		let i = t.slice(0, r), a = t.slice(r, r + n.length), o = t.slice(r + n.length);
		return `${e(i)}<mark class="cmm-tree-node__highlight">${e(a)}</mark>${e(o)}`;
	}
};
//#endregion
export { c as t };

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