import { replaceTemplateVariables } from "../string-template.js";
import { cookieSchema } from "@scalar/oas-utils/entities/cookie";
import { shouldUseProxy } from "@scalar/helpers/url/redirect-to-proxy";
//#region src/libs/send-request/set-request-cookies.ts
/**
* The %x2F ("/") character is considered a directory separator, and subdirectories match as well.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
*/
var defaultPath = "/";
/**
* Set all cookie params and workspace level cookies that are applicable
*/
function setRequestCookies({ example, env, globalCookies, serverUrl, proxyUrl }) {
	const cookieParams = [];
	const defaultDomain = determineCookieDomain(shouldUseProxy(proxyUrl, serverUrl) ? proxyUrl : serverUrl ?? "http://localhost");
	globalCookies.forEach((c) => {
		const { name, value, domain: configuredHostname, ...params } = c;
		if (!matchesDomain(serverUrl, configuredHostname) || !name) return;
		cookieParams.push(cookieSchema.parse({
			name,
			value,
			domain: configuredHostname,
			path: params.path
		}));
	});
	example.parameters.cookies.forEach((c) => {
		if (!c.enabled || !c.key) return;
		cookieParams.push(cookieSchema.parse({
			name: c.key,
			value: replaceTemplateVariables(c.value, env),
			domain: defaultDomain,
			path: defaultPath
		}));
	});
	return { cookieParams };
}
/**
* If the Set-Cookie header does not specify a Domain attribute, the cookies are available on the server that sets it
* but not on its subdomains. Therefore, specifying Domain is less restrictive than omitting it.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
*/
var determineCookieDomain = (url) => {
	const hostname = new URL(url.startsWith("http") ? url : `http://${url}`).hostname;
	if (hostname.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)) return hostname;
	if (hostname.match(/^[a-fA-F0-9:]+$/)) return hostname;
	return hostname.startsWith(".") ? hostname : `.${hostname}`;
};
/**
* Matches, when:
* - Isn't scoped to a domain, or
* - matches the current host, or
* - or ends with the current host, or
* - matches the current host with a wildcard.
*/
var matchesDomain = (givenUrl, configuredHostname) => {
	if (!givenUrl || !configuredHostname) return true;
	try {
		const urlWithProtocol = givenUrl.startsWith("http") ? givenUrl : `http://${givenUrl}`;
		const givenHostname = new URL(urlWithProtocol).hostname;
		const noHostnameConfigured = !configuredHostname;
		const hostnameMatches = configuredHostname === givenHostname;
		const domainMatchesWildcard = configuredHostname.startsWith(".") && configuredHostname === `.${givenHostname}`;
		const subdomainMatchesWildcard = configuredHostname.startsWith(".") && givenHostname?.endsWith(configuredHostname);
		return noHostnameConfigured || hostnameMatches || subdomainMatchesWildcard || domainMatchesWildcard;
	} catch {
		return false;
	}
};
/**
* Generate a cookie header from the cookie params
*/
var getCookieHeader = (cookieParams, originalCookieHeader) => {
	const cookieHeader = cookieParams.map((c) => `${c.name}=${c.value}`).join("; ");
	if (originalCookieHeader) return `${originalCookieHeader}; ${cookieHeader}`.trim();
	return cookieHeader.trim();
};
//#endregion
export { getCookieHeader, matchesDomain, setRequestCookies };

//# sourceMappingURL=set-request-cookies.js.map