import { defineComponent as Y, toRef as A, ref as W, watch as x, createBlock as $, openBlock as h, unref as s, withCtx as l, createElementVNode as n, createVNode as g, withKeys as b, withModifiers as R, isRef as j, createElementBlock as y, Fragment as E, renderList as G, createSlots as J, normalizeClass as Q, createTextVNode as d, createCommentVNode as X, toDisplayString as c } from "vue";
import { ScalarModal as Z, ScalarSearchInput as _, ScalarSearchResultList as ee, ScalarSearchResultItem as te } from "@scalar/components";
import { scrollToId as N } from "@scalar/helpers/dom/scroll-to-id";
import { nanoid as oe } from "nanoid";
import { lazyBus as ae } from "../../components/Content/Lazy/lazyBus.js";
import { useSearchIndex as se } from "./useSearchIndex.js";
import ne from "../sidebar/components/SidebarHttpBadge.vue.js";
import { useSidebar as re } from "../sidebar/hooks/useSidebar.js";
const le = { class: "sr-only" }, ie = { class: "sr-only" }, ve = /* @__PURE__ */ Y({
  __name: "SearchModal",
  props: {
    parsedSpec: {},
    modalState: {}
  },
  setup(F) {
    const m = F, q = A(m, "parsedSpec"), S = oe(), I = `${S}-search-result`, w = `${S}-search-instructions`, v = (o) => `${S}${o}`, {
      resetSearch: D,
      fuseSearch: L,
      selectedSearchIndex: r,
      selectedSearchResult: k,
      searchResultsWithPlaceholderResults: u,
      searchText: p
    } = se({
      specification: q
    }), M = {
      heading: "DocsPage",
      model: "Brackets",
      req: "Terminal",
      tag: "CodeFolder",
      webhook: "Terminal"
    }, P = {
      heading: "Document Heading",
      req: "Request",
      tag: "Tag",
      model: "",
      // The title of the entry is already "Model"
      webhook: ""
      // The title of the entry is already "Webhook"
    }, H = W(null);
    x(
      () => m.modalState.open,
      (o) => {
        o && D();
      }
    );
    const { setCollapsedSidebarItem: U } = re(), K = /#(tag\/[^/]*)/;
    function V(o) {
      let t = "models";
      const e = o.item.href.match(K);
      e != null && e.length && e.length > 1 && (t = e[1]), U(t, !0);
      const a = o.item.href.replace("#", "");
      if (document.getElementById(a))
        N(a), m.modalState.hide();
      else {
        const i = ae.on((f) => {
          f.id === a && (N(a), i(), m.modalState.hide());
        });
      }
    }
    x(r, (o) => {
      var a;
      if (typeof o != "number")
        return;
      const t = u.value[o], e = v(t == null ? void 0 : t.item.href);
      (a = document.getElementById(e)) == null || a.scrollIntoView({
        behavior: "smooth",
        block: "nearest"
      });
    });
    const B = (o) => {
      const t = o === "up" ? -1 : 1, e = u.value.length;
      if (typeof r.value == "number") {
        const a = (r.value + t + e) % e;
        r.value = a;
      } else
        r.value = t === -1 ? e - 1 : 0;
    };
    function z(o) {
      const t = new URL(window.location.href);
      return t.hash = o, t.toString();
    }
    function O() {
      if (!r.value)
        return;
      const o = u.value;
      V(o[r.value]);
    }
    return (o, t) => (h(), $(s(Z), {
      "aria-label": "Reference Search",
      state: o.modalState,
      variant: "search"
    }, {
      default: l(() => [
        n("div", {
          ref_key: "searchModalRef",
          ref: H,
          class: "ref-search-container",
          role: "search"
        }, [
          g(s(_), {
            modelValue: s(p),
            "onUpdate:modelValue": t[0] || (t[0] = (e) => j(p) ? p.value = e : null),
            "aria-activedescendant": s(k) ? v(s(k).item.href) : void 0,
            "aria-autocomplete": "list",
            "aria-controls": I,
            "aria-describedby": w,
            role: "combobox",
            onBlur: t[1] || (t[1] = (e) => r.value = void 0),
            onInput: s(L),
            onKeydown: [
              t[2] || (t[2] = b(R((e) => B("down"), ["stop", "prevent"]), ["down"])),
              b(R(O, ["stop", "prevent"]), ["enter"]),
              t[3] || (t[3] = b(R((e) => B("up"), ["stop", "prevent"]), ["up"]))
            ]
          }, null, 8, ["modelValue", "aria-activedescendant", "onInput", "onKeydown"])
        ], 512),
        g(s(ee), {
          id: I,
          "aria-label": "Reference Search Results",
          class: "ref-search-results custom-scroll",
          noResults: !s(u).length
        }, {
          query: l(() => [
            d(c(s(p)), 1)
          ]),
          default: l(() => [
            (h(!0), y(E, null, G(s(u), (e, a) => (h(), $(s(te), {
              id: v(e.item.href),
              key: e.refIndex,
              href: z(e.item.href),
              icon: M[e.item.type],
              selected: s(r) === a,
              onClick: (i) => V(e),
              onFocus: (i) => r.value = a
            }, J({
              default: l(() => {
                var i, f, C, T;
                return [
                  n("span", {
                    class: Q({
                      deprecated: (f = (i = e.item.operation) == null ? void 0 : i.information) == null ? void 0 : f.deprecated
                    })
                  }, [
                    n("span", le, [
                      d(c(P[e.item.type]) + ":  ", 1),
                      (T = (C = e.item.operation) == null ? void 0 : C.information) != null && T.deprecated ? (h(), y(E, { key: 0 }, [
                        d(" (Deprecated)  ")
                      ], 64)) : X("", !0)
                    ]),
                    d(" " + c(e.item.title) + " ", 1),
                    t[4] || (t[4] = n("span", { class: "sr-only" }, ",", -1))
                  ], 2)
                ];
              }),
              _: 2
            }, [
              e.item.type !== "webhook" && (e.item.httpVerb || e.item.path) && e.item.path !== e.item.title ? {
                name: "description",
                fn: l(() => [
                  t[5] || (t[5] = n("span", { class: "sr-only" }, "Path: ", -1)),
                  d(" " + c(e.item.path), 1)
                ]),
                key: "0"
              } : e.item.description ? {
                name: "description",
                fn: l(() => [
                  t[6] || (t[6] = n("span", { class: "sr-only" }, "Description: ", -1)),
                  d(" " + c(e.item.description), 1)
                ]),
                key: "1"
              } : void 0,
              e.item.type === "req" ? {
                name: "addon",
                fn: l(() => [
                  g(ne, {
                    "aria-hidden": "true",
                    method: e.item.httpVerb ?? "get"
                  }, null, 8, ["method"]),
                  n("span", ie, " HTTP Method: " + c(e.item.httpVerb ?? "get") + ", ", 1)
                ]),
                key: "2"
              } : void 0
            ]), 1032, ["id", "href", "icon", "selected", "onClick", "onFocus"]))), 128))
          ]),
          _: 1
        }, 8, ["noResults"]),
        n("div", {
          id: w,
          class: "ref-search-meta"
        }, t[7] || (t[7] = [
          n("span", {
            "aria-hidden": "true",
            class: "contents"
          }, [
            n("span", null, "↑↓ Navigate"),
            n("span", null, "⏎ Select")
          ], -1),
          n("span", { class: "sr-only" }, " Press up arrow / down arrow to navigate, enter to select, type to filter results ", -1)
        ]))
      ]),
      _: 1
    }, 8, ["state"]));
  }
});
export {
  ve as default
};
