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