import { i as e } from "../../sanitize-CDxuPfMB.js";
import { n as t, t as n } from "../../utils-C_mWfcFX.js";
//#region src/visualization/file-tree/index.ts
var r = "<svg viewBox=\"0 0 16 16\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n  <path d=\"M1.5 2.5h4.25l1.25 1.5h7.5v9.5h-13z\" stroke=\"currentColor\" stroke-width=\"1.2\" stroke-linejoin=\"round\"/>\n</svg>", i = "<svg viewBox=\"0 0 16 16\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n  <path d=\"M1.5 2.5h4.25l1.25 1.5h7.5v2H3.5l-2 6.5z\" stroke=\"currentColor\" stroke-width=\"1.2\" stroke-linejoin=\"round\"/>\n  <path d=\"M3.5 6.5h11l-2 7h-11z\" stroke=\"currentColor\" stroke-width=\"1.2\" stroke-linejoin=\"round\"/>\n</svg>", a = "<svg viewBox=\"0 0 16 16\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n  <path d=\"M3 1.5h6.5L13 5v9.5H3z\" stroke=\"currentColor\" stroke-width=\"1.2\" stroke-linejoin=\"round\"/>\n  <path d=\"M9.5 1.5v3.5H13\" stroke=\"currentColor\" stroke-width=\"1.2\" stroke-linejoin=\"round\"/>\n</svg>", o = "<svg viewBox=\"0 0 16 16\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n  <path d=\"M6 4l4 4-4 4\" stroke=\"currentColor\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n</svg>", s = class {
	id;
	container;
	config;
	indent;
	showIcons;
	data;
	wrapperEl = null;
	selectedIds = /* @__PURE__ */ new Set();
	flatNodes = [];
	focusedIndex = -1;
	expandedIds = /* @__PURE__ */ new Set();
	dragSourceNode = null;
	constructor(e) {
		this.id = t("cmm-file-tree"), this.container = n(e.container), this.config = e, this.data = this.cloneData(e.data), this.indent = e.indent ?? 16, this.showIcons = e.showIcons !== !1, this.initExpandedState(this.data), this.render();
	}
	update(e) {
		this.data = this.cloneData(e), this.initExpandedState(this.data), this.render();
	}
	getSelected() {
		let e = [];
		return this.collectSelected(this.data, e), e;
	}
	expandAll() {
		this.setAllExpanded(this.data, !0), this.render();
	}
	collapseAll() {
		this.setAllExpanded(this.data, !1), this.render();
	}
	destroy() {
		this.container.innerHTML = "", this.wrapperEl = null, this.flatNodes = [], this.selectedIds.clear(), this.expandedIds.clear();
	}
	cloneData(e) {
		return e.map((e) => this.cloneNode(e));
	}
	cloneNode(e) {
		let t = {
			id: e.id,
			name: e.name,
			type: e.type
		};
		return e.icon !== void 0 && (t.icon = e.icon), e.data !== void 0 && (t.data = { ...e.data }), e.expanded !== void 0 && (t.expanded = e.expanded), e.selected !== void 0 && (t.selected = e.selected), e.children !== void 0 && (t.children = e.children.map((e) => this.cloneNode(e))), t;
	}
	initExpandedState(e) {
		for (let t of e) t.type === "folder" && t.expanded === !0 && this.expandedIds.add(t.id), t.selected === !0 && this.selectedIds.add(t.id), t.children !== void 0 && this.initExpandedState(t.children);
	}
	setAllExpanded(e, t) {
		for (let n of e) n.type === "folder" && (t ? this.expandedIds.add(n.id) : this.expandedIds.delete(n.id)), n.children !== void 0 && this.setAllExpanded(n.children, t);
	}
	collectSelected(e, t) {
		for (let n of e) this.selectedIds.has(n.id) && t.push(n), n.children !== void 0 && this.collectSelected(n.children, t);
	}
	render() {
		this.container.innerHTML = "", this.flatNodes = [];
		let e = document.createElement("div");
		if (e.className = "cmm-file-tree", e.id = this.id, e.setAttribute("role", "tree"), e.setAttribute("tabindex", "0"), this.config.className !== void 0 && e.classList.add(this.config.className), this.data.length === 0) {
			let t = document.createElement("div");
			t.className = "cmm-file-tree__empty", t.textContent = "No files", e.appendChild(t);
		} else {
			let t = document.createElement("ul");
			t.className = "cmm-file-tree__list cmm-file-tree__list--root", t.setAttribute("role", "group"), this.renderNodes(this.data, t, 0), e.appendChild(t);
		}
		e.addEventListener("keydown", this.handleKeyDown.bind(this)), this.container.appendChild(e), this.wrapperEl = e;
	}
	renderNodes(t, n, s) {
		for (let c of t) {
			let t = c.type === "folder", l = this.expandedIds.has(c.id), u = this.selectedIds.has(c.id), d = document.createElement("li");
			d.className = "cmm-file-tree__item", d.setAttribute("role", "treeitem"), d.setAttribute("aria-expanded", t ? String(l) : "false"), d.setAttribute("data-node-id", c.id);
			let f = document.createElement("div");
			f.className = "cmm-file-tree__row", u && f.classList.add("cmm-file-tree__row--selected"), f.style.paddingLeft = `${String(s * this.indent + 8)}px`;
			let p = document.createElement("span");
			if (p.className = "cmm-file-tree__chevron", t ? (p.innerHTML = o, l && p.classList.add("cmm-file-tree__chevron--expanded")) : p.classList.add("cmm-file-tree__chevron--hidden"), f.appendChild(p), this.showIcons) {
				let n = document.createElement("span");
				n.className = "cmm-file-tree__icon", c.icon === void 0 ? t ? (n.classList.add("cmm-file-tree__icon--folder"), n.innerHTML = l ? i : r) : n.innerHTML = a : e(n, c.icon), f.appendChild(n);
			}
			let m = document.createElement("span");
			m.className = "cmm-file-tree__label", m.textContent = c.name, f.appendChild(m), f.addEventListener("click", (e) => {
				this.handleNodeClick(c, e);
			}), this.config.draggable === !0 && (f.draggable = !0, f.addEventListener("dragstart", (e) => {
				this.dragSourceNode = c, e.dataTransfer !== null && (e.dataTransfer.effectAllowed = "move", e.dataTransfer.setData("text/plain", c.id));
			}), f.addEventListener("dragover", (e) => {
				this.dragSourceNode !== null && this.dragSourceNode.id !== c.id && c.type === "folder" && (e.preventDefault(), e.dataTransfer !== null && (e.dataTransfer.dropEffect = "move"), f.classList.add("cmm-file-tree__row--drag-over"));
			}), f.addEventListener("dragleave", () => {
				f.classList.remove("cmm-file-tree__row--drag-over");
			}), f.addEventListener("drop", (e) => {
				e.preventDefault(), f.classList.remove("cmm-file-tree__row--drag-over"), this.dragSourceNode !== null && this.dragSourceNode.id !== c.id && c.type === "folder" && this.config.onDrop !== void 0 && this.config.onDrop(this.dragSourceNode, c), this.dragSourceNode = null;
			}), f.addEventListener("dragend", () => {
				this.dragSourceNode = null;
			})), d.appendChild(f);
			let h = this.flatNodes.length;
			if (this.flatNodes.push({
				node: c,
				depth: s,
				rowEl: f
			}), h === this.focusedIndex && f.classList.add("cmm-file-tree__row--focused"), t && c.children !== void 0 && c.children.length > 0) {
				let e = document.createElement("ul");
				e.className = "cmm-file-tree__children", e.setAttribute("role", "group"), l || e.classList.add("cmm-file-tree__children--collapsed"), this.renderNodes(c.children, e, s + 1), d.appendChild(e);
			}
			n.appendChild(d);
		}
	}
	handleNodeClick(e, t) {
		e.type === "folder" && (this.expandedIds.has(e.id) ? this.expandedIds.delete(e.id) : this.expandedIds.add(e.id), this.config.onExpand !== void 0 && this.config.onExpand(e)), this.config.selectable !== !1 && (this.config.multiSelect === !0 && (t.ctrlKey || t.metaKey) ? this.selectedIds.has(e.id) ? this.selectedIds.delete(e.id) : this.selectedIds.add(e.id) : (this.selectedIds.clear(), this.selectedIds.add(e.id)), this.config.onSelect !== void 0 && this.config.onSelect(e));
		let n = this.flatNodes.findIndex((t) => t.node.id === e.id);
		n >= 0 && (this.focusedIndex = n), this.render();
	}
	handleKeyDown(e) {
		if (this.flatNodes.length !== 0) switch (e.key) {
			case "ArrowDown":
				e.preventDefault(), this.focusedIndex = Math.min(this.focusedIndex + 1, this.flatNodes.length - 1), this.render(), this.scrollFocusedIntoView();
				break;
			case "ArrowUp":
				e.preventDefault(), this.focusedIndex = Math.max(this.focusedIndex - 1, 0), this.render(), this.scrollFocusedIntoView();
				break;
			case "ArrowRight": {
				e.preventDefault();
				let t = this.flatNodes[this.focusedIndex];
				t !== void 0 && t.node.type === "folder" && (this.expandedIds.has(t.node.id) ? this.focusedIndex < this.flatNodes.length - 1 && (this.focusedIndex += 1, this.render()) : (this.expandedIds.add(t.node.id), this.config.onExpand !== void 0 && this.config.onExpand(t.node), this.render()));
				break;
			}
			case "ArrowLeft": {
				e.preventDefault();
				let t = this.flatNodes[this.focusedIndex];
				if (t?.node.type === "folder" && this.expandedIds.has(t.node.id)) this.expandedIds.delete(t.node.id), this.config.onExpand !== void 0 && this.config.onExpand(t.node), this.render();
				else if (t !== void 0 && t.depth > 0) for (let e = this.focusedIndex - 1; e >= 0; e--) {
					let n = this.flatNodes[e];
					if (n !== void 0 && n.depth < t.depth) {
						this.focusedIndex = e, this.render();
						break;
					}
				}
				break;
			}
			case "Enter":
			case " ": {
				e.preventDefault();
				let t = this.flatNodes[this.focusedIndex];
				t !== void 0 && this.handleNodeClick(t.node, new MouseEvent("click"));
				break;
			}
		}
	}
	scrollFocusedIntoView() {
		if (this.wrapperEl === null || this.focusedIndex < 0) return;
		let e = this.flatNodes[this.focusedIndex];
		e !== void 0 && e.rowEl.scrollIntoView({ block: "nearest" });
	}
};
//#endregion
export { s as FileTree };

//# sourceMappingURL=index.js.map