import AuthSelector_default from "../../../blocks/scalar-auth-selector-block/components/AuthSelector.vue.js";
import { getSelectedSecurity } from "../../operation/helpers/get-selected-security.js";
import { getActiveProxyUrl } from "../../../helpers/get-active-proxy-url.js";
import { getServers } from "../../../helpers/get-servers.js";
import { getDefaultOperationSecurityToggle } from "../helpers/get-default-operation-security-toggle.js";
import Section_default from "../../settings/components/Section.vue.js";
import { Fragment, computed, createBlock, createElementBlock, createElementVNode, createSlots, createTextVNode, createVNode, defineComponent, normalizeClass, openBlock, ref, unref, watchEffect, withCtx } from "vue";
import { ScalarToggle } from "@scalar/components";
import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { unpackProxyObject } from "@scalar/workspace-store/helpers/unpack-proxy";
import { isHttpMethod } from "@scalar/helpers/http/is-http-method";
//#region src/v2/features/collection/components/Authentication.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "flex h-8 items-center" };
var Authentication_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "Authentication",
	props: {
		documentSlug: {},
		document: {},
		eventBus: {},
		layout: {},
		path: {},
		method: {},
		exampleName: {},
		environment: {},
		securitySchemes: {},
		workspaceStore: {},
		activeWorkspace: {},
		plugins: {},
		customThemes: {},
		currentTheme: {},
		isDarkMode: { type: Boolean },
		fetchRegistryDocument: { type: Function },
		telemetry: { type: Boolean },
		onUpdateTelemetry: { type: Function },
		collectionType: {}
	},
	setup(__props) {
		/**
		* Compute the authentication metadata based on the current collection type.
		* If we're working with an operation, include its path and method; otherwise, use the document scope.
		*/
		const authMeta = computed(() => {
			if (__props.collectionType === "operation") return {
				type: "operation",
				path: __props.path ?? "",
				method: __props.method ?? "get"
			};
			return { type: "document" };
		});
		/**
		* Compute the operation object based on the current collection type.
		*/
		const operation = computed(() => {
			if (__props.collectionType === "operation") {
				if (!__props.path || !isHttpMethod(__props.method)) return null;
				return getResolvedRef(__props.document?.paths?.[__props.path]?.[__props.method]);
			}
			return null;
		});
		/**
		* If enabled we use/set the selected security schemes on the operation level
		*/
		const useOperationSecurity = ref(false);
		watchEffect(() => {
			useOperationSecurity.value = getDefaultOperationSecurityToggle({
				authStore: __props.workspaceStore.auth,
				documentName: __props.documentSlug,
				...authMeta.value
			});
		});
		/** Resolved selected security for the current collection (operation or document), with defaults applied */
		const selectedSecurity = computed(() => {
			if (__props.collectionType === "operation") return getSelectedSecurity(void 0, __props.workspaceStore.auth.getAuthSelectedSchemas({
				type: "operation",
				documentName: __props.documentSlug,
				path: __props.path ?? "",
				method: __props.method ?? "get"
			}), operation.value?.security ?? [], __props.securitySchemes);
			return getSelectedSecurity(__props.workspaceStore.auth.getAuthSelectedSchemas({
				type: "document",
				documentName: __props.documentSlug
			}), void 0, __props.document?.security ?? [], __props.securitySchemes);
		});
		/** Compute the security requirements for the operation or document based on the current collection type */
		const securityRequirements = computed(() => {
			if (__props.collectionType === "operation") return operation.value?.security ?? [];
			return __props.document?.security ?? [];
		});
		/** Compute the proxy URL for the current layout (for the electron we don't want to use the proxy by default) */
		const proxyUrl = computed(() => getActiveProxyUrl(__props.workspaceStore.workspace["x-scalar-active-proxy"], __props.layout) ?? "");
		const servers = computed(() => {
			return getServers(operation.value?.servers ?? __props.document?.servers, { documentUrl: __props.document?.["x-scalar-original-source-url"] });
		});
		/** Grab the currently selected server for relative auth URIs */
		const server = computed(() => {
			const documentServer = __props.document?.["x-scalar-selected-server"];
			const selectedServerUrl = operation.value?.["x-scalar-selected-server"] ?? documentServer;
			return servers.value.find(({ url }) => url === selectedServerUrl) ?? servers.value[0] ?? null;
		});
		/**
		* Handles toggling operation-level security authentication. (Only for operation collections)
		* When enabled (`value` is true), overrides document-level authentication for the current operation.
		* When disabled (`value` is false), reverts to using document-level authentication instead.
		*/
		const handleToggleOperationSecurity = (value) => {
			if (authMeta.value.type !== "operation") return;
			useOperationSecurity.value = value;
			if (value) {
				const { selectedSchemes } = selectedSecurity.value;
				return __props.eventBus.emit("auth:update:selected-security-schemes", {
					selectedRequirements: unpackProxyObject(selectedSchemes, { depth: 1 }),
					newSchemes: [],
					meta: authMeta.value
				});
			}
			return __props.eventBus.emit("auth:clear:selected-security-schemes", { meta: authMeta.value });
		};
		return (_ctx, _cache) => {
			return openBlock(), createBlock(Section_default, null, createSlots({
				title: withCtx(() => [_cache[0] || (_cache[0] = createTextVNode("Authentication", -1))]),
				description: withCtx(() => [__props.collectionType === "operation" ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [
					_cache[1] || (_cache[1] = createElementVNode("span", { class: "block" }, " Override authentication for this operation with the toggle. ", -1)),
					_cache[2] || (_cache[2] = createElementVNode("span", { class: "mt-1 block" }, [createElementVNode("strong", null, "On"), createTextVNode(" — Authentication below applies only to this operation. ")], -1)),
					_cache[3] || (_cache[3] = createElementVNode("span", { class: "mt-1 block" }, [createElementVNode("strong", null, "Off"), createTextVNode(" — This operation uses document-level authentication from the OpenAPI spec. ")], -1))
				], 64)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [createTextVNode(" Configure authentication for this document. Selected authentication applies to all operations unless overridden at the operation level. ")], 64))]),
				default: withCtx(() => [createElementVNode("div", { class: normalizeClass(__props.collectionType === "operation" && !useOperationSecurity.value && "cursor-not-allowed") }, [createVNode(unref(AuthSelector_default), {
					class: normalizeClass(["scalar-collection-auth border-none!", __props.collectionType === "operation" && !useOperationSecurity.value && "pointer-events-none opacity-50 mix-blend-luminosity"]),
					createAnySecurityScheme: true,
					environment: __props.environment,
					eventBus: __props.eventBus,
					isStatic: "",
					meta: authMeta.value,
					proxyUrl: proxyUrl.value,
					securityRequirements: securityRequirements.value,
					securitySchemes: __props.securitySchemes,
					selectedSecurity: selectedSecurity.value,
					server: server.value,
					title: "Authentication"
				}, null, 8, [
					"class",
					"environment",
					"eventBus",
					"meta",
					"proxyUrl",
					"securityRequirements",
					"securitySchemes",
					"selectedSecurity",
					"server"
				])], 2)]),
				_: 2
			}, [__props.collectionType === "operation" ? {
				name: "actions",
				fn: withCtx(() => [createElementVNode("div", _hoisted_1, [createVNode(unref(ScalarToggle), {
					class: "w-4",
					modelValue: useOperationSecurity.value,
					"onUpdate:modelValue": handleToggleOperationSecurity
				}, null, 8, ["modelValue"])])]),
				key: "0"
			} : void 0]), 1024);
		};
	}
});
//#endregion
export { Authentication_vue_vue_type_script_setup_true_lang_default as default };

//# sourceMappingURL=Authentication.vue.script.js.map