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