import { jsxs as s, jsx as t, Fragment as g } from "react/jsx-runtime";
import { forwardRef as G, useState as T, useRef as M, useImperativeHandle as O, useEffect as P, useCallback as p, useMemo as b, Fragment as x } from "react";
import { cn as m } from "../../utils/cn.js";
import { Combobox as X, ComboboxInput as B, ComboboxOptions as J, ComboboxOption as C } from "../../node_modules/.pnpm/@headlessui_react@2.2.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@headlessui/react/dist/components/combobox/combobox.js";
import U from "../../node_modules/.pnpm/lucide-react@0.453.0_react@18.3.1/node_modules/lucide-react/dist/esm/icons/search.js";
import V from "../../node_modules/.pnpm/lucide-react@0.453.0_react@18.3.1/node_modules/lucide-react/dist/esm/icons/loader-circle.js";
import W from "../../node_modules/.pnpm/lucide-react@0.453.0_react@18.3.1/node_modules/lucide-react/dist/esm/icons/search-x.js";
import { Transition as Y, TransitionChild as F } from "../../node_modules/.pnpm/@headlessui_react@2.2.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@headlessui/react/dist/components/transition/transition.js";
import { Dialog as Z, DialogPanel as _ } from "../../node_modules/.pnpm/@headlessui_react@2.2.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@headlessui/react/dist/components/dialog/dialog.js";
import L from "../../node_modules/.pnpm/lucide-react@0.453.0_react@18.3.1/node_modules/lucide-react/dist/esm/icons/chevron-right.js";
const j = ({
  isActive: d,
  header: r,
  description: i,
  icon: o,
  metadata: n
}) => /* @__PURE__ */ s(
  "div",
  {
    className: m(
      "flex w-full cursor-pointer items-start gap-2 rounded-xl border border-transparent bg-transparent px-3 py-2 text-stone-500 transition-colors focus:ring-0 focus:ring-offset-0",
      d && "bg-stone-100 dark:bg-white/5"
    ),
    children: [
      o && /* @__PURE__ */ t("div", { className: "shrink-0 self-center text-stone-700 dark:text-stone-300", children: o }),
      /* @__PURE__ */ s("div", { className: "flex flex-1 flex-col gap-1", children: [
        (n == null ? void 0 : n.breadcrumbs) && n.breadcrumbs.length > 0 && /* @__PURE__ */ t("div", { className: "flex items-center gap-1 truncate text-stone-500 text-xs dark:text-stone-400", children: n.breadcrumbs.map((f, c) => (
          // biome-ignore lint/suspicious/noArrayIndexKey: Breadcrumbs are positional and may contain duplicates
          /* @__PURE__ */ s(x, { children: [
            c > 0 && /* @__PURE__ */ t("span", { className: "text-stone-400", children: " > " }),
            /* @__PURE__ */ t("span", { className: "truncate", children: f })
          ] }, c)
        )) }),
        /* @__PURE__ */ t("div", { className: "truncate font-medium text-sm text-stone-900 dark:text-white", children: r }),
        i && /* @__PURE__ */ t("div", { className: "line-clamp-2 text-sm text-stone-600 dark:text-stone-400", children: i })
      ] }),
      d && /* @__PURE__ */ t(L, { className: "size-5 shrink-0 self-center text-stone-400 dark:text-stone-500" })
    ]
  }
), ee = G(
  ({
    onSearch: d,
    results: r,
    isOpen: i,
    onClose: o,
    isLoading: n = !1,
    placeholder: f = "Search...",
    recentSearches: c = [],
    onSelectResult: h,
    className: z,
    emptyState: S,
    loadingState: D,
    position: y = "top",
    paddingTop: E = "64px"
  }, K) => {
    const [a, k] = T(""), [v, N] = T(!1), w = M(null);
    O(K, () => w.current), P(() => {
      i || (k(""), N(!1));
    }, [i]);
    const R = p(
      (e) => {
        k(e), d(e);
      },
      [d]
    ), u = p(
      (e) => {
        e && (h == null || h(e, a), o());
      },
      [h, a, o]
    ), H = p(
      (e) => {
        if (e.key === "Escape") {
          e.preventDefault(), o();
          return;
        }
        if (e.key === "Tab" && (r.length > 0 || c.length > 0) && (e.preventDefault(), e.currentTarget.setSelectionRange(
          e.currentTarget.value.length,
          e.currentTarget.value.length
        )), e.key === "Enter" && (e.metaKey || e.ctrlKey) && a && r.length > 0 && !n) {
          const l = r[0];
          l && u(l);
        }
      },
      [r, c, u, o, a, n]
    ), I = p((e) => {
      N(e.currentTarget.scrollTop > 0);
    }, []), Q = b(
      () => !a && c.length > 0 && r.length === 0,
      [a, c.length, r.length]
    ), $ = b(
      () => a && r.length > 0 && !n,
      [a, r.length, n]
    ), q = b(
      () => a && !n && r.length === 0,
      [a, n, r.length]
    ), A = /* @__PURE__ */ s(
      X,
      {
        onChange: (e) => {
          e && u(e);
        },
        value: null,
        children: [
          /* @__PURE__ */ s(
            "div",
            {
              className: m(
                "relative z-10 h-14 border-transparent border-b p-1.5 transition",
                v && "border-stone-200 dark:border-white/10"
              ),
              children: [
                /* @__PURE__ */ t(
                  B,
                  {
                    autoComplete: "off",
                    autoFocus: !0,
                    className: m(
                      "peer h-full w-full rounded-xl bg-white pr-14 pl-11 text-stone-950 tracking-tight shadow-sm outline-none ring ring-black/5 transition placeholder:text-stone-400 focus:ring-black/90 dark:bg-stone-900 dark:text-white dark:focus:ring-white placeholder:dark:text-white/50",
                      "[&::-webkit-search-cancel-button]:appearance-none [&::-webkit-search-decoration]:appearance-none",
                      !v && a && "shadow-lg"
                    ),
                    onChange: (e) => R(e.target.value),
                    onKeyDown: H,
                    placeholder: f,
                    ref: w,
                    type: "search",
                    value: a
                  }
                ),
                /* @__PURE__ */ t(
                  U,
                  {
                    className: "absolute top-1/2 left-5 -translate-y-1/2 text-stone-950 opacity-50 peer-focus:opacity-100 dark:text-white",
                    size: 18
                  }
                ),
                a && /* @__PURE__ */ t("div", { className: "absolute top-1/2 right-6 flex -translate-y-1/2 items-center justify-center gap-1 rounded-md bg-stone-950/5 px-1.5 py-1.5 font-medium text-xs text-zinc-950/70 leading-[9px] dark:bg-white/5 dark:text-white/70", children: "ESC" })
              ]
            }
          ),
          /* @__PURE__ */ s(
            J,
            {
              className: "mx-1.5 max-h-[calc(100vh-10rem)] overflow-y-auto",
              onScroll: I,
              static: !0,
              children: [
                Q && /* @__PURE__ */ s(g, { children: [
                  /* @__PURE__ */ t("div", { className: "flex items-center justify-between px-2.5 py-2", children: /* @__PURE__ */ t("span", { className: "truncate text-sm text-stone-500", children: "Recent searches" }) }),
                  c.map((e) => /* @__PURE__ */ t(
                    C,
                    {
                      className: "last:mb-2",
                      value: e,
                      children: ({ focus: l }) => /* @__PURE__ */ t(
                        j,
                        {
                          description: e.content,
                          header: e.header,
                          icon: e.icon,
                          isActive: l,
                          metadata: e.metadata
                        }
                      )
                    },
                    e.id
                  ))
                ] }),
                $ && r.map((e) => /* @__PURE__ */ t(
                  C,
                  {
                    className: "last:mb-2",
                    value: e,
                    children: ({ focus: l }) => /* @__PURE__ */ t(
                      j,
                      {
                        description: e.content,
                        header: e.header,
                        icon: e.icon,
                        isActive: l,
                        metadata: e.metadata
                      }
                    )
                  },
                  e.id
                )),
                n && a && /* @__PURE__ */ t("div", { className: "flex flex-col items-center justify-center gap-3 px-2.5 py-12", children: D || /* @__PURE__ */ s(g, { children: [
                  /* @__PURE__ */ t(V, { className: "size-6 animate-spin text-stone-400 dark:text-stone-500" }),
                  /* @__PURE__ */ s("div", { className: "text-center", children: [
                    /* @__PURE__ */ t("p", { className: "font-medium text-sm text-stone-700 dark:text-stone-300", children: "Searching..." }),
                    /* @__PURE__ */ s("p", { className: "mt-1 text-stone-500 text-xs dark:text-stone-400", children: [
                      'Finding results for "',
                      a,
                      '"'
                    ] })
                  ] })
                ] }) }),
                q && /* @__PURE__ */ t("div", { className: "flex flex-col items-center justify-center gap-3 px-2.5 py-12", children: S || /* @__PURE__ */ s(g, { children: [
                  /* @__PURE__ */ t(W, { className: "size-6 text-stone-300 dark:text-stone-600" }),
                  /* @__PURE__ */ s("div", { className: "text-center", children: [
                    /* @__PURE__ */ t("p", { className: "font-medium text-sm text-stone-700 dark:text-stone-300", children: "No results found" }),
                    /* @__PURE__ */ t("p", { className: "mt-1 text-stone-500 text-xs dark:text-stone-400", children: "Try searching with different keywords" })
                  ] })
                ] }) })
              ]
            }
          )
        ]
      }
    );
    return /* @__PURE__ */ t(Y, { appear: !0, as: x, show: i, children: /* @__PURE__ */ s(
      Z,
      {
        as: "div",
        className: m("relative z-50", z),
        onClose: o,
        children: [
          /* @__PURE__ */ t(
            F,
            {
              as: x,
              enter: "ease-out duration-100",
              enterFrom: "opacity-0",
              enterTo: "opacity-100",
              leave: "ease-in duration-50",
              leaveFrom: "opacity-100",
              leaveTo: "opacity-0",
              children: /* @__PURE__ */ t("div", { className: "fixed inset-0 bg-black/20 backdrop-blur-sm transition-opacity dark:bg-black/40" })
            }
          ),
          /* @__PURE__ */ t(
            "div",
            {
              className: m(
                "fixed inset-0 z-10 flex justify-center p-4",
                y === "top" ? "items-start" : "items-center"
              ),
              style: y === "top" ? { paddingTop: E } : void 0,
              children: /* @__PURE__ */ t(
                F,
                {
                  as: x,
                  enter: "ease-out duration-100",
                  enterFrom: "opacity-0 scale-95",
                  enterTo: "opacity-100 scale-100",
                  leave: "ease-in duration-50",
                  leaveFrom: "opacity-100 scale-100",
                  leaveTo: "opacity-0 scale-95",
                  children: /* @__PURE__ */ t(_, { className: "flex w-full max-w-[640px] flex-col overflow-hidden rounded-2xl border border-stone-200 bg-white shadow-2xl dark:border-white/10 dark:bg-stone-900", children: A })
                }
              )
            }
          )
        ]
      }
    ) });
  }
);
ee.displayName = "Search";
export {
  ee as Search,
  j as SearchHit
};
