import { ADD_AUTH_OPTIONS } from "../consts/new-auth-options.js";
import { isDefined } from "@scalar/oas-utils/helpers";
//#region src/views/Request/libs/auth.ts
/** Format a scheme object into a display object */
var formatScheme = (s) => ({
	id: s.uid,
	label: s.type === "openIdConnect" ? `${s.nameKey} (coming soon)` : s.nameKey
});
/** Formats complex security schemes */
var formatComplexScheme = (uids, securitySchemes) => formatScheme(uids.reduce((acc, uid, index) => {
	const scheme = securitySchemes[uid];
	if (scheme) {
		acc.nameKey += `${index > 0 ? " & " : ""}${scheme.nameKey}`;
		acc.uid = `${acc.uid}${index > 0 ? "," : ""}${scheme.uid}`;
	}
	return acc;
}, {
	type: "complex",
	nameKey: "",
	uid: ""
}));
/** Compute what the security requirements should be for a request */
var getSecurityRequirements = (operation, collection) => {
	if (JSON.stringify(operation?.security) === "[{}]" && collection?.security?.length) return Boolean(collection?.security.find((s) => JSON.stringify(s) === "{}")) ? collection.security : [...collection.security, {}];
	return operation?.security ?? collection?.security ?? [];
};
/**
* Generates the options for the security scheme combobox
*
* contains either a flat list, or different groups of required, available, and add new
*/
var getSchemeOptions = (filteredRequirements, collectionSchemeUids, securitySchemes, isReadOnly = false) => {
	{
		/** Creates a dictionary of security schemes by nameKey (not UID) */
		const schemeDict = collectionSchemeUids.reduce((acc, uid) => {
			const scheme = securitySchemes[uid];
			if (scheme) acc[scheme.nameKey] = scheme;
			return acc;
		}, {});
		/** Builds the required schemes formatted as options */
		const requiredFormatted = filteredRequirements.flatMap((r) => {
			const keys = Object.keys(r);
			if (keys.length > 1) return formatComplexScheme(keys.map((k) => schemeDict[k]?.uid).filter(isDefined), securitySchemes);
			if (keys[0]) {
				const scheme = schemeDict[keys[0]];
				if (scheme) return formatScheme(scheme);
			}
			return [];
		});
		/** Collection schemes minus the required ones */
		const availableFormatted = collectionSchemeUids.filter((uid) => !requiredFormatted.some((r) => r.id === uid)).map((uid) => {
			const scheme = securitySchemes[uid];
			if (scheme) return formatScheme(scheme);
			return null;
		}).filter(isDefined);
		const options = [{
			label: "Required authentication",
			options: requiredFormatted
		}, {
			label: "Available authentication",
			options: availableFormatted
		}];
		if (isReadOnly) return requiredFormatted.length ? options : availableFormatted;
		options.push({
			label: "Add new authentication",
			options: ADD_AUTH_OPTIONS
		});
		return options;
	}
};
//#endregion
export { formatComplexScheme, formatScheme, getSchemeOptions, getSecurityRequirements };

//# sourceMappingURL=auth.js.map