import { t as e } from "../../sanitize-CDxuPfMB.js";
import { n as t, t as n } from "../../utils-C_mWfcFX.js";
//#region src/visualization/json-path-browser/index.ts
function r(e) {
	return e === null ? "null" : Array.isArray(e) ? "array" : typeof e;
}
function i(t) {
	return t === null ? "null" : typeof t == "string" ? `"${e(t)}"` : typeof t == "number" || typeof t == "boolean" ? String(t) : "";
}
function a(e) {
	return e === null ? "null" : Array.isArray(e) ? `[${String(e.length)}]` : typeof e == "object" ? `{${String(Object.keys(e).length)}}` : typeof e == "string" ? "str" : typeof e == "number" ? "123" : typeof e == "boolean" ? "bool" : typeof e;
}
function o(e) {
	return typeof e == "object" && !!e;
}
function s(e) {
	navigator.clipboard.writeText(e).catch(() => {});
}
var c = class {
	container;
	config;
	instanceId;
	data;
	pathBarEl = null;
	searchInputEl = null;
	treeEl = null;
	nodeStates = /* @__PURE__ */ new Map();
	searchQuery = "";
	selectedPath = "";
	constructor(e) {
		this.container = n(e.container), this.config = e, this.instanceId = t("cmm-json-path"), this.data = e.data, e.expanded === !0 && this.initExpandedStates(this.data, "$"), this.render();
	}
	update(e) {
		this.data = e, this.nodeStates.clear(), this.searchQuery = "", this.selectedPath = "", this.config.expanded === !0 && this.initExpandedStates(this.data, "$"), this.render();
	}
	expandAll() {
		this.initExpandedStates(this.data, "$"), this.renderTree();
	}
	collapseAll() {
		this.nodeStates.clear(), this.renderTree();
	}
	search(e) {
		this.searchQuery = e.toLowerCase(), this.searchInputEl !== null && (this.searchInputEl.value = e), this.searchQuery.length > 0 && this.expandMatchingPaths(this.data, "$"), this.renderTree();
	}
	destroy() {
		this.container.innerHTML = "", this.pathBarEl = null, this.searchInputEl = null, this.treeEl = null, this.nodeStates.clear();
	}
	initExpandedStates(e, t) {
		if (o(e)) if (this.nodeStates.set(t, { expanded: !0 }), Array.isArray(e)) for (let n = 0; n < e.length; n++) {
			let r = e[n];
			this.initExpandedStates(r, `${t}[${String(n)}]`);
		}
		else {
			let n = Object.entries(e);
			for (let [e, r] of n) this.initExpandedStates(r, `${t}.${e}`);
		}
	}
	expandMatchingPaths(e, t) {
		if (!o(e)) return i(e).toLowerCase().includes(this.searchQuery);
		let n = !1;
		if (Array.isArray(e)) for (let r = 0; r < e.length; r++) {
			let i = e[r], a = `${t}[${String(r)}]`;
			this.expandMatchingPaths(i, a) && (n = !0);
		}
		else {
			let r = Object.entries(e);
			for (let [e, i] of r) {
				let r = `${t}.${e}`;
				e.toLowerCase().includes(this.searchQuery) && (n = !0), this.expandMatchingPaths(i, r) && (n = !0);
			}
		}
		return n && this.nodeStates.set(t, { expanded: !0 }), n;
	}
	isExpanded(e) {
		return this.nodeStates.get(e)?.expanded === !0;
	}
	toggleNode(e) {
		let t = this.nodeStates.get(e);
		t === void 0 ? this.nodeStates.set(e, { expanded: !0 }) : t.expanded = !t.expanded, this.renderTree();
	}
	matchesSearch(e, t) {
		return this.searchQuery.length === 0 ? !1 : e.toLowerCase().includes(this.searchQuery) ? !0 : o(t) ? !1 : i(t).toLowerCase().includes(this.searchQuery);
	}
	render() {
		this.container.innerHTML = "";
		let e = document.createElement("div");
		e.className = "cmm-json-path", e.id = this.instanceId, this.config.className !== void 0 && e.classList.add(this.config.className);
		let t = document.createElement("div");
		t.className = "cmm-json-path__toolbar";
		let n = document.createElement("button");
		n.className = "cmm-json-path__btn", n.textContent = "Expand All", n.addEventListener("click", () => {
			this.expandAll();
		}), t.appendChild(n);
		let r = document.createElement("button");
		if (r.className = "cmm-json-path__btn", r.textContent = "Collapse All", r.addEventListener("click", () => {
			this.collapseAll();
		}), t.appendChild(r), this.config.searchable !== !1) {
			let e = document.createElement("input");
			e.className = "cmm-json-path__search", e.type = "text", e.placeholder = "Search keys or values...", e.value = this.searchQuery, e.addEventListener("input", () => {
				this.search(e.value);
			}), this.searchInputEl = e, t.appendChild(e);
		}
		if (e.appendChild(t), this.config.showPath !== !1) {
			let t = document.createElement("div");
			t.className = "cmm-json-path__path-bar", t.textContent = this.selectedPath.length > 0 ? this.selectedPath : "$", t.addEventListener("click", () => {
				s(this.selectedPath.length > 0 ? this.selectedPath : "$"), t.classList.add("cmm-json-path__path-bar--copied"), setTimeout(() => {
					t.classList.remove("cmm-json-path__path-bar--copied");
				}, 1e3);
			}), t.title = "Click to copy path", this.pathBarEl = t, e.appendChild(t);
		}
		let i = document.createElement("div");
		i.className = "cmm-json-path__tree", this.treeEl = i, e.appendChild(i), this.container.appendChild(e), this.renderTree();
	}
	renderTree() {
		if (this.treeEl === null) return;
		this.treeEl.innerHTML = "";
		let e = this.buildNode("$", "", this.data, 0);
		this.treeEl.appendChild(e);
	}
	buildNode(e, t, n, r) {
		let i = document.createElement("div");
		return i.className = "cmm-json-path__node", i.style.paddingLeft = `${String(r * 16)}px`, this.matchesSearch(t, n) && i.classList.add("cmm-json-path__node--highlight"), o(n) ? this.buildCompositeNode(i, e, t, n, r) : this.buildLeafNode(i, e, t, n);
	}
	buildCompositeNode(e, t, n, i, o) {
		let s = this.isExpanded(t), c = document.createElement("div");
		c.className = "cmm-json-path__row cmm-json-path__row--composite";
		let l = document.createElement("span");
		if (l.className = "cmm-json-path__arrow", l.classList.add(s ? "cmm-json-path__arrow--expanded" : "cmm-json-path__arrow--collapsed"), l.textContent = s ? "▾" : "▸", l.addEventListener("click", () => {
			this.toggleNode(t);
		}), c.appendChild(l), n.length > 0) {
			let e = document.createElement("span");
			e.className = "cmm-json-path__key", e.textContent = n, e.addEventListener("click", () => {
				this.selectPath(t, i);
			}), c.appendChild(e);
			let r = document.createElement("span");
			r.className = "cmm-json-path__colon", r.textContent = ": ", c.appendChild(r);
		}
		if (this.config.showTypes !== !1) {
			let e = document.createElement("span");
			e.className = "cmm-json-path__type-badge cmm-json-path__type-badge--" + r(i), e.textContent = a(i), c.appendChild(e);
		}
		if (e.appendChild(c), s) {
			let n = document.createElement("div");
			if (n.className = "cmm-json-path__children", Array.isArray(i)) for (let e = 0; e < i.length; e++) {
				let r = i[e], a = `${t}[${String(e)}]`;
				n.appendChild(this.buildNode(a, String(e), r, o + 1));
			}
			else {
				let e = Object.entries(i);
				for (let [r, i] of e) {
					let e = `${t}.${r}`;
					n.appendChild(this.buildNode(e, r, i, o + 1));
				}
			}
			e.appendChild(n);
		}
		return e;
	}
	buildLeafNode(e, t, n, o) {
		let c = document.createElement("div");
		c.className = "cmm-json-path__row cmm-json-path__row--leaf";
		let l = document.createElement("span");
		if (l.className = "cmm-json-path__arrow-spacer", c.appendChild(l), n.length > 0) {
			let e = document.createElement("span");
			e.className = "cmm-json-path__key", e.textContent = n, e.addEventListener("click", () => {
				this.selectPath(t, o);
			}), c.appendChild(e);
			let r = document.createElement("span");
			r.className = "cmm-json-path__colon", r.textContent = ": ", c.appendChild(r);
		}
		let u = r(o), d = document.createElement("span");
		if (d.className = `cmm-json-path__value cmm-json-path__value--${u}`, d.textContent = i(o), d.addEventListener("click", () => {
			this.selectPath(t, o), s(typeof o == "object" ? JSON.stringify(o) : String(o)), d.classList.add("cmm-json-path__value--copied"), setTimeout(() => {
				d.classList.remove("cmm-json-path__value--copied");
			}, 800);
		}), d.title = "Click to copy value", c.appendChild(d), this.config.showTypes !== !1) {
			let e = document.createElement("span");
			e.className = `cmm-json-path__type-badge cmm-json-path__type-badge--${u}`, e.textContent = a(o), c.appendChild(e);
		}
		return e.appendChild(c), e;
	}
	selectPath(e, t) {
		this.selectedPath = e, this.pathBarEl !== null && (this.pathBarEl.textContent = e), this.config.onSelect?.(e, t);
	}
};
//#endregion
export { c as JsonPathBrowser };

//# sourceMappingURL=index.js.map