import { t as e } from "../../utils-B7XIe3qq.js";
import { t } from "../../search-CrxnZfZ5.js";
import { t as n } from "../../x-BleQvk5v.js";
import * as r from "react";
import { jsx as i, jsxs as a } from "react/jsx-runtime";
//#region src/react/primitives/search.tsx
function o({ className: o, onSearch: c, suggest: l, onSelect: u, debounceMs: d = 200, clearable: f = !0, value: p, onChange: m, ...h }) {
	let [g, _] = r.useState(""), v = p === void 0 ? g : String(p), [y, b] = r.useState([]), [x, S] = r.useState(-1), [C, w] = r.useState(!1), T = r.useRef(null), E = r.useRef(null), D = r.useRef(null), O = r.useCallback((e) => {
		let t = e.target.value;
		if (p === void 0 && _(t), m?.(e), l === void 0) return;
		T.current !== null && clearTimeout(T.current);
		let n = l;
		T.current = setTimeout(() => {
			if (t.trim().length === 0) {
				b([]), w(!1);
				return;
			}
			let e = n(t);
			e instanceof Promise ? e.then((e) => {
				b(e), S(-1), w(e.length > 0);
			}) : (b(e), S(-1), w(e.length > 0));
		}, d);
	}, [
		p,
		m,
		l,
		d
	]), k = r.useCallback((e) => {
		p === void 0 && _(e.label), w(!1), b([]), u?.(e), D.current?.focus();
	}, [p, u]), A = r.useCallback((e) => {
		if (C && y.length > 0) {
			if (e.key === "ArrowDown") {
				e.preventDefault(), S((e) => Math.min(e + 1, y.length - 1));
				return;
			}
			if (e.key === "ArrowUp") {
				e.preventDefault(), S((e) => Math.max(e - 1, 0));
				return;
			}
			if (e.key === "Enter" && x >= 0) {
				e.preventDefault();
				let t = y[x];
				t !== void 0 && k(t);
				return;
			}
			if (e.key === "Escape") {
				e.preventDefault(), w(!1);
				return;
			}
		}
		e.key === "Enter" && c?.(v);
	}, [
		C,
		y,
		x,
		v,
		c,
		k
	]);
	r.useEffect(() => {
		let e = (e) => {
			E.current !== null && !E.current.contains(e.target) && w(!1);
		};
		return document.addEventListener("click", e), () => {
			document.removeEventListener("click", e);
		};
	}, []), r.useEffect(() => () => {
		T.current !== null && clearTimeout(T.current);
	}, []);
	let j = r.useCallback(() => {
		p === void 0 && _(""), b([]), w(!1), D.current?.focus();
	}, [p]), M = r.useMemo(() => {
		let e = /* @__PURE__ */ new Map(), t = [];
		for (let n of y) if (n.group !== void 0) {
			let t = e.get(n.group);
			t === void 0 && (t = [], e.set(n.group, t)), t.push(n);
		} else t.push(n);
		return {
			ungrouped: t,
			groups: e
		};
	}, [y]), N = 0;
	return /* @__PURE__ */ a("div", {
		ref: E,
		"data-slot": "search",
		className: e("relative", o),
		children: [/* @__PURE__ */ a("div", {
			className: "relative",
			children: [
				/* @__PURE__ */ i(t, { className: "absolute left-3 top-1/2 size-4 -translate-y-1/2 text-foreground-muted pointer-events-none" }),
				/* @__PURE__ */ i("input", {
					ref: D,
					"data-slot": "search-input",
					type: "text",
					role: "combobox",
					"aria-expanded": C,
					"aria-autocomplete": "list",
					"aria-haspopup": "listbox",
					autoComplete: "off",
					autoCorrect: "off",
					autoCapitalize: "off",
					spellCheck: !1,
					value: v,
					onChange: O,
					onKeyDown: A,
					className: e("h-10 w-full rounded-xl border border-border-glass bg-glass pl-9 pr-9 text-sm text-foreground placeholder:text-foreground-muted", "backdrop-blur-[20px] outline-none transition-colors", "focus:border-primary focus:ring-2 focus:ring-primary/20"),
					...h
				}),
				f && v.length > 0 && /* @__PURE__ */ i("button", {
					type: "button",
					className: "absolute right-2 top-1/2 -translate-y-1/2 rounded-md p-1 text-foreground-muted hover:text-foreground transition-colors",
					onClick: j,
					"aria-label": "Clear search",
					children: /* @__PURE__ */ i(n, { className: "size-3.5" })
				})
			]
		}), C && /* @__PURE__ */ a("div", {
			"data-slot": "search-results",
			role: "listbox",
			"aria-label": "Search suggestions",
			className: "absolute top-full left-0 z-50 mt-1 w-full overflow-hidden rounded-xl border border-border-glass bg-popover p-1 shadow-xl backdrop-blur-xl",
			children: [M.ungrouped.map((e) => {
				let t = N++;
				return /* @__PURE__ */ i(s, {
					result: e,
					active: t === x,
					onSelect: () => {
						k(e);
					},
					onHover: () => {
						S(t);
					}
				}, e.value);
			}), Array.from(M.groups).map(([e, t]) => /* @__PURE__ */ a(r.Fragment, { children: [/* @__PURE__ */ i("div", {
				className: "px-3 py-1.5 text-xs font-semibold text-foreground-muted",
				children: e
			}), t.map((e) => {
				let t = N++;
				return /* @__PURE__ */ i(s, {
					result: e,
					active: t === x,
					onSelect: () => {
						k(e);
					},
					onHover: () => {
						S(t);
					}
				}, e.value);
			})] }, e))]
		})]
	});
}
function s({ result: t, active: n, onSelect: r, onHover: o }) {
	return /* @__PURE__ */ a("div", {
		role: "option",
		"aria-selected": n,
		className: e("flex cursor-pointer items-center gap-2 rounded-lg px-3 py-2 text-sm transition-colors outline-none select-none", "text-foreground", n ? "bg-accent" : "hover:bg-accent"),
		onClick: r,
		onMouseEnter: o,
		children: [t.icon !== void 0 && /* @__PURE__ */ i("span", {
			className: "shrink-0",
			children: t.icon
		}), /* @__PURE__ */ a("div", {
			className: "flex flex-col gap-0.5 overflow-hidden",
			children: [/* @__PURE__ */ i("span", {
				className: "truncate",
				children: t.label
			}), t.description !== void 0 && /* @__PURE__ */ i("span", {
				className: "truncate text-xs text-foreground-muted",
				children: t.description
			})]
		})]
	});
}
function c({ open: e, onOpenChange: n, commands: o, placeholder: s = "Type a command...", keybinding: c = "ctrl+k" }) {
	let [u, d] = r.useState(!1), f = e ?? u, p = r.useCallback((t) => {
		e === void 0 && d(t), n?.(t);
	}, [e, n]), [m, h] = r.useState(""), [g, _] = r.useState(0), v = r.useRef(null);
	r.useEffect(() => {
		let e = c.toLowerCase().split("+"), t = e.some((e) => [
			"ctrl",
			"meta",
			"cmd"
		].includes(e.trim())), n = e.find((e) => ![
			"ctrl",
			"meta",
			"cmd"
		].includes(e.trim()))?.trim() ?? "", r = (e) => {
			let r = e.ctrlKey || e.metaKey;
			t && r && e.key.toLowerCase() === n && (e.preventDefault(), p(!f));
		};
		return document.addEventListener("keydown", r), () => {
			document.removeEventListener("keydown", r);
		};
	}, [
		c,
		f,
		p
	]), r.useEffect(() => {
		f && (h(""), _(0), requestAnimationFrame(() => {
			v.current?.focus();
		}));
	}, [f]);
	let y = r.useMemo(() => {
		if (m.trim().length === 0) return o;
		let e = m.toLowerCase();
		return o.filter((t) => t.label.toLowerCase().includes(e));
	}, [o, m]), b = r.useCallback((e) => {
		p(!1), e.handler();
	}, [p]), x = r.useCallback((e) => {
		if (e.key === "ArrowDown") e.preventDefault(), _((e) => Math.min(e + 1, y.length - 1));
		else if (e.key === "ArrowUp") e.preventDefault(), _((e) => Math.max(e - 1, 0));
		else if (e.key === "Enter") {
			e.preventDefault();
			let t = y[g];
			t !== void 0 && b(t);
		} else e.key === "Escape" && p(!1);
	}, [
		y,
		g,
		b,
		p
	]);
	if (!f) return null;
	let S = /* @__PURE__ */ new Map(), C = [];
	for (let e of y) if (e.group !== void 0) {
		let t = S.get(e.group);
		t === void 0 && (t = [], S.set(e.group, t)), t.push(e);
	} else C.push(e);
	let w = 0;
	return /* @__PURE__ */ a("div", {
		"data-slot": "command-palette",
		className: "fixed inset-0 z-50 flex items-start justify-center pt-[20vh]",
		children: [/* @__PURE__ */ i("div", {
			className: "fixed inset-0 bg-black/60 backdrop-blur-sm animate-in fade-in-0",
			onClick: () => {
				p(!1);
			}
		}), /* @__PURE__ */ a("div", {
			className: "relative z-10 w-full max-w-lg overflow-hidden rounded-xl border border-border-glass bg-glass shadow-xl backdrop-blur-xl animate-in fade-in-0 zoom-in-95",
			children: [
				/* @__PURE__ */ a("div", {
					className: "flex items-center border-b border-border-glass px-4",
					children: [/* @__PURE__ */ i(t, { className: "size-4 shrink-0 text-foreground-muted" }), /* @__PURE__ */ i("input", {
						ref: v,
						type: "text",
						placeholder: s,
						value: m,
						onChange: (e) => {
							h(e.target.value), _(0);
						},
						onKeyDown: x,
						className: "flex-1 bg-transparent px-3 py-3 text-sm text-foreground placeholder:text-foreground-muted outline-none"
					})]
				}),
				y.length > 0 && /* @__PURE__ */ a("div", {
					className: "max-h-72 overflow-y-auto p-1",
					children: [C.map((e) => {
						let t = w++;
						return /* @__PURE__ */ i(l, {
							entry: e,
							active: t === g,
							onSelect: () => {
								b(e);
							},
							onHover: () => {
								_(t);
							}
						}, e.id);
					}), Array.from(S).map(([e, t]) => /* @__PURE__ */ a(r.Fragment, { children: [/* @__PURE__ */ i("div", {
						className: "px-3 py-1.5 text-xs font-semibold text-foreground-muted",
						children: e
					}), t.map((e) => {
						let t = w++;
						return /* @__PURE__ */ i(l, {
							entry: e,
							active: t === g,
							onSelect: () => {
								b(e);
							},
							onHover: () => {
								_(t);
							}
						}, e.id);
					})] }, e))]
				}),
				y.length === 0 && m.length > 0 && /* @__PURE__ */ i("div", {
					className: "px-4 py-8 text-center text-sm text-foreground-muted",
					children: "No commands found."
				})
			]
		})]
	});
}
function l({ entry: t, active: n, onSelect: r, onHover: o }) {
	return /* @__PURE__ */ a("div", {
		className: e("flex cursor-pointer items-center justify-between rounded-lg px-3 py-2 text-sm transition-colors select-none", "text-foreground", n ? "bg-accent" : "hover:bg-accent"),
		onClick: r,
		onMouseEnter: o,
		children: [/* @__PURE__ */ a("div", {
			className: "flex items-center gap-2 overflow-hidden",
			children: [t.icon !== void 0 && /* @__PURE__ */ i("span", {
				className: "shrink-0",
				children: t.icon
			}), /* @__PURE__ */ i("span", {
				className: "truncate",
				children: t.label
			})]
		}), t.keybinding !== void 0 && /* @__PURE__ */ i("span", {
			className: "ml-4 shrink-0 text-xs text-foreground-muted tracking-wider",
			children: t.keybinding
		})]
	});
}
//#endregion
export { c as CommandPalette, o as Search };

//# sourceMappingURL=search.js.map