import { n as e } from "../../utils-C_mWfcFX.js";
import { t } from "../../a11y-CuHKgVSx.js";
import { s as n, t as r } from "../../keyboard-wOEoCCWj.js";
import { CommandPalette as i } from "./command-palette.js";
//#region src/components/search/index.ts
function a(e) {
	if (typeof e == "string") {
		let t = document.querySelector(e);
		if (!t) throw Error(`SearchInput: container not found for selector "${e}"`);
		return t;
	}
	return e;
}
var o = class {
	config;
	containerEl;
	rootEl;
	inputEl;
	resultsEl;
	results = [];
	activeIndex = -1;
	debounceTimer = null;
	destroyed = !1;
	inputId;
	listboxId;
	labelId;
	optionIds = [];
	constructor(t) {
		this.config = t, this.containerEl = a(t.container), this.inputId = e("cmm-search-input"), this.listboxId = e("cmm-search-listbox"), this.labelId = e("cmm-search-label"), this.rootEl = document.createElement("div"), this.rootEl.className = "cmm-search";
		let n = document.createElement("label");
		n.id = this.labelId, n.className = "cmm-sr-only", n.setAttribute("for", this.inputId), n.textContent = t.placeholder ?? "Search", this.rootEl.appendChild(n), this.inputEl = document.createElement("input"), this.inputEl.type = "text", this.inputEl.id = this.inputId, this.inputEl.className = "cmm-search__input", this.inputEl.setAttribute("role", "combobox"), this.inputEl.setAttribute("aria-autocomplete", "list"), this.inputEl.setAttribute("aria-expanded", "false"), this.inputEl.setAttribute("aria-controls", this.listboxId), this.inputEl.setAttribute("aria-haspopup", "listbox"), this.inputEl.setAttribute("autocomplete", "off"), this.inputEl.setAttribute("autocorrect", "off"), this.inputEl.setAttribute("autocapitalize", "off"), this.inputEl.setAttribute("spellcheck", "false"), t.placeholder !== void 0 && (this.inputEl.placeholder = t.placeholder), this.resultsEl = document.createElement("div"), this.resultsEl.id = this.listboxId, this.resultsEl.className = "cmm-search__results", this.resultsEl.setAttribute("role", "listbox"), this.resultsEl.setAttribute("aria-label", "Search suggestions"), this.resultsEl.style.display = "none", this.rootEl.appendChild(this.inputEl), this.rootEl.appendChild(this.resultsEl), this.containerEl.appendChild(this.rootEl), this.inputEl.addEventListener("input", this.handleInput), this.inputEl.addEventListener("keydown", this.handleKeyDown), this.inputEl.addEventListener("focus", this.handleFocus), this.inputEl.addEventListener("blur", this.handleBlur), document.addEventListener("click", this.handleDocumentClick);
	}
	setValue(e) {
		this.inputEl.value = e;
	}
	getValue() {
		return this.inputEl.value;
	}
	focus() {
		this.inputEl.focus();
	}
	clear() {
		this.inputEl.value = "", this.hideResults();
	}
	destroy() {
		this.destroyed || (this.destroyed = !0, this.debounceTimer !== null && clearTimeout(this.debounceTimer), this.inputEl.removeEventListener("input", this.handleInput), this.inputEl.removeEventListener("keydown", this.handleKeyDown), this.inputEl.removeEventListener("focus", this.handleFocus), this.inputEl.removeEventListener("blur", this.handleBlur), document.removeEventListener("click", this.handleDocumentClick), this.rootEl.parentNode && this.rootEl.parentNode.removeChild(this.rootEl));
	}
	isDestroyed() {
		return this.destroyed;
	}
	handleFocus = () => {
		this.results.length > 0 && this.showResults();
	};
	handleBlur = () => {
		setTimeout(() => {
			this.rootEl.contains(document.activeElement) || this.hideResults();
		}, 150);
	};
	handleInput = () => {
		let e = this.inputEl.value;
		this.debounceTimer !== null && clearTimeout(this.debounceTimer);
		let t = this.config.debounceMs ?? 200;
		this.debounceTimer = setTimeout(() => {
			this.performSuggest(e);
		}, t);
	};
	async performSuggest(e) {
		if (this.destroyed) return;
		if (!this.config.suggest) {
			this.config.onSearch && this.config.onSearch(e);
			return;
		}
		if (e.trim().length === 0) {
			this.hideResults();
			return;
		}
		let n = this.config.suggest(e), r = n instanceof Promise ? await n : n;
		this.isDestroyed() || (this.results = r, this.activeIndex = -1, this.renderResults(), r.length === 0 ? t("No results found", "polite") : r.length === 1 ? t("1 result available", "polite") : t(`${String(r.length)} results available`, "polite"));
	}
	showResults() {
		this.resultsEl.style.display = "", this.inputEl.setAttribute("aria-expanded", "true");
	}
	renderResults() {
		if (this.resultsEl.innerHTML = "", this.optionIds = [], this.results.length === 0) {
			this.hideResults();
			return;
		}
		let t = /* @__PURE__ */ new Map(), n = [];
		for (let e of this.results) if (e.group) {
			let n = t.get(e.group);
			n || (n = [], t.set(e.group, n)), n.push(e);
		} else n.push(e);
		let r = 0, i = (t, n) => {
			let r = e("cmm-search-option");
			this.optionIds[n] = r;
			let i = document.createElement("div");
			i.className = "cmm-search__result", i.id = r, i.setAttribute("role", "option"), i.setAttribute("aria-selected", n === this.activeIndex ? "true" : "false"), i.dataset.index = String(n), n === this.activeIndex && i.classList.add("cmm-search__result--active");
			let a = document.createElement("span");
			if (a.className = "cmm-search__result-label", a.textContent = t.label, i.appendChild(a), t.description) {
				let e = document.createElement("span");
				e.className = "cmm-search__result-description", e.textContent = t.description, i.appendChild(e);
			}
			return i.addEventListener("click", () => {
				this.selectResult(t);
			}), i.addEventListener("mouseenter", () => {
				this.setActiveIndex(n);
			}), i;
		};
		if (n.length > 0) for (let e of n) this.resultsEl.appendChild(i(e, r)), r += 1;
		for (let [e, n] of t) {
			let t = document.createElement("div");
			t.className = "cmm-search__group", t.setAttribute("role", "presentation"), t.setAttribute("aria-hidden", "true"), t.textContent = e, this.resultsEl.appendChild(t);
			for (let e of n) this.resultsEl.appendChild(i(e, r)), r += 1;
		}
		this.showResults();
	}
	setActiveIndex(e) {
		let n = this.resultsEl.querySelectorAll("[role=\"option\"]");
		for (let e of n) e.classList.remove("cmm-search__result--active"), e.setAttribute("aria-selected", "false");
		if (this.activeIndex = e, e >= 0 && e < this.results.length) {
			let n = this.optionIds[e];
			if (n !== void 0) {
				let r = this.resultsEl.querySelector(`#${n}`);
				if (r !== null) {
					r.classList.add("cmm-search__result--active"), r.setAttribute("aria-selected", "true"), r.scrollIntoView({ block: "nearest" }), this.inputEl.setAttribute("aria-activedescendant", n);
					let i = this.results[e];
					i !== void 0 && t(i.description ? `${i.label}, ${i.description}` : i.label, "polite");
				}
			}
		} else this.inputEl.removeAttribute("aria-activedescendant");
	}
	selectResult(e) {
		this.inputEl.value = e.label, this.hideResults(), this.inputEl.focus(), this.config.onSelect && this.config.onSelect(e), t(`${e.label} selected`, "polite");
	}
	hideResults() {
		this.results = [], this.activeIndex = -1, this.optionIds = [], this.resultsEl.innerHTML = "", this.resultsEl.style.display = "none", this.inputEl.setAttribute("aria-expanded", "false"), this.inputEl.removeAttribute("aria-activedescendant");
	}
	handleKeyDown = (e) => {
		if (this.results.length > 0) {
			let t = n(e, {
				activeIndex: this.activeIndex,
				itemCount: this.results.length,
				wrap: !0,
				orientation: "vertical"
			});
			if (t.handled) {
				if (e.preventDefault(), t.activated) if (this.activeIndex >= 0) {
					let e = this.results[this.activeIndex];
					e && this.selectResult(e);
				} else this.config.onSearch && (this.hideResults(), this.config.onSearch(this.inputEl.value));
				else this.setActiveIndex(t.index);
				return;
			}
		}
		switch (e.key) {
			case r.ENTER:
				this.results.length === 0 && this.config.onSearch && this.config.onSearch(this.inputEl.value);
				break;
			case r.ESCAPE:
				this.results.length > 0 && (e.preventDefault(), this.hideResults(), t("Search suggestions dismissed", "polite"));
				break;
			case r.TAB:
				this.hideResults();
				break;
		}
	};
	handleDocumentClick = (e) => {
		let t = e.target;
		t instanceof Node && !this.rootEl.contains(t) && this.hideResults();
	};
};
//#endregion
export { i as CommandPalette, o as SearchInput };

//# sourceMappingURL=index.js.map