//#region src/components/search/command-palette.ts
function e(e) {
	let t = e.toLowerCase().split("+"), n = !1, r = "";
	for (let e of t) {
		let t = e.trim();
		t === "ctrl" || t === "meta" || t === "cmd" ? n = !0 : r = t;
	}
	return {
		ctrl: n,
		key: r
	};
}
var t = class {
	commands;
	overlayEl;
	containerEl;
	inputEl;
	resultsEl;
	filteredCommands = [];
	activeIndex = 0;
	destroyed = !1;
	constructor(e) {
		this.commands = [...e.commands], this.overlayEl = document.createElement("div"), this.overlayEl.className = "cmm-command-palette__overlay", this.overlayEl.style.display = "none", this.containerEl = document.createElement("div"), this.containerEl.className = "cmm-command-palette", this.inputEl = document.createElement("input"), this.inputEl.type = "text", this.inputEl.className = "cmm-command-palette__input", e.placeholder === void 0 ? this.inputEl.placeholder = "Type a command..." : this.inputEl.placeholder = e.placeholder, this.resultsEl = document.createElement("div"), this.resultsEl.className = "cmm-command-palette__results", this.containerEl.appendChild(this.inputEl), this.containerEl.appendChild(this.resultsEl), this.overlayEl.appendChild(this.containerEl), document.body.appendChild(this.overlayEl), this.inputEl.addEventListener("input", this.handleInput), this.inputEl.addEventListener("keydown", this.handleKeyDown), this.overlayEl.addEventListener("click", this.handleOverlayClick);
		let t = e.keybinding ?? "ctrl+k";
		document.addEventListener("keydown", this.createGlobalKeyHandler(t));
	}
	open() {
		this.destroyed || (this.overlayEl.style.display = "", this.inputEl.value = "", this.activeIndex = 0, this.filteredCommands = [...this.commands], this.renderResults(), this.inputEl.focus());
	}
	close() {
		this.overlayEl.style.display = "none", this.inputEl.value = "", this.filteredCommands = [], this.resultsEl.innerHTML = "";
	}
	setCommands(e) {
		this.commands = [...e];
	}
	destroy() {
		this.destroyed || (this.destroyed = !0, this.inputEl.removeEventListener("input", this.handleInput), this.inputEl.removeEventListener("keydown", this.handleKeyDown), this.overlayEl.removeEventListener("click", this.handleOverlayClick), document.removeEventListener("keydown", this.globalKeyHandler), this.overlayEl.parentNode && this.overlayEl.parentNode.removeChild(this.overlayEl));
	}
	globalKeyHandler = () => {};
	createGlobalKeyHandler(t) {
		let n = e(t);
		return this.globalKeyHandler = (e) => {
			let t = e.ctrlKey || e.metaKey;
			n.ctrl && t && e.key.toLowerCase() === n.key && (e.preventDefault(), this.overlayEl.style.display === "none" ? this.open() : this.close());
		}, this.globalKeyHandler;
	}
	handleInput = () => {
		let e = this.inputEl.value.toLowerCase().trim();
		e.length === 0 ? this.filteredCommands = [...this.commands] : this.filteredCommands = this.commands.filter((t) => t.label.toLowerCase().includes(e)), this.activeIndex = 0, this.renderResults();
	};
	renderResults() {
		if (this.resultsEl.innerHTML = "", this.filteredCommands.length === 0) return;
		let e = /* @__PURE__ */ new Map(), t = [];
		for (let n of this.filteredCommands) if (n.group) {
			let t = e.get(n.group);
			t || (t = [], e.set(n.group, t)), t.push(n);
		} else t.push(n);
		let n = 0, r = (e, t) => {
			let n = document.createElement("div");
			n.className = "cmm-command-palette__item", t === this.activeIndex && n.classList.add("cmm-command-palette__item--active");
			let r = document.createElement("span");
			if (r.textContent = e.label, n.appendChild(r), e.keybinding) {
				let t = document.createElement("span");
				t.className = "cmm-command-palette__keybinding", t.textContent = e.keybinding, n.appendChild(t);
			}
			return n.addEventListener("click", () => {
				this.executeCommand(e);
			}), n;
		};
		if (t.length > 0) for (let e of t) this.resultsEl.appendChild(r(e, n)), n += 1;
		for (let [t, i] of e) {
			let e = document.createElement("div");
			e.className = "cmm-command-palette__group", e.textContent = t, this.resultsEl.appendChild(e);
			for (let e of i) this.resultsEl.appendChild(r(e, n)), n += 1;
		}
	}
	executeCommand(e) {
		this.close(), e.handler();
	}
	handleKeyDown = (e) => {
		if (e.key === "ArrowDown") e.preventDefault(), this.filteredCommands.length > 0 && (this.activeIndex = Math.min(this.activeIndex + 1, this.filteredCommands.length - 1), this.renderResults());
		else if (e.key === "ArrowUp") e.preventDefault(), this.activeIndex = Math.max(this.activeIndex - 1, 0), this.renderResults();
		else if (e.key === "Enter") {
			e.preventDefault();
			let t = this.filteredCommands[this.activeIndex];
			t && this.executeCommand(t);
		} else e.key === "Escape" && this.close();
	};
	handleOverlayClick = (e) => {
		e.target === this.overlayEl && this.close();
	};
};
//#endregion
export { t as CommandPalette };

//# sourceMappingURL=command-palette.js.map