import { useApiClientStore as Q, useOpenApiStore as R } from "@scalar/api-client";
import { ssrState as U } from "@scalar/oas-utils/helpers";
import { ref as v, reactive as X, computed as N, watch as L } from "vue";
import { useNavState as Y } from "./useNavState.js";
import { getLowestHeadingLevel as P } from "../helpers/getLowestHeadingLevel.js";
import { openClientFor as W } from "../helpers/openClientFor.js";
import { hasModels as Z } from "../helpers/hasModels.js";
import { getModels as D } from "../helpers/getModels.js";
import { hasWebhooks as _ } from "../helpers/hasWebhooks.js";
import { getHeadingsFromMarkdown as $ } from "../helpers/getHeadingsFromMarkdown.js";
const {
  getHeadingId: F,
  getModelId: V,
  getOperationId: B,
  getSectionId: ee,
  getTagId: q,
  getWebhookId: g,
  hash: C
} = Y(), o = v(void 0);
function te(t) {
  return o.value = t;
}
const z = v(!1), m = X(
  U["useSidebarContent-collapsedSidebarItems"] ?? {}
);
function se(t) {
  m[t] = !m[t];
}
function S(t, s) {
  m[t] = s;
}
const f = v([]), ie = async (t) => {
  const s = await $(t), l = P(s);
  return s.filter((n) => (
    // highest level, eg. # Introduction
    n.depth === l || // second highest level, eg. ## Authentication
    n.depth === l + 1
  ));
}, J = N(() => {
  var I, A, k, O, y, T, H, M, x, j;
  const { state: t } = Q(), s = {}, {
    openApi: { globalSecurity: l }
  } = R(), n = [];
  let p = null;
  f.value.forEach((e) => {
    var i;
    e.depth === P(f.value) ? (p = {
      id: F(e),
      title: e.value,
      show: !t.showApiClient,
      children: []
    }, n.push(p)) : p && ((i = p.children) == null || i.push({
      id: F(e),
      title: e.value,
      show: !t.showApiClient
    }));
  });
  const c = (A = (I = o.value) == null ? void 0 : I.tags) == null ? void 0 : A[0], h = c && ((e) => (e == null ? void 0 : e.length) !== 1 || e[0].name !== "default" || e[0].description !== "")((k = o.value) == null ? void 0 : k.tags) ? (y = (O = o.value) == null ? void 0 : O.tags) == null ? void 0 : y.filter((e) => {
    var i;
    return ((i = e.operations) == null ? void 0 : i.length) > 0;
  }).map((e) => {
    var i;
    return {
      id: q(e),
      title: e.name,
      displayTitle: e["x-displayName"] ?? e.name,
      show: !0,
      children: (i = e.operations) == null ? void 0 : i.map(
        (r) => {
          var u;
          const a = B(r, e), d = r.name ?? r.path;
          return s[a] = d, {
            id: a,
            title: d,
            httpVerb: r.httpVerb,
            deprecated: ((u = r.information) == null ? void 0 : u.deprecated) ?? !1,
            show: !0,
            select: () => {
              t.showApiClient && W(r, l);
            }
          };
        }
      )
    };
  }) : (T = c == null ? void 0 : c.operations) == null ? void 0 : T.map((e) => {
    var a;
    const i = B(e, c), r = e.name ?? e.path;
    return s[i] = r, {
      id: i,
      title: r,
      httpVerb: e.httpVerb,
      deprecated: ((a = e.information) == null ? void 0 : a.deprecated) ?? !1,
      show: !0,
      select: () => {
        t.showApiClient && W(e, l);
      }
    };
  }), w = Z(o.value) && !z.value ? [
    {
      id: V(),
      title: "Models",
      show: !t.showApiClient,
      children: Object.keys(D(o.value) ?? {}).map(
        (e) => {
          var r;
          const i = V(e);
          return s[i] = e, {
            id: i,
            title: ((r = D(o.value)) == null ? void 0 : r[e]).title ?? e,
            show: !t.showApiClient
          };
        }
      )
    }
  ] : [], b = (H = o.value) != null && H["x-tagGroups"] ? (x = (M = o.value) == null ? void 0 : M["x-tagGroups"]) == null ? void 0 : x.map((e) => {
    const i = [];
    return e.tags.map((a) => {
      if (a === "models" && w.length > 0)
        i.push(w[0]);
      else {
        const d = h == null ? void 0 : h.find(
          (u) => u.title === a
        );
        d && i.push(d);
      }
    }), {
      id: e.name,
      title: e.name,
      children: i,
      show: !0,
      isGroup: !0
    };
  }) : void 0, K = _(o.value) ? [
    {
      id: g(),
      title: "Webhook",
      show: !t.showApiClient,
      children: Object.keys(((j = o.value) == null ? void 0 : j.webhooks) ?? {}).map((e) => {
        var r, a;
        const i = g(e);
        return s[i] = e, Object.keys(
          ((a = (r = o.value) == null ? void 0 : r.webhooks) == null ? void 0 : a[e]) ?? {}
        ).map((d) => {
          var u, E, G;
          return {
            id: g(e, d),
            title: (G = (E = (u = o.value) == null ? void 0 : u.webhooks) == null ? void 0 : E[e][d]) == null ? void 0 : G.name,
            httpVerb: d,
            show: !t.showApiClient
          };
        });
      }).flat()
    }
  ] : [];
  return {
    entries: [
      ...n,
      ...b ?? h ?? [],
      ...b ? [] : K,
      ...b ? [] : w
    ],
    titles: s
  };
}), oe = v(!1), le = N(() => {
  var t, s;
  return ((s = (t = J.value) == null ? void 0 : t.titles) == null ? void 0 : s[C.value]) ?? "";
});
function we(t) {
  return t != null && t.parsedSpec && (o.value = t.parsedSpec, L(
    () => {
      var s, l;
      return (l = (s = o.value) == null ? void 0 : s.tags) == null ? void 0 : l.length;
    },
    () => {
      var s, l;
      if (C.value) {
        const n = ee(C.value);
        n && S(n, !0);
      } else {
        const n = (l = (s = o.value) == null ? void 0 : s.tags) == null ? void 0 : l[0];
        n && S(q(n), !0);
      }
    }
  ), L(
    () => {
      var s, l;
      return (l = (s = o.value) == null ? void 0 : s.info) == null ? void 0 : l.description;
    },
    async () => {
      var l, n;
      const s = (n = (l = o.value) == null ? void 0 : l.info) == null ? void 0 : n.description;
      return s ? f.value = await ie(s) : f.value = [];
    }
  )), {
    breadcrumb: le,
    items: J,
    isSidebarOpen: oe,
    collapsedSidebarItems: m,
    toggleCollapsedSidebarItem: se,
    setCollapsedSidebarItem: S,
    hideModels: z,
    setParsedSpec: te
  };
}
export {
  we as useSidebar
};
