import { defineComponent, computed, watch, createBlock, createCommentVNode, openBlock, unref } from "vue";
import { AuthSelector } from "@scalar/api-client/v2/blocks/scalar-auth-selector-block";
import { getSecurityRequirements, getSelectedSecurity } from "@scalar/api-client/v2/features/operation";
import { getDefaultSecurity } from "./helpers/get-default-security.js";
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "Auth",
  props: {
    options: {},
    authStore: {},
    document: {},
    eventBus: {},
    securitySchemes: {},
    selectedServer: {},
    environment: {}
  },
  setup(__props) {
    const securityRequirements = computed(
      () => getSecurityRequirements(__props.document?.security)
    );
    const documentSelectedSecurity = computed(
      () => __props.authStore.getAuthSelectedSchemas({
        type: "document",
        documentName: __props.document?.["x-scalar-navigation"]?.name ?? ""
      })
    );
    const selectedSecurity = computed(
      () => getSelectedSecurity(
        documentSelectedSecurity.value,
        void 0,
        securityRequirements.value
      )
    );
    watch(
      documentSelectedSecurity,
      (newDocumentSelectedSecurity) => {
        if (typeof newDocumentSelectedSecurity !== "undefined") {
          return;
        }
        const defaultSecurity = getDefaultSecurity(
          securityRequirements.value,
          __props.options.authentication?.preferredSecurityScheme,
          __props.securitySchemes
        );
        if (!defaultSecurity) {
          return;
        }
        __props.eventBus.emit("auth:update:selected-security-schemes", {
          selectedRequirements: [defaultSecurity],
          newSchemes: [],
          meta: { type: "document" }
        });
      },
      { immediate: true }
    );
    return (_ctx, _cache) => {
      return Object.keys(__props.securitySchemes).length ? (openBlock(), createBlock(unref(AuthSelector), {
        key: 0,
        createAnySecurityScheme: __props.options.authentication?.createAnySecurityScheme ?? false,
        environment: __props.environment,
        eventBus: __props.eventBus,
        isStatic: "",
        layout: "reference",
        meta: { type: "document" },
        persistAuth: __props.options.persistAuth,
        proxyUrl: __props.options.proxyUrl ?? "",
        securityRequirements: securityRequirements.value,
        securitySchemes: __props.securitySchemes,
        selectedSecurity: selectedSecurity.value,
        server: __props.selectedServer,
        title: "Authentication"
      }, null, 8, ["createAnySecurityScheme", "environment", "eventBus", "persistAuth", "proxyUrl", "securityRequirements", "securitySchemes", "selectedSecurity", "server"])) : createCommentVNode("", true);
    };
  }
});
export {
  _sfc_main as default
};
