"use client";
import { cn, extractText } from "./index3.js";
import { createSafeContext } from "./index21.js";
import { DialogContent, DialogDescription, DialogHeader, DialogNamespace, DialogTitle } from "./index387.js";
import { InputGroup, InputGroupAddon } from "./index388.js";
import { Check, Search } from "lucide-react";
import * as React from "react";
import { jsx, jsxs } from "react/jsx-runtime";
//#region src/components/ui/command.tsx
var [CommandContext, useCommandContext] = createSafeContext("Command");
function defaultFilter(value, search) {
	const haystack = value.toLowerCase();
	const needle = search.toLowerCase().trim();
	if (!needle) return 1;
	return haystack.includes(needle) ? 1 : 0;
}
function composeEventHandlers(theirHandler, ourHandler) {
	return (event) => {
		theirHandler?.(event);
		if (event?.defaultPrevented) return;
		ourHandler(event);
	};
}
function Command({ className, filter, children, ...props }) {
	const [search, setSearch] = React.useState("");
	const [activeId, setActiveId] = React.useState(null);
	const listId = React.useId();
	const itemsRef = React.useRef(/* @__PURE__ */ new Map());
	const [itemsVersion, setItemsVersion] = React.useState(0);
	const registerItem = React.useCallback((record) => {
		itemsRef.current.set(record.id, record);
		setItemsVersion((v) => v + 1);
		return () => {
			itemsRef.current.delete(record.id);
			setItemsVersion((v) => v + 1);
		};
	}, []);
	const updateItem = React.useCallback((id, updates) => {
		const prev = itemsRef.current.get(id);
		if (!prev) return;
		const next = {
			...prev,
			...updates
		};
		itemsRef.current.set(id, next);
		if (prev.value !== next.value || prev.disabled !== next.disabled || prev.searchValue !== next.searchValue) setItemsVersion((v) => v + 1);
	}, []);
	const effectiveFilter = filter ?? defaultFilter;
	const getItemVisibility = React.useCallback((item) => {
		if (!search.trim()) return true;
		return effectiveFilter(item.searchValue || item.value, search) > 0;
	}, [effectiveFilter, search]);
	const visibleEnabledItemIds = React.useMemo(() => {
		const ids = [];
		for (const [id, item] of itemsRef.current) {
			if (item.disabled) continue;
			if (getItemVisibility(item)) ids.push(id);
		}
		return ids;
	}, [getItemVisibility, itemsVersion]);
	const visibleItemCount = React.useMemo(() => {
		let count = 0;
		for (const item of itemsRef.current.values()) if (getItemVisibility(item)) count += 1;
		return count;
	}, [getItemVisibility, itemsVersion]);
	const selectActiveItem = React.useCallback(() => {
		if (!activeId) return;
		const item = itemsRef.current.get(activeId);
		if (!item || item.disabled || !getItemVisibility(item)) return;
		item.onSelect?.(item.value);
	}, [activeId, getItemVisibility]);
	React.useEffect(() => {
		if (visibleEnabledItemIds.length === 0) {
			setActiveId(null);
			return;
		}
		if (!activeId || !visibleEnabledItemIds.includes(activeId)) setActiveId(visibleEnabledItemIds[0]);
	}, [activeId, visibleEnabledItemIds]);
	const handleKeyDown = React.useCallback((event) => {
		if (visibleEnabledItemIds.length === 0) return;
		const index = activeId ? visibleEnabledItemIds.indexOf(activeId) : -1;
		if (event.key === "ArrowDown") {
			event.preventDefault();
			const next = visibleEnabledItemIds[Math.min(index + 1, visibleEnabledItemIds.length - 1)];
			setActiveId(next);
			return;
		}
		if (event.key === "ArrowUp") {
			event.preventDefault();
			const next = visibleEnabledItemIds[Math.max(index - 1, 0)];
			setActiveId(next);
			return;
		}
		if (event.key === "Home") {
			event.preventDefault();
			setActiveId(visibleEnabledItemIds[0]);
			return;
		}
		if (event.key === "End") {
			event.preventDefault();
			setActiveId(visibleEnabledItemIds[visibleEnabledItemIds.length - 1]);
			return;
		}
		if (event.key === "Enter") {
			if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) return;
			event.preventDefault();
			selectActiveItem();
		}
	}, [
		activeId,
		selectActiveItem,
		visibleEnabledItemIds
	]);
	const contextValue = React.useMemo(() => ({
		search,
		setSearch,
		filter,
		activeId,
		setActiveId,
		registerItem,
		updateItem,
		getItemVisibility,
		visibleEnabledItemIds,
		visibleItemCount,
		onKeyDown: handleKeyDown,
		selectActiveItem,
		listId
	}), [
		activeId,
		filter,
		getItemVisibility,
		handleKeyDown,
		listId,
		registerItem,
		search,
		selectActiveItem,
		updateItem,
		visibleEnabledItemIds,
		visibleItemCount
	]);
	return /* @__PURE__ */ jsx(CommandContext.Provider, {
		value: contextValue,
		children: /* @__PURE__ */ jsxs("div", {
			"data-slot": "command",
			"cmdk-root": "",
			className: cn("bg-popover text-popover-foreground rounded-xl! p-1 flex size-full flex-col overflow-hidden", className),
			...props,
			children: [children, /* @__PURE__ */ jsx("div", {
				className: "sr-only",
				"aria-live": "polite",
				"aria-atomic": "true",
				role: "status",
				children: search.trim() ? visibleItemCount === 0 ? "No results found" : `${visibleItemCount} result${visibleItemCount === 1 ? "" : "s"} available` : ""
			})]
		})
	});
}
function CommandDialog({ title = "Command Palette", description = "Search for a command to run...", children, className, showCloseButton = false, ...props }) {
	return /* @__PURE__ */ jsxs(DialogNamespace, {
		...props,
		children: [/* @__PURE__ */ jsxs(DialogHeader, {
			className: "sr-only",
			children: [/* @__PURE__ */ jsx(DialogTitle, { children: title }), /* @__PURE__ */ jsx(DialogDescription, { children: description })]
		}), /* @__PURE__ */ jsx(DialogContent, {
			className: cn("rounded-xl! overflow-hidden p-0", className),
			showCloseButton,
			children
		})]
	});
}
function CommandInput({ className, render, onChange, onKeyDown, ...props }) {
	const { onKeyDown: onCommandKeyDown, search, setSearch, listId, activeId } = useCommandContext();
	const composedOnChange = composeEventHandlers(onChange, React.useCallback((event) => {
		setSearch(event.target.value);
	}, [setSearch]));
	const composedOnKeyDown = composeEventHandlers(onKeyDown, onCommandKeyDown);
	if (render) return render({
		"data-slot": "command-input",
		"cmdk-input": "",
		role: "combobox",
		"aria-expanded": true,
		"aria-autocomplete": "list",
		"aria-controls": listId,
		"aria-activedescendant": activeId || void 0,
		value: search,
		onChange: composedOnChange,
		onKeyDown: composedOnKeyDown
	});
	return /* @__PURE__ */ jsx("div", {
		"data-slot": "command-input-wrapper",
		"cmdk-input-wrapper": "",
		className: "p-1 pb-0",
		children: /* @__PURE__ */ jsxs(InputGroup, {
			className: "bg-input/30 border-input/30 h-8! rounded-lg! shadow-none! *:data-[slot=input-group-addon]:ps-2!",
			children: [/* @__PURE__ */ jsx("input", {
				"data-slot": "command-input",
				"cmdk-input": "",
				role: "combobox",
				"aria-expanded": true,
				"aria-autocomplete": "list",
				"aria-controls": listId,
				"aria-activedescendant": activeId || void 0,
				className: cn("w-full text-sm outline-hidden disabled:cursor-not-allowed disabled:opacity-50", className),
				value: search,
				onChange: composedOnChange,
				onKeyDown: composedOnKeyDown,
				...props
			}), /* @__PURE__ */ jsx(InputGroupAddon, { children: /* @__PURE__ */ jsx(Search, { className: "size-4 shrink-0 opacity-50" }) })]
		})
	});
}
function CommandList({ className, ...props }) {
	const { listId } = useCommandContext();
	return /* @__PURE__ */ jsx("div", {
		"data-slot": "command-list",
		"cmdk-list": "",
		role: "listbox",
		id: listId,
		className: cn("no-scrollbar max-h-72 scroll-py-1 outline-none overflow-x-hidden overflow-y-auto", className),
		...props
	});
}
function CommandEmpty({ className, ...props }) {
	const { visibleItemCount } = useCommandContext();
	if (visibleItemCount > 0) return null;
	return /* @__PURE__ */ jsx("div", {
		"data-slot": "command-empty",
		"cmdk-empty": "",
		className: cn("py-6 text-center text-sm", className),
		...props
	});
}
function CommandGroup({ className, heading, children, ...props }) {
	const headingId = React.useId();
	return /* @__PURE__ */ jsxs("div", {
		"data-slot": "command-group",
		"cmdk-group": "",
		role: "group",
		"aria-labelledby": heading != null ? headingId : void 0,
		className: cn("text-foreground [&_[cmdk-group-heading]]:text-muted-foreground overflow-hidden p-1 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1.5 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-semibold [&_[cmdk-group-heading]]:uppercase [&_[cmdk-group-heading]]:tracking-wider", className),
		...props,
		children: [heading != null && /* @__PURE__ */ jsx("div", {
			"cmdk-group-heading": "",
			id: headingId,
			children: heading
		}), children]
	});
}
function CommandSeparator({ className, ...props }) {
	return /* @__PURE__ */ jsx("hr", {
		"data-slot": "command-separator",
		"cmdk-separator": "",
		className: cn("bg-border -mx-1 h-px w-auto", className),
		...props
	});
}
function CommandItem({ className, children, value, disabled = false, onSelect, keywords, onClick, onKeyDown, onMouseMove, ...props }) {
	const { activeId, getItemVisibility, registerItem, setActiveId, updateItem } = useCommandContext();
	const id = React.useId();
	const searchValue = React.useMemo(() => {
		return [
			value,
			keywords?.join(" ") ?? "",
			extractText(children)
		].filter(Boolean).join(" ");
	}, [
		children,
		keywords,
		value
	]);
	const visible = getItemVisibility({
		value,
		searchValue
	});
	const selected = activeId === id;
	React.useLayoutEffect(() => {
		return registerItem({
			id,
			value: "",
			disabled: false,
			searchValue: ""
		});
	}, [id, registerItem]);
	React.useEffect(() => {
		updateItem(id, {
			value,
			disabled,
			onSelect,
			searchValue
		});
	}, [
		disabled,
		id,
		onSelect,
		searchValue,
		updateItem,
		value
	]);
	const handleSelect = React.useCallback(() => {
		if (disabled || !visible) return;
		onSelect?.(value);
	}, [
		disabled,
		onSelect,
		value,
		visible
	]);
	return /* @__PURE__ */ jsxs("div", {
		id,
		"data-slot": "command-item",
		"cmdk-item": "",
		"data-disabled": disabled,
		"data-selected": selected,
		role: "option",
		"aria-selected": selected,
		"aria-disabled": disabled,
		hidden: !visible,
		tabIndex: -1,
		className: cn("data-selected:bg-muted data-selected:text-foreground data-selected:**:[svg]:text-foreground border-l-2 border-l-transparent data-selected:border-l-brand-500 relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none transition-colors duration-150 [&_svg:not([class*='size-'])]:size-4 [[data-slot=dialog-content]_&]:rounded-lg! group/command-item data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0", className),
		onMouseMove: composeEventHandlers(onMouseMove, () => setActiveId(id)),
		onClick: composeEventHandlers(onClick, () => {
			setActiveId(id);
			handleSelect();
		}),
		onKeyDown: composeEventHandlers(onKeyDown, (event) => {
			if (disabled || !visible) return;
			if (event.key !== "Enter" && event.key !== " ") return;
			event.preventDefault();
			setActiveId(id);
			handleSelect();
		}),
		...props,
		children: [children, /* @__PURE__ */ jsx(Check, { className: "ms-auto opacity-0 group-has-[[data-slot=command-shortcut]]/command-item:hidden group-data-[checked=true]/command-item:opacity-100" })]
	});
}
function CommandShortcut({ className, ...props }) {
	return /* @__PURE__ */ jsx("span", {
		"data-slot": "command-shortcut",
		className: cn("text-muted-foreground group-data-selected/command-item:text-foreground ms-auto text-xs tracking-widest", className),
		...props
	});
}
CommandDialog.displayName = "Command.Dialog";
CommandInput.displayName = "Command.Input";
CommandList.displayName = "Command.List";
CommandEmpty.displayName = "Command.Empty";
CommandGroup.displayName = "Command.Group";
CommandSeparator.displayName = "Command.Separator";
CommandItem.displayName = "Command.Item";
CommandShortcut.displayName = "Command.Shortcut";
Command.displayName = "Command";
var CommandNamespace = Object.assign(Command, {
	Dialog: CommandDialog,
	Input: CommandInput,
	List: CommandList,
	Empty: CommandEmpty,
	Group: CommandGroup,
	Separator: CommandSeparator,
	Item: CommandItem,
	Shortcut: CommandShortcut
});
//#endregion
export { CommandDialog, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandNamespace, CommandSeparator };

//# sourceMappingURL=index389.js.map