import { defineComponent as F, ref as E, watch as I, onMounted as N, onUnmounted as L, openBlock as r, createElementBlock as d, renderSlot as w, createElementVNode as V, createVNode as G, withCtx as v, Fragment as o, renderList as y, unref as n, toDisplayString as M, createBlock as f, createCommentVNode as h } from "vue";
import m from "./SidebarElement.vue.js";
import g from "./SidebarGroup.vue.js";
import { useNavState as D } from "../../hooks/useNavState.js";
import { useSidebar as H } from "../../hooks/useSidebar.js";
import { sleep as C } from "../../helpers/sleep.js";
const R = { class: "sidebar" }, U = ["aria-label"], j = { class: "sidebar-group-title" }, q = -160, W = /* @__PURE__ */ F({
  __name: "Sidebar",
  props: {
    parsedSpec: {},
    tagsSorter: { type: [String, Function] },
    operationsSorter: { type: [String, Function] }
  },
  setup(A) {
    const S = A, { hash: c, isIntersectionEnabled: $ } = D(), { items: O, toggleCollapsedSidebarItem: T, collapsedSidebarItems: _ } = H(
      {
        parsedSpec: S.parsedSpec,
        tagsSorter: S.tagsSorter,
        operationsSorter: S.operationsSorter
      }
    ), b = E(null), p = E(!0);
    I(c, (s) => {
      !$.value || p.value || typeof window > "u" || k(s);
    });
    const k = (s) => {
      var e, t, l, a;
      const i = document.getElementById(`sidebar-${s}`);
      if (!i || !b.value)
        return;
      let u = q;
      i.getAttribute("data-sidebar-type") === "heading" ? u += i.offsetTop + ((e = i.getElementsByClassName("sidebar-heading")) == null ? void 0 : e[0]).offsetHeight : u += i.offsetTop + (((t = i.parentElement) == null ? void 0 : t.offsetTop) ?? 0) + (((a = (l = i.parentElement) == null ? void 0 : l.parentElement) == null ? void 0 : a.offsetTop) ?? 0), b.value.scrollTo({ top: u, behavior: "smooth" });
    }, B = (s) => {
      if (!b.value)
        return;
      const i = new MutationObserver((u, e) => {
        document.getElementById(`sidebar-${s}`) && (k(s), p.value = !1, e.disconnect());
      });
      return i.observe(b.value, {
        childList: !0,
        subtree: !0
      }), i;
    };
    return N(() => {
      const s = B(c.value);
      c.value || setTimeout(() => p.value = !1, 300), L(() => {
        s == null || s.disconnect();
      });
    }), (s, i) => {
      var u;
      return r(), d("div", R, [
        w(s.$slots, "sidebar-start", {}, void 0, !0),
        V("nav", {
          ref_key: "scrollerEl",
          ref: b,
          "aria-label": `Table of contents for ${(u = s.parsedSpec.info) == null ? void 0 : u.title}`,
          class: "sidebar-pages custom-scroll custom-scroll-self-contain-overflow"
        }, [
          G(g, { level: 0 }, {
            default: v(() => [
              (r(!0), d(o, null, y(n(O).entries, (e) => (r(), d(o, {
                key: e.id
              }, [
                e.isGroup ? (r(), d(o, { key: 0 }, [
                  V("li", j, M(e.displayTitle ?? e.title), 1),
                  (r(!0), d(o, null, y(e.children, (t) => (r(), f(m, {
                    key: t.id,
                    id: `sidebar-${t.id}`,
                    "data-sidebar-type": "heading",
                    hasChildren: t.children && t.children.length > 0,
                    isActive: n(c) === t.id,
                    item: {
                      id: t.id,
                      title: t.displayTitle ?? t.title,
                      select: t.select,
                      httpVerb: t.httpVerb,
                      deprecated: t.deprecated ?? !1
                    },
                    open: n(_)[t.id] ?? !1,
                    onToggleOpen: async () => {
                      p.value = !0, n(T)(t.id), await n(C)(100), p.value = !1;
                    }
                  }, {
                    default: v(() => {
                      var l;
                      return [
                        t.children && ((l = t.children) == null ? void 0 : l.length) > 0 ? (r(), f(g, {
                          key: 0,
                          level: 1
                        }, {
                          default: v(() => [
                            (r(!0), d(o, null, y(t.children, (a) => (r(), d(o, {
                              key: a.id
                            }, [
                              e.show ? (r(), f(m, {
                                key: 0,
                                id: `sidebar-${a.id}`,
                                isActive: n(c) === a.id,
                                item: {
                                  id: a.id,
                                  title: a.displayTitle ?? a.title,
                                  select: a.select,
                                  httpVerb: a.httpVerb,
                                  deprecated: a.deprecated ?? !1
                                }
                              }, null, 8, ["id", "isActive", "item"])) : h("", !0)
                            ], 64))), 128))
                          ]),
                          _: 2
                        }, 1024)) : h("", !0)
                      ];
                    }),
                    _: 2
                  }, 1032, ["id", "hasChildren", "isActive", "item", "open", "onToggleOpen"]))), 128))
                ], 64)) : (r(), d(o, { key: 1 }, [
                  e.show ? (r(), f(m, {
                    key: 0,
                    id: `sidebar-${e.id}`,
                    "data-sidebar-type": "heading",
                    hasChildren: e.children && e.children.length > 0,
                    isActive: n(c) === e.id,
                    item: {
                      id: e.id,
                      title: e.displayTitle ?? e.title,
                      select: e.select,
                      httpVerb: e.httpVerb,
                      deprecated: e.deprecated ?? !1
                    },
                    open: n(_)[e.id] ?? !1,
                    onToggleOpen: async () => {
                      p.value = !0, n(T)(e.id), await n(C)(100), p.value = !1;
                    }
                  }, {
                    default: v(() => {
                      var t;
                      return [
                        e.children && ((t = e.children) == null ? void 0 : t.length) > 0 ? (r(), f(g, {
                          key: 0,
                          level: 1
                        }, {
                          default: v(() => [
                            (r(!0), d(o, null, y(e.children, (l) => (r(), d(o, {
                              key: l.id
                            }, [
                              e.show ? (r(), f(m, {
                                key: 0,
                                id: `sidebar-${l.id}`,
                                isActive: n(c) === l.id,
                                item: {
                                  id: l.id,
                                  title: l.displayTitle ?? l.title,
                                  select: l.select,
                                  httpVerb: l.httpVerb,
                                  deprecated: l.deprecated ?? !1
                                }
                              }, null, 8, ["id", "isActive", "item"])) : h("", !0)
                            ], 64))), 128))
                          ]),
                          _: 2
                        }, 1024)) : h("", !0)
                      ];
                    }),
                    _: 2
                  }, 1032, ["id", "hasChildren", "isActive", "item", "open", "onToggleOpen"])) : h("", !0)
                ], 64))
              ], 64))), 128))
            ]),
            _: 1
          })
        ], 8, U),
        w(s.$slots, "sidebar-end", {}, void 0, !0)
      ]);
    };
  }
});
export {
  W as default
};
