import { filterGlobalCookie } from "./filter-global-cookies.js";
import { replaceEnvVariables } from "@scalar/helpers/regex/replace-variables";
import { xScalarCookieSchema } from "@scalar/workspace-store/schemas/extensions/general/x-scalar-cookies";
import { coerceValue } from "@scalar/workspace-store/schemas/typebox-coerce";
//#region src/v2/blocks/operation-block/helpers/build-request-cookie-header.ts
var CUSTOM_COOKIE_HEADER_WARNING = "We're using a `X-Scalar-Cookie` custom header to the request. The proxy will forward this as a `Cookie` header. We do this to avoid the browser omitting the `Cookie` header for cross-origin requests for security reasons.";
var COOKIE_HEADER_WARNING = `We're trying to add a Cookie header, but browsers often omit them for cross-origin requests for various security reasons. If it's not working, that's probably why. Here are the requirements for it to work:

        - The browser URL must be on the same domain as the server URL.
        - The connection must be made over HTTPS.
        `;
/**
* Generate a cookie header from the cookie params
*/
var getCookieHeader = (cookieParams, originalCookieHeader) => {
	const cookieHeader = cookieParams.map((c) => `${c.name}=${c.value}`).join("; ");
	if (originalCookieHeader && cookieHeader) return `${originalCookieHeader}; ${cookieHeader}`;
	return originalCookieHeader || cookieHeader || "";
};
/**
* Build out the cookies header taking in global, param and security scheme cookies
*/
var buildRequestCookieHeader = ({ paramCookies, globalCookies, env, originalCookieHeader, url, useCustomCookieHeader, disabledGlobalCookies }) => {
	/** Generate the cookie header */
	const cookieHeader = getCookieHeader([...globalCookies.filter((cookie) => filterGlobalCookie({
		cookie,
		url,
		disabledGlobalCookies
	})).map((cookie) => {
		return coerceValue(xScalarCookieSchema, {
			...cookie,
			name: replaceEnvVariables(cookie.name, env),
			value: replaceEnvVariables(cookie.value, env)
		});
	}), ...paramCookies], originalCookieHeader ?? void 0);
	if (cookieHeader) {
		if (useCustomCookieHeader) {
			console.warn(CUSTOM_COOKIE_HEADER_WARNING);
			return {
				name: "X-Scalar-Cookie",
				value: cookieHeader
			};
		}
		console.warn(COOKIE_HEADER_WARNING);
		return {
			name: "Cookie",
			value: cookieHeader
		};
	}
	return null;
};
//#endregion
export { buildRequestCookieHeader };

//# sourceMappingURL=build-request-cookie-header.js.map