import { replaceTemplateVariables } from "../string-template.js";
import { isDefined } from "@scalar/oas-utils/helpers";
import { cookieSchema } from "@scalar/oas-utils/entities/cookie";
import { encode } from "js-base64";
//#region src/libs/send-request/build-request-security.ts
/**
* Generates the headers, cookies and query params for selected security schemes
* In the future we can add customization for where the security is applied
*/
var buildRequestSecurity = (securitySchemes = [], env = {}, emptyTokenPlaceholder = "") => {
	const headers = {};
	const cookies = [];
	const urlParams = new URLSearchParams();
	securitySchemes.forEach((scheme) => {
		if (scheme.type === "apiKey") {
			const value = replaceTemplateVariables(scheme.value, env) || emptyTokenPlaceholder;
			if (scheme.in === "header") headers[scheme.name] = value;
			if (scheme.in === "query") urlParams.append(scheme.name, value);
			if (scheme.in === "cookie") cookies.push(cookieSchema.parse({
				uid: scheme.uid,
				name: scheme.name,
				value,
				path: "/"
			}));
		}
		if (scheme.type === "http") if (scheme.scheme === "basic") {
			const value = `${replaceTemplateVariables(scheme.username, env)}:${replaceTemplateVariables(scheme.password, env)}`;
			headers["Authorization"] = `Basic ${value === ":" ? "username:password" : encode(value)}`;
		} else headers["Authorization"] = `Bearer ${replaceTemplateVariables(scheme.token, env) || emptyTokenPlaceholder}`;
		if (scheme.type === "oauth2") headers["Authorization"] = `Bearer ${Object.values(scheme.flows).filter(isDefined).find((f) => f.token)?.token || emptyTokenPlaceholder}`;
	});
	return {
		headers,
		cookies,
		urlParams
	};
};
//#endregion
export { buildRequestSecurity };

//# sourceMappingURL=build-request-security.js.map