import { isAuthOptional } from "../../../blocks/scalar-auth-selector-block/helpers/is-auth-optional.js";
import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
//#region src/v2/features/operation/helpers/get-selected-security.ts
var applyDefaultScopes = (requirement, securitySchemes) => {
	let didApplyDefaultScopes = false;
	const hydratedRequirement = Object.fromEntries(Object.entries(requirement).map(([name, scopes]) => {
		if (Array.isArray(scopes) && scopes.length > 0) return [name, scopes];
		const scheme = getResolvedRef(securitySchemes[name]);
		const defaultScopes = scheme?.type === "oauth2" ? scheme["x-default-scopes"] : void 0;
		if (Array.isArray(defaultScopes) && defaultScopes.length > 0) {
			didApplyDefaultScopes = true;
			return [name, [...defaultScopes]];
		}
		return [name, scopes];
	}));
	return didApplyDefaultScopes ? hydratedRequirement : requirement;
};
/**
* Resolves which security selection to use for an operation.
* Priority: operation-level selection, then document-level selection, then a default.
* When neither level has a selection, returns the first security requirement unless
* authentication is optional or there are no requirements, in which case returns no selection.
*/
var getSelectedSecurity = (documentSelectedSecurity, operationSelectedSecurity, securityRequirements = [], securitySchemes = {}) => {
	if (operationSelectedSecurity) return operationSelectedSecurity;
	if (documentSelectedSecurity) return documentSelectedSecurity;
	const isOptional = isAuthOptional(securityRequirements);
	const firstRequirement = securityRequirements[0];
	if (isOptional || !firstRequirement) return {
		selectedIndex: -1,
		selectedSchemes: []
	};
	return {
		selectedIndex: 0,
		selectedSchemes: [applyDefaultScopes(firstRequirement, securitySchemes)]
	};
};
//#endregion
export { getSelectedSecurity };

//# sourceMappingURL=get-selected-security.js.map