import A from "fuse.js";
import { ref as m, watch as T, computed as k } from "vue";
import { useConfig as B } from "../../hooks/useConfig.js";
import { useNavState as P } from "../../hooks/useNavState.js";
import { useOperation as O } from "../../hooks/useOperation.js";
import { getHeadingsFromMarkdown as z } from "../../libs/markdown.js";
import { extractRequestBody as j, getModels as G } from "../../libs/openapi.js";
import { isHttpMethod as J } from "@scalar/helpers/http/is-http-method";
function w({
  specification: q
}) {
  const { getHeadingId: H, getModelId: L, getTagId: C } = P(), F = B(), a = m([]), d = m([]), i = m(), l = m(""), y = new A(a.value, {
    keys: ["title", "description", "body"]
  }), b = () => {
    i.value = 0, d.value = y.search(l.value);
  };
  T(l, (e) => {
    e.length ? b() : d.value = [];
  });
  function N() {
    l.value = "", i.value = void 0, d.value = [];
  }
  const g = k(() => l.value.length === 0 ? a.value.slice(0, 25).map((f) => ({
    item: f
  })) : d.value.slice(0, 25)), W = k(
    () => typeof i.value == "number" ? g.value[i.value] : void 0
  );
  return T(
    q,
    (e) => {
      var E, x, V;
      a.value = [];
      const f = [], I = z(((E = e == null ? void 0 : e.info) == null ? void 0 : E.description) ?? "");
      if (I.length && (I.forEach((t) => {
        f.push({
          type: "heading",
          title: `Info > ${t.value}`,
          description: "",
          href: `#${H(t)}`,
          tag: t.slug,
          body: ""
        });
      }), a.value = a.value.concat(f)), (x = e == null ? void 0 : e.tags) != null && x.length)
        (V = e == null ? void 0 : e.tags) == null || V.forEach((t) => {
          const h = {
            title: t["x-displayName"] ?? t.name,
            href: `#${C(t)}`,
            description: t.description,
            type: "tag",
            tag: t.name,
            body: ""
          };
          a.value.push(h), t.operations && t.operations.forEach((s) => {
            var u;
            const { parameterMap: o } = O(s), n = j(s.information) || o.value;
            let r = null;
            typeof n != "boolean" && (r = n);
            const c = {
              type: "req",
              title: s.name ?? s.path,
              href: `#${s.id}`,
              operationId: (u = s.information) == null ? void 0 : u.operationId,
              description: s.description ?? "",
              httpVerb: s.httpVerb,
              path: s.path,
              tag: t.name,
              operation: s
            };
            r && (c.body = r), a.value.push(c);
          });
        });
      else if (e != null && e.paths) {
        const t = e.paths;
        Object.keys(t ?? {}).forEach((h) => {
          Object.keys((t == null ? void 0 : t[h]) ?? {}).forEach((s) => {
            var n, r;
            const o = (n = t == null ? void 0 : t[h]) == null ? void 0 : n[s];
            if (J(s) && o) {
              const { parameterMap: c } = O({ ...o, information: o }), u = j(o) || c.value;
              let p = null;
              typeof u != "boolean" && (p = u);
              const R = {
                type: "req",
                title: o.name ?? o.path,
                href: `#${o.id}`,
                operationId: (r = o.information) == null ? void 0 : r.operationId,
                description: o.description ?? "",
                httpVerb: o.httpVerb,
                path: o.path,
                operation: o
              };
              p && (R.body = p), a.value.push(R);
            }
          });
        });
      }
      const M = e == null ? void 0 : e.webhooks, D = [];
      M && M.forEach((t) => {
        D.push({
          type: "webhook",
          title: "Webhook",
          href: `#${t.id}`,
          description: `${t.name}`,
          httpVerb: t.httpVerb,
          tag: t.name,
          body: ""
        }), a.value = a.value.concat(D);
      });
      const v = F.value.hideModels ? {} : G(e), $ = [];
      v && (Object.keys(v).forEach((t) => {
        $.push({
          type: "model",
          title: "Model",
          href: `#${L({ name: t })}`,
          description: v[t].title ?? t,
          tag: t,
          body: ""
        });
      }), a.value = a.value.concat($)), y.setCollection(a.value);
    },
    { immediate: !0 }
  ), {
    resetSearch: N,
    fuseSearch: b,
    selectedSearchIndex: i,
    selectedSearchResult: W,
    searchResultsWithPlaceholderResults: g,
    searchText: l
  };
}
export {
  w as useSearchIndex
};
