"use client";
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const jsxRuntime = require('react/jsx-runtime');
const LucideIcons = require('lucide-react');
const React = require('react');
const useDebounce = require('../../hooks/use-debounce.cjs');
const command = require('../../../../../../../ui/src/components/command.cjs');

function _interopNamespaceCompat(e) {
	if (e && typeof e === 'object' && 'default' in e) return e;
	const n = Object.create(null);
	if (e) {
		for (const k in e) {
			n[k] = e[k];
		}
	}
	n.default = e;
	return n;
}

const React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);

function SearchModal({
  placeholder = "Type to search...",
  emptyMessage = "No results found.",
  buttonText = "Search",
  keyboardShortcut = "\u2318K",
  searchFn,
  renderResult,
  results: externalResults,
  isLoading = false,
  className,
  triggerClassName
}) {
  const [open, setOpen] = React__namespace.useState(false);
  const [query, setQuery] = React__namespace.useState("");
  const [results, setResults] = React__namespace.useState([]);
  const shouldDebounce = externalResults === void 0;
  const debouncedQuery = useDebounce.useDebounce(query, shouldDebounce ? 300 : 0);
  React__namespace.useEffect(() => {
    const down = (e) => {
      const cleanShortcut = keyboardShortcut.replace("\u2318", "").replace("\u21E7", "").toLowerCase();
      if (e.key === cleanShortcut && (e.metaKey || e.ctrlKey)) {
        e.preventDefault();
        setOpen((open2) => !open2);
      }
    };
    document.addEventListener("keydown", down);
    return () => document.removeEventListener("keydown", down);
  }, [keyboardShortcut]);
  React__namespace.useEffect(() => {
    if (!open) {
      setQuery("");
      setResults([]);
      return;
    }
  }, [open]);
  React__namespace.useEffect(() => {
    if (!open) return;
    const searchResults = searchFn(debouncedQuery);
    if (externalResults !== void 0) {
      setResults(externalResults);
    } else {
      setResults(searchResults);
    }
  }, [debouncedQuery, open, searchFn, externalResults]);
  const buttonClasses = [
    "border-input bg-background text-foreground",
    "placeholder:text-muted-foreground",
    "focus-visible:border-ring focus-visible:ring-ring/50",
    "inline-flex h-9 w-fit rounded-md border px-3 py-2 text-sm",
    "shadow-xs transition-[color,box-shadow] outline-none",
    "focus-visible:ring-[3px]",
    triggerClassName
  ].filter(Boolean).join(" ");
  const showEmpty = debouncedQuery && !isLoading && results.length === 0;
  const currentEmptyMessage = isLoading ? "Searching..." : emptyMessage;
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    /* @__PURE__ */ jsxRuntime.jsxs(
      "button",
      {
        "data-testid": "search-button",
        type: "button",
        className: buttonClasses,
        onClick: () => setOpen(true),
        children: [
          /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "flex grow items-center", children: [
            /* @__PURE__ */ jsxRuntime.jsx(
              LucideIcons.SearchIcon,
              {
                className: "-ms-1 me-3 text-muted-foreground",
                size: 16,
                "aria-hidden": "true"
              }
            ),
            /* @__PURE__ */ jsxRuntime.jsx("span", { className: "font-normal text-muted-foreground", children: buttonText })
          ] }),
          keyboardShortcut && /* @__PURE__ */ jsxRuntime.jsx("kbd", { className: "-me-1 ms-12 inline-flex h-5 max-h-full items-center rounded border bg-background px-1 font-[inherit] font-medium text-[0.625rem] text-muted-foreground", children: keyboardShortcut })
        ]
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsxs(
      command.CommandDialog,
      {
        "data-testid": "search-modal",
        open,
        onOpenChange: setOpen,
        className,
        children: [
          /* @__PURE__ */ jsxRuntime.jsx(
            command.CommandInput,
            {
              "data-testid": "search-input",
              placeholder,
              value: query,
              onValueChange: setQuery
            }
          ),
          /* @__PURE__ */ jsxRuntime.jsxs(command.CommandList, { className: "max-h-[400px]", children: [
            showEmpty && /* @__PURE__ */ jsxRuntime.jsx(command.CommandEmpty, { children: currentEmptyMessage }),
            results.length > 0 && results.map(
              (item, index) => renderResult(item, index, debouncedQuery)
            )
          ] })
        ]
      }
    )
  ] });
}

exports.SearchModal = SearchModal;
exports.default = SearchModal;
