import { defineComponent as ve, computed as T, provide as S, ref as D, onBeforeMount as ge, onMounted as Y, watch as E, toValue as K, onUnmounted as he, useId as Se, createElementBlock as m, openBlock as n, Fragment as z, createElementVNode as p, createVNode as f, normalizeStyle as be, normalizeClass as ye, unref as r, createCommentVNode as i, withDirectives as we, createBlock as b, renderSlot as l, normalizeProps as v, guardReactiveProps as g, withCtx as s, vShow as ke, createSlots as Ce } from "vue";
import { provideUseId as $e } from "@headlessui/vue";
import { OpenApiClientButton as Te } from "@scalar/api-client/components";
import { LAYOUT_SYMBOL as De } from "@scalar/api-client/hooks";
import { WORKSPACE_SYMBOL as Ee, ACTIVE_ENTITIES_SYMBOL as Me } from "@scalar/api-client/store";
import { addScalarClassesToHeadless as Oe, ScalarErrorBoundary as Ie, ScalarSidebarFooter as Be, ScalarColorModeToggleButton as Re, ScalarColorModeToggleIcon as Le } from "@scalar/components";
import { sleep as He } from "@scalar/helpers/testing/sleep";
import { hasObtrusiveScrollbars as _e, getThemeStyles as Ae } from "@scalar/themes";
import { apiReferenceConfigurationSchema as Pe } from "@scalar/types/api-reference";
import { useBreakpoints as Fe } from "@scalar/use-hooks/useBreakpoints";
import { useToasts as Ne, ScalarToasts as Ve } from "@scalar/use-toasts";
import { useMediaQuery as Ue, useResizeObserver as Ye, useDebounceFn as Ke } from "@vueuse/core";
import ze from "./ClassicHeader.vue.js";
import Ge from "./GettingStarted.vue.js";
import Qe from "./MobileHeader.vue.js";
import { parse as We } from "../helpers/parse.js";
import { CONFIGURATION_SYMBOL as je } from "../hooks/useConfig.js";
import { useNavState as qe } from "../hooks/useNavState.js";
import { downloadEventBus as G, downloadDocument as Je } from "../libs/download.js";
import { createEmptySpecification as Xe } from "../libs/openapi.js";
import { useHttpClientStore as Ze } from "../stores/useHttpClientStore.js";
import { useDocumentSource as xe } from "../features/document-source/hooks/useDocumentSource.js";
import { OPENAPI_VERSION_SYMBOL as et } from "../features/download-link/openapi-version-symbol.js";
import { useSidebar as tt } from "../features/sidebar/hooks/useSidebar.js";
import { PLUGIN_MANAGER_SYMBOL as ot } from "../plugins/hooks/usePluginManager.js";
import { createPluginManager as rt } from "../plugins/plugin-manager.js";
import at from "../features/sidebar/components/Sidebar.vue.js";
import Q from "../features/Search/SearchButton.vue.js";
import nt from "./Content/Content.vue.js";
import it from "../features/api-client-modal/ApiClientModal.vue.js";
const st = ["innerHTML"], lt = { class: "references-header" }, ut = ["aria-label"], dt = { class: "references-navigation-list" }, ct = { key: 0 }, mt = {
  key: 1,
  class: "scalar-api-references-standalone-search"
}, ft = { key: 1 }, pt = { class: "references-editor" }, vt = { class: "references-editor-textarea" }, gt = ["aria-label"], ht = {
  key: 0,
  class: "w-64 *:!p-0 empty:hidden"
}, St = {
  key: 0,
  class: "references-footer"
}, qt = /* @__PURE__ */ ve({
  inheritAttrs: !1,
  __name: "ApiReferenceLayout",
  props: {
    configuration: {},
    dereferencedDocument: {},
    originalDocument: {},
    isDark: { type: Boolean },
    parsedSpec: {},
    rawSpec: {}
  },
  emits: ["changeTheme", "updateContent", "loadSwaggerFile", "linkSwaggerFile", "toggleDarkMode"],
  setup(M) {
    const t = T(
      () => Pe.parse(M.configuration)
    ), { initializeToasts: W, toast: j } = Ne();
    W((e) => j(e));
    const {
      originalDocument: q,
      originalOpenApiVersion: J,
      dereferencedDocument: u,
      workspaceStore: X,
      activeEntitiesStore: Z
    } = xe({
      configuration: t,
      dereferencedDocument: M.dereferencedDocument,
      originalDocument: M.originalDocument
    });
    S(et, J), S(Ee, X), S(Me, Z);
    const x = Ue("(min-width: 1150px)"), ee = D("100dvh"), y = D(null);
    Ye(y, (e) => {
      ee.value = e[0].contentRect.height + "px";
    });
    const te = T(_e), R = qe(t), { isSidebarOpen: I, setCollapsedSidebarItem: L, scrollToOperation: oe, items: H } = tt(u, {
      ...R,
      config: t
    }), {
      getReferenceId: re,
      getPathRoutingId: ae,
      getSectionId: ne,
      getTagId: ie,
      hash: O,
      isIntersectionEnabled: _,
      updateHash: A,
      replaceUrlState: se
    } = R;
    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);
    }
    ge(() => {
      A(), Oe();
    });
    const P = async (e) => {
      var o;
      _.value = !1, A(), e ? oe(e) : (o = y.value) == null || o.scrollTo(0, 0), await He(100), _.value = !0;
    }, F = D(0);
    Y(() => {
      var a, k, C;
      history.scrollRestoration = "manual";
      const e = (k = (a = y.value) == null ? void 0 : a.parentElement) == null ? void 0 : k.getBoundingClientRect(), o = (C = y.value) == null ? void 0 : C.getBoundingClientRect();
      if (e && o) {
        const $ = o.top - e.top;
        F.value = $ < 2 ? 0 : $;
      }
      window.onhashchange = () => {
        P(re());
      }, window.onpopstate = () => t.value.pathRouting && P(ae(window.location.pathname)), window.addEventListener("scroll", N, { passive: !0 });
    });
    const le = T(
      () => x.value || !t.value.isEditable
    ), N = Ke(() => {
      window.scrollY < 50 && se("");
    }), ue = D(!1);
    E(u, (e) => {
      var o;
      if (O.value) {
        const a = ne(O.value);
        a && L(a, !0);
      } else {
        const a = (o = e.tags) == null ? void 0 : o[0];
        a && L(ie(a), !0);
      }
      ue.value = !0;
    });
    const w = D(Xe());
    E(
      u,
      async (e) => {
        if (!e)
          return;
        const o = await We(e, H.value.entries);
        w.value = o;
      },
      { immediate: !0 }
    );
    const d = T(() => {
      var e;
      return {
        spec: w.value,
        breadcrumb: ((e = H.value) == null ? void 0 : e.titles.get(O.value)) ?? ""
      };
    });
    Y(
      () => G.on(({ filename: e, format: o }) => {
        Je(
          K(q) || K(M.rawSpec) || "",
          e,
          o
        );
      })
    ), he(() => {
      window.removeEventListener("scroll", N), G.reset();
    }), $e(() => Se()), S(De, "modal"), S(je, t), S(
      ot,
      rt({
        plugins: t.value.plugins
      })
    );
    function V(e, o) {
      E(
        () => t.value[e],
        (a) => {
          typeof a < "u" && o(a);
        },
        { immediate: !0 }
      );
    }
    const { setExcludedClients: de, setDefaultHttpClient: ce } = Ze();
    V("defaultHttpClient", ce), V("hiddenClients", de);
    const me = T(
      () => `<style>
  ${Ae(t.value.theme, {
        fonts: t.value.withDefaultFonts
      })}</style>`
    ), { mediaQueries: fe } = Fe(), pe = !1;
    return E(fe.lg, (e, o) => {
      o && !e && (I.value = !1);
    }), E(O, (e, o) => {
      e && e !== o && (I.value = !1);
    }), (e, o) => {
      var a, k, C, $, U;
      return n(), m(z, null, [
        p("div", { innerHTML: me.value }, null, 8, st),
        p("div", {
          ref_key: "documentEl",
          ref: y,
          class: ye(["scalar-app scalar-api-reference references-layout", [
            {
              "scalar-api-references-standalone-mobile": t.value.showSidebar ?? !0,
              "scalar-scrollbars-obtrusive": te.value,
              "references-editable": t.value.isEditable,
              "references-sidebar": t.value.showSidebar,
              "references-sidebar-mobile-open": r(I),
              "references-classic": t.value.layout === "classic"
            },
            e.$attrs.class
          ]]),
          style: be({
            "--scalar-y-offset": `var(--scalar-custom-header-height, ${F.value}px)`
          })
        }, [
          p("div", lt, [
            t.value.layout === "modern" && (t.value.showSidebar ?? !0) ? (n(), b(Qe, {
              key: 0,
              breadcrumb: d.value.breadcrumb
            }, null, 8, ["breadcrumb"])) : i("", !0),
            l(e.$slots, "header", v(g(d.value)), void 0, !0)
          ]),
          t.value.showSidebar ? (n(), m("aside", {
            key: 0,
            "aria-label": `Sidebar for ${(k = (a = r(u)) == null ? void 0 : a.info) == null ? void 0 : k.title}`,
            class: "references-navigation t-doc__sidebar"
          }, [
            p("div", dt, [
              f(r(Ie), null, {
                default: s(() => {
                  var h, c;
                  return [
                    f(r(at), {
                      title: ((c = (h = r(u)) == null ? void 0 : h.info) == null ? void 0 : c.title) ?? "The OpenAPI Schema"
                    }, {
                      "sidebar-start": s(() => {
                        var B;
                        return [
                          e.$slots["document-selector"] ? (n(), m("div", ct, [
                            l(e.$slots, "document-selector", {}, void 0, !0)
                          ])) : i("", !0),
                          t.value.hideSearch ? i("", !0) : (n(), m("div", mt, [
                            f(r(Q), {
                              searchHotKey: (B = t.value) == null ? void 0 : B.searchHotKey,
                              spec: w.value
                            }, null, 8, ["searchHotKey", "spec"])
                          ])),
                          l(e.$slots, "sidebar-start", v(g(d.value)), void 0, !0)
                        ];
                      }),
                      "sidebar-end": s(() => [
                        l(e.$slots, "sidebar-end", v(g(d.value)), () => [
                          f(r(Be), { class: "darklight-reference" }, {
                            toggle: s(() => [
                              t.value.hideDarkModeToggle ? (n(), m("span", ft)) : (n(), b(r(Re), {
                                key: 0,
                                modelValue: e.isDark,
                                "onUpdate:modelValue": o[0] || (o[0] = (B) => e.$emit("toggleDarkMode"))
                              }, null, 8, ["modelValue"]))
                            ]),
                            default: s(() => [
                              t.value.hideClientButton ? i("", !0) : (n(), b(r(Te), {
                                key: 0,
                                buttonSource: "sidebar",
                                integration: t.value._integration,
                                isDevelopment: pe,
                                url: t.value.url
                              }, null, 8, ["integration", "url"]))
                            ]),
                            _: 1
                          })
                        ], !0)
                      ]),
                      _: 3
                    }, 8, ["title"])
                  ];
                }),
                _: 3
              })
            ])
          ], 8, ut)) : i("", !0),
          we(p("div", pt, [
            p("div", vt, [
              l(e.$slots, "editor", v(g(d.value)), void 0, !0)
            ])
          ], 512), [
            [ke, t.value.isEditable]
          ]),
          le.value ? (n(), m(z, { key: 1 }, [
            p("main", {
              "aria-label": `Open API Documentation for ${($ = (C = r(u)) == null ? void 0 : C.info) == null ? void 0 : $.title}`,
              class: "references-rendered"
            }, [
              f(r(nt), {
                layout: t.value.layout,
                document: r(u),
                parsedSpec: w.value
              }, Ce({
                start: s(() => [
                  l(e.$slots, "content-start", v(g(d.value)), () => [
                    t.value.layout === "classic" ? (n(), b(ze, { key: 0 }, {
                      "dark-mode-toggle": s(() => [
                        t.value.hideDarkModeToggle ? i("", !0) : (n(), b(r(Le), {
                          key: 0,
                          class: "text-c-2 hover:text-c-1",
                          mode: e.isDark ? "dark" : "light",
                          style: { transform: "scale(1.4)" },
                          variant: "icon",
                          onClick: o[1] || (o[1] = (h) => e.$emit("toggleDarkMode"))
                        }, null, 8, ["mode"]))
                      ]),
                      default: s(() => [
                        e.$slots["document-selector"] ? (n(), m("div", ht, [
                          l(e.$slots, "document-selector", {}, void 0, !0)
                        ])) : i("", !0),
                        t.value.hideSearch ? i("", !0) : (n(), b(r(Q), {
                          key: 1,
                          class: "t-doc__sidebar max-w-64",
                          searchHotKey: t.value.searchHotKey,
                          spec: w.value
                        }, null, 8, ["searchHotKey", "spec"]))
                      ]),
                      _: 3
                    })) : i("", !0)
                  ], !0)
                ]),
                end: s(() => [
                  l(e.$slots, "content-end", v(g(d.value)), void 0, !0)
                ]),
                _: 2
              }, [
                (U = t.value) != null && U.isEditable ? {
                  name: "empty-state",
                  fn: s(() => {
                    var h;
                    return [
                      f(Ge, {
                        theme: ((h = t.value) == null ? void 0 : h.theme) || "default",
                        onChangeTheme: o[2] || (o[2] = (c) => e.$emit("changeTheme", c)),
                        onLinkSwaggerFile: o[3] || (o[3] = (c) => e.$emit("linkSwaggerFile")),
                        onLoadSwaggerFile: o[4] || (o[4] = (c) => e.$emit("loadSwaggerFile")),
                        onUpdateContent: o[5] || (o[5] = (c) => e.$emit("updateContent", c))
                      }, null, 8, ["theme"])
                    ];
                  }),
                  key: "0"
                } : void 0
              ]), 1032, ["layout", "document", "parsedSpec"])
            ], 8, gt),
            e.$slots.footer ? (n(), m("div", St, [
              l(e.$slots, "footer", v(g(d.value)), void 0, !0)
            ])) : i("", !0)
          ], 64)) : i("", !0),
          f(r(it), {
            configuration: t.value,
            dereferencedDocument: r(u)
          }, null, 8, ["configuration", "dereferencedDocument"])
        ], 6),
        f(r(Ve))
      ], 64);
    };
  }
});
export {
  qt as default
};
