import { defineComponent as ee, toRef as te, ref as m, computed as q, watch as U, openBlock as E, createBlock as j, unref as S, withCtx as c, createElementVNode as w, createVNode as V, createTextVNode as g, toDisplayString as b, createElementBlock as oe, Fragment as ae, renderList as le, createSlots as re, pushScopeId as se, popScopeId as ne } from "vue";
import { ScalarModal as ce, ScalarSearchInput as ie, ScalarSearchResultList as de, ScalarSearchResultItem as ue } from "@scalar/components";
import { useMagicKeys as pe, whenever as _ } from "@vueuse/core";
import he from "fuse.js";
import { extractRequestBody as me } from "../helpers/specHelpers.js";
import fe from "./Sidebar/SidebarHttpBadge.vue.js";
import { useSidebar as ve } from "../hooks/useSidebar.js";
import { getHeadingsFromMarkdown as Se } from "../helpers/getHeadingsFromMarkdown.js";
import { useOperation as ge } from "../hooks/useOperation.js";
import { getModels as be } from "../helpers/getModels.js";
import { useNavState as we } from "../hooks/useNavState.js";
const ke = (d) => (se("data-v-9def0ef3"), d = d(), ne(), d), ye = /* @__PURE__ */ ke(() => /* @__PURE__ */ w("div", { class: "ref-search-meta" }, [
  /* @__PURE__ */ w("span", null, "↑↓ Navigate"),
  /* @__PURE__ */ w("span", null, "⏎ Select")
], -1)), xe = /* @__PURE__ */ ee({
  __name: "SearchModal",
  props: {
    parsedSpec: {},
    modalState: {}
  },
  setup(d) {
    const l = d, H = te(l, "parsedSpec"), A = {
      heading: "DocsPage",
      model: "JsonObject",
      req: "Terminal",
      tag: "CodeFolder",
      webhook: "Terminal"
    }, k = pe(), r = m([]), y = m([]), s = m(0), i = m(""), L = m(null), I = new he(r.value, {
      keys: ["title", "description", "body"]
    }), W = () => {
      s.value = 0, y.value = I.search(i.value);
    }, f = q(
      () => u.value[s.value]
    ), { getHeadingId: P, getWebhookId: J, getModelId: K, getOperationId: Y, getTagId: z } = we();
    U(
      () => l.modalState.open,
      (o) => {
        o && (i.value = "", s.value = 0, y.value = []);
      }
    );
    const { setCollapsedSidebarItem: G, hideModels: Q } = ve();
    U(
      H.value,
      async () => {
        var M, $, F, N, x;
        if (r.value = [], !(($ = (M = l.parsedSpec) == null ? void 0 : M.tags) != null && $.length) && !((F = l.parsedSpec) != null && F.webhooks)) {
          I.setCollection([]);
          return;
        }
        const o = [], n = await Se(
          ((N = l.parsedSpec.info) == null ? void 0 : N.description) ?? ""
        );
        n.length && (n.forEach((t) => {
          o.push({
            type: "heading",
            title: `Info > ${t.value}`,
            description: "",
            href: `#${P(t)}`,
            tag: t.slug,
            body: ""
          });
        }), r.value = r.value.concat(o)), (x = l.parsedSpec.tags) == null || x.forEach((t) => {
          const B = {
            title: t["x-displayName"] ?? t.name,
            href: `#${z(t)}`,
            description: t.description,
            type: "tag",
            tag: t.name,
            body: ""
          };
          r.value.push(B), t.operations && t.operations.forEach((a) => {
            const { parameterMap: v } = ge({ operation: a }), O = me(a) || v.value;
            let R = null;
            typeof O != "boolean" && (R = O);
            const T = {
              type: "req",
              title: a.name ?? a.path,
              href: `#${Y(a, t)}`,
              operationId: a.operationId,
              description: a.description ?? "",
              httpVerb: a.httpVerb,
              path: a.path,
              tag: t.name,
              operation: a
            };
            R && (T.body = R), r.value.push(T);
          });
        });
        const e = l.parsedSpec.webhooks, p = [];
        e && Object.keys(e).forEach((t) => {
          Object.keys(
            e[t]
          ).forEach((a) => {
            var v;
            p.push({
              type: "webhook",
              title: `Webhook: ${(v = e[t][a]) == null ? void 0 : v.name}`,
              href: `#${J(t, a)}`,
              description: t,
              httpVerb: a,
              tag: t,
              body: ""
            });
          }), r.value = r.value.concat(p);
        });
        const h = Q.value ? {} : be(l.parsedSpec), D = [];
        h && (Object.keys(h).forEach((t) => {
          D.push({
            type: "model",
            title: "Model",
            href: `#${K(t)}`,
            description: h[t].title ?? t,
            tag: t,
            body: ""
          });
        }), r.value = r.value.concat(D)), I.setCollection(r.value);
      },
      { immediate: !0 }
    ), _(k.enter, () => {
      l.modalState.open && window && (C(f.value), window.location.hash = f.value.item.href, l.modalState.hide());
    }), _(k.ArrowDown, () => {
      var o;
      l.modalState.open && window && (s.value < u.value.length - 1 ? s.value++ : s.value = 0, (o = document.getElementById(f.value.item.href)) == null || o.scrollIntoView({
        behavior: "smooth",
        block: "center"
      }));
    }), _(k.ArrowUp, () => {
      var o;
      l.modalState.open && window && (s.value > 0 ? s.value-- : s.value = u.value.length - 1, (o = document.getElementById(f.value.item.href)) == null || o.scrollIntoView({
        behavior: "smooth",
        block: "center"
      }));
    });
    const u = q(
      () => i.value.length === 0 ? r.value.map((o) => ({
        item: o
      })) : y.value
    ), X = /#(tag\/[^/]*)/, C = (o) => {
      let n = "models";
      const e = o.item.href.match(X);
      e != null && e.length && e.length > 1 && (n = e[1]), G(n, !0), l.modalState.hide();
    };
    function Z(o) {
      const n = new URL(window.location.href);
      return n.hash = o, n.toString();
    }
    return (o, n) => (E(), j(S(ce), {
      state: o.modalState,
      variant: "search"
    }, {
      default: c(() => [
        w("div", {
          ref_key: "searchModalRef",
          ref: L,
          class: "ref-search-container"
        }, [
          V(S(ie), {
            modelValue: i.value,
            "onUpdate:modelValue": n[0] || (n[0] = (e) => i.value = e),
            onInput: W
          }, null, 8, ["modelValue"])
        ], 512),
        V(S(de), {
          class: "ref-search-results custom-scroll",
          noResults: !u.value.length
        }, {
          query: c(() => [
            g(b(i.value), 1)
          ]),
          default: c(() => [
            (E(!0), oe(ae, null, le(u.value, (e, p) => (E(), j(S(ue), {
              id: e.item.href,
              key: e.refIndex,
              active: s.value === p,
              href: Z(e.item.href),
              icon: A[e.item.type],
              onClick: (h) => C(e),
              onFocus: (h) => s.value = p
            }, re({
              default: c(() => [
                g(b(e.item.title) + " ", 1)
              ]),
              _: 2
            }, [
              (e.item.httpVerb || e.item.path) && e.item.path !== e.item.title ? {
                name: "description",
                fn: c(() => [
                  g(b(e.item.path), 1)
                ]),
                key: "0"
              } : e.item.description ? {
                name: "description",
                fn: c(() => [
                  g(b(e.item.description), 1)
                ]),
                key: "1"
              } : void 0,
              e.item.type === "req" ? {
                name: "addon",
                fn: c(() => [
                  V(fe, {
                    method: e.item.httpVerb ?? "get"
                  }, null, 8, ["method"])
                ]),
                key: "2"
              } : void 0
            ]), 1032, ["id", "active", "href", "icon", "onClick", "onFocus"]))), 128))
          ]),
          _: 1
        }, 8, ["noResults"]),
        ye
      ]),
      _: 1
    }, 8, ["state"]));
  }
});
export {
  xe as default
};
