import { defineComponent as ie, computed as h, ref as R, onBeforeMount as B, onMounted as le, onUnmounted as de, watch as $, onServerPrefetch as ue, useSSRContext as ce, useId as pe, provide as b, openBlock as y, createElementBlock as C, Fragment as _, createElementVNode as d, normalizeClass as fe, unref as i, normalizeStyle as me, renderSlot as u, normalizeProps as c, guardReactiveProps as p, createVNode as m, withCtx as v, createCommentVNode as E, withDirectives as ve, vShow as Se, createSlots as ge } from "vue";
import { provideUseId as he } from "@headlessui/vue";
import { LAYOUT_SYMBOL as be } from "@scalar/api-client/hooks";
import { createWorkspaceStore as we, WORKSPACE_SYMBOL as ye, createActiveEntitiesStore as Ce, ACTIVE_ENTITIES_SYMBOL as Te } from "@scalar/api-client/store";
import { addScalarClassesToHeadless as Re, ScalarErrorBoundary as $e } from "@scalar/components";
import { defaultStateFactory as N } from "@scalar/oas-utils/helpers";
import { hasObtrusiveScrollbars as Ee, getThemeStyles as Oe } from "@scalar/themes";
import { apiReferenceConfigurationSchema as Ie } from "@scalar/types/api-reference";
import { useToasts as Me, ScalarToasts as Pe } from "@scalar/use-toasts";
import { useMediaQuery as ke, useResizeObserver as Le, useDebounceFn as Fe } from "@vueuse/core";
import Ae from "./GettingStarted.vue.js";
import { CONFIGURATION_SYMBOL as Be } from "../hooks/useConfig.js";
import { useHttpClientStore as _e } from "../stores/useHttpClientStore.js";
import { downloadSpecBus as D, downloadSpecFile as Ne } from "../helpers/specDownloads.js";
import { PLUGIN_MANAGER_SYMBOL as De } from "../plugins/hooks/usePluginManager.js";
import { createPluginManager as He } from "../plugins/plugin-manager.js";
import Ue from "./Sidebar/Sidebar.vue.js";
import Ye from "./Content/Content.vue.js";
import ze from "../features/ApiClientModal/ApiClientModal.vue.js";
import { useSidebar as Ge } from "../hooks/useSidebar.js";
import { useNavState as Ve } from "../hooks/useNavState.js";
import { sleep as We } from "../helpers/sleep.js";
const je = ["innerHTML"], Ke = { class: "references-header" }, Qe = ["aria-label"], qe = { class: "references-navigation-list" }, Je = { class: "references-editor" }, Xe = { class: "references-editor-textarea" }, Ze = ["aria-label"], xe = {
  key: 0,
  class: "references-footer"
}, Ct = /* @__PURE__ */ ie({
  inheritAttrs: !1,
  __name: "ApiReferenceLayout",
  props: {
    configuration: {},
    parsedSpec: {},
    rawSpec: {}
  },
  emits: ["changeTheme", "updateContent", "loadSwaggerFile", "linkSwaggerFile", "toggleDarkMode"],
  setup(H) {
    const f = H, t = h(
      () => Ie.parse(f.configuration)
    ), { initializeToasts: U, toast: Y } = Me();
    U((e) => Y(e));
    const z = ke("(min-width: 1150px)"), G = R("100dvh"), S = R(null);
    Le(S, (e) => {
      G.value = e[0].contentRect.height + "px";
    });
    const V = h(Ee), {
      breadcrumb: W,
      collapsedSidebarItems: w,
      isSidebarOpen: j,
      setCollapsedSidebarItem: O,
      hideModels: K,
      defaultOpenAllTags: Q,
      setParsedSpec: q,
      scrollToOperation: J
    } = Ge(), {
      getReferenceId: X,
      getPathRoutingId: I,
      getSectionId: Z,
      getTagId: x,
      hash: ee,
      isIntersectionEnabled: M,
      updateHash: P,
      replaceUrlState: te
    } = Ve(t);
    if (t.value.redirect && typeof window < "u") {
      const e = t.value.redirect(
        (t.value.pathRouting ? window.location.pathname : "") + window.location.hash
      );
      e && history.replaceState({}, "", e);
    }
    B(() => P());
    const k = async (e) => {
      var a;
      M.value = !1, P(), e ? J(e) : (a = S.value) == null || a.scrollTo(0, 0), await We(100), M.value = !0;
    }, L = R(0);
    B(() => Re()), le(() => {
      var o, s, r;
      history.scrollRestoration = "manual", D.on(({ specTitle: n }) => {
        Ne(f.rawSpec, n);
      });
      const e = (s = (o = S.value) == null ? void 0 : o.parentElement) == null ? void 0 : s.getBoundingClientRect(), a = (r = S.value) == null ? void 0 : r.getBoundingClientRect();
      if (e && a) {
        const n = a.top - e.top;
        L.value = n < 2 ? 0 : n;
      }
      window.onhashchange = () => {
        k(X());
      }, window.onpopstate = () => t.value.pathRouting && k(I(window.location.pathname));
    });
    const ae = h(
      () => z.value || !t.value.isEditable
    ), F = Fe((e) => {
      if ((e.target.scrollTop ?? 0) < 50) {
        const o = t.value.pathRouting ? t.value.pathRouting.basePath : window.location.pathname;
        te("", o + window.location.search);
      }
    }), l = h(() => ({
      breadcrumb: W.value,
      spec: f.parsedSpec
    }));
    de(() => {
      D.reset();
    }), $(() => f.parsedSpec, q, { deep: !0 }), ue(() => {
      var a, o;
      const e = ce();
      if (e && (e.payload || (e.payload = { data: N() }), (a = e.payload).data || (a.data = N()), t.value.pathRouting)) {
        const s = I(e.url);
        ee.value = s, e.payload.data.hash = s;
        for (const r in w)
          Object.hasOwn(w, r) && delete w[r];
        if (s)
          O(Z(s), !0);
        else {
          const r = (o = f.parsedSpec.tags) == null ? void 0 : o[0];
          r && O(x(r), !0);
        }
        e.payload.data["useSidebarContent-collapsedSidebarItems"] = w;
      }
    }), he(() => pe());
    const T = we({
      useLocalStorage: !1,
      ...t.value
    });
    $(
      () => f.rawSpec,
      (e) => {
        var a;
        return e && T.importSpecFile(e, "default", {
          documentUrl: ((a = t.value.spec) == null ? void 0 : a.url) ?? t.value.url,
          useCollectionSecurity: !0,
          ...t.value
        });
      },
      { immediate: !0 }
    ), b(ye, T);
    const oe = Ce(T);
    b(Te, oe), b(be, "modal"), b(Be, t), b(
      De,
      He({
        // TODO: Get plugins from the configuration
        plugins: t.value.plugins
      })
    );
    function A(e, a) {
      $(
        () => t.value[e],
        (o) => {
          typeof o < "u" && a(o);
        },
        { immediate: !0 }
      );
    }
    const { setExcludedClients: re, setDefaultHttpClient: se } = _e();
    A("defaultHttpClient", se), A("hiddenClients", re), K.value = t.value.hideModels ?? !1, Q.value = t.value.defaultOpenAllTags ?? !1;
    const ne = h(
      () => `<style>
  ${Oe(t.value.theme, {
        fonts: t.value.withDefaultFonts
      })}</style>`
    );
    return (e, a) => {
      var o, s, r;
      return y(), C(_, null, [
        d("div", { innerHTML: ne.value }, null, 8, je),
        d("div", {
          ref_key: "documentEl",
          ref: S,
          class: fe(["scalar-app scalar-api-reference references-layout", [
            {
              "scalar-scrollbars-obtrusive": V.value,
              "references-editable": t.value.isEditable,
              "references-sidebar": t.value.showSidebar,
              "references-sidebar-mobile-open": i(j),
              "references-classic": t.value.layout === "classic"
            },
            e.$attrs.class
          ]]),
          style: me({
            "--scalar-y-offset": `var(--scalar-custom-header-height, ${L.value}px)`
          }),
          onScrollPassive: a[4] || (a[4] = //@ts-ignore
          (...n) => i(F) && i(F)(...n))
        }, [
          d("div", Ke, [
            u(e.$slots, "header", c(p(l.value)), void 0, !0)
          ]),
          t.value.showSidebar ? (y(), C("aside", {
            key: 0,
            "aria-label": `Sidebar for ${(o = e.parsedSpec.info) == null ? void 0 : o.title}`,
            class: "references-navigation t-doc__sidebar"
          }, [
            d("div", qe, [
              m(i($e), null, {
                default: v(() => [
                  m(i(Ue), {
                    operationsSorter: t.value.operationsSorter,
                    parsedSpec: e.parsedSpec,
                    tagsSorter: t.value.tagsSorter
                  }, {
                    "sidebar-start": v(() => [
                      u(e.$slots, "sidebar-start", c(p(l.value)), void 0, !0)
                    ]),
                    "sidebar-end": v(() => [
                      u(e.$slots, "sidebar-end", c(p(l.value)), void 0, !0)
                    ]),
                    _: 3
                  }, 8, ["operationsSorter", "parsedSpec", "tagsSorter"])
                ]),
                _: 3
              })
            ])
          ], 8, Qe)) : E("", !0),
          ve(d("div", Je, [
            d("div", Xe, [
              u(e.$slots, "editor", c(p(l.value)), void 0, !0)
            ])
          ], 512), [
            [Se, t.value.isEditable]
          ]),
          ae.value ? (y(), C(_, { key: 1 }, [
            d("main", {
              "aria-label": `Open API Documentation for ${(s = e.parsedSpec.info) == null ? void 0 : s.title}`,
              class: "references-rendered"
            }, [
              m(i(Ye), {
                layout: t.value.layout,
                parsedSpec: e.parsedSpec
              }, ge({
                start: v(() => [
                  u(e.$slots, "content-start", c(p(l.value)), void 0, !0)
                ]),
                end: v(() => [
                  u(e.$slots, "content-end", c(p(l.value)), void 0, !0)
                ]),
                _: 2
              }, [
                (r = t.value) != null && r.isEditable ? {
                  name: "empty-state",
                  fn: v(() => {
                    var n;
                    return [
                      m(Ae, {
                        theme: ((n = t.value) == null ? void 0 : n.theme) || "default",
                        onChangeTheme: a[0] || (a[0] = (g) => e.$emit("changeTheme", g)),
                        onLinkSwaggerFile: a[1] || (a[1] = (g) => e.$emit("linkSwaggerFile")),
                        onLoadSwaggerFile: a[2] || (a[2] = (g) => e.$emit("loadSwaggerFile")),
                        onUpdateContent: a[3] || (a[3] = (g) => e.$emit("updateContent", g))
                      }, null, 8, ["theme"])
                    ];
                  }),
                  key: "0"
                } : void 0
              ]), 1032, ["layout", "parsedSpec"])
            ], 8, Ze),
            e.$slots.footer ? (y(), C("div", xe, [
              u(e.$slots, "footer", c(p(l.value)), void 0, !0)
            ])) : E("", !0)
          ], 64)) : E("", !0),
          m(i(ze), { configuration: t.value }, null, 8, ["configuration"])
        ], 38),
        m(i(Pe))
      ], 64);
    };
  }
});
export {
  Ct as default
};
