import { getDefaultHeaders } from "../../../request-block/helpers/get-default-headers.js";
import { filterGlobalCookie } from "../../../operation-block/helpers/filter-global-cookies.js";
import { processBody } from "./process-body.js";
import { processParameters } from "./process-parameters.js";
import { processSecuritySchemes } from "./process-security-schemes.js";
import { processServerUrl } from "./process-server-url.js";
import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
//#region src/v2/blocks/operation-code-sample/helpers/operation-to-har/operation-to-har.ts
/**
* Converts an OpenAPI Operation to a HarRequest format for generating HTTP request snippets.
*
* This function transforms OpenAPI 3.1 operation objects into HAR (HTTP Archive) format requests,
* which can be used to generate code snippets for various programming languages and HTTP clients.
*
* The conversion handles:
* - Server URL processing and path parameter substitution
* - Query parameter formatting based on OpenAPI parameter styles
* - Request body processing with content type handling
* - Security scheme integration (API keys, etc.)
*
* The resulting HarRequest object follows the HAR specification and includes:
* - HTTP method and URL
* - Headers and query parameters
* - Request body (if present)
* - Cookie information
* - Size calculations for headers and body
*
* @see https://w3c.github.io/web-performance/specs/HAR/Overview.html
* @see https://spec.openapis.org/oas/v3.1.0#operation-object
*/
var operationToHar = ({ includeDefaultHeaders = false, operation, contentType, method, path, server = null, example, securitySchemes, globalCookies, requestBodyCompositionSelection }) => {
	const defaultHeaders = includeDefaultHeaders ? getDefaultHeaders({
		method,
		operation,
		exampleKey: example ?? "default",
		hideDisabledHeaders: true
	}).filter((header) => !header.isOverridden) : [];
	const disabledGlobalCookies = operation["x-scalar-disable-parameters"]?.["global-cookies"]?.[example ?? "default"] ?? {};
	const harRequest = {
		method,
		url: processServerUrl(server, path),
		headers: defaultHeaders.map((header) => ({
			name: header.name,
			value: header.defaultValue
		})),
		queryString: [],
		postData: void 0,
		httpVersion: "HTTP/1.1",
		cookies: [],
		headersSize: -1,
		bodySize: -1
	};
	if (operation.parameters) {
		const { url, headers, queryString, cookies } = processParameters({
			harRequest,
			parameters: operation.parameters,
			example
		});
		const filteredGlobalCookies = globalCookies?.filter((cookie) => filterGlobalCookie({
			cookie,
			url,
			disabledGlobalCookies
		}))?.map((cookie) => ({
			name: cookie.name,
			value: cookie.value
		})) ?? [];
		harRequest.url = url;
		harRequest.headers = headers;
		harRequest.queryString = queryString;
		harRequest.cookies = [...filteredGlobalCookies, ...cookies];
	}
	const body = getResolvedRef(operation.requestBody);
	if (body?.content) {
		const postData = processBody({
			requestBody: body,
			contentType,
			example,
			requestBodyCompositionSelection
		});
		if (postData) {
			harRequest.postData = postData;
			harRequest.bodySize = postData.text?.length ?? -1;
			if (postData.mimeType) {
				const existingContentTypeHeader = harRequest.headers.find((header) => header.name.toLowerCase() === "content-type");
				if (existingContentTypeHeader && !existingContentTypeHeader.value) existingContentTypeHeader.value = postData.mimeType;
				else if (!existingContentTypeHeader) harRequest.headers.push({
					name: "Content-Type",
					value: postData.mimeType
				});
			}
		}
	}
	if (securitySchemes) {
		const { headers, queryString, cookies } = processSecuritySchemes(securitySchemes);
		harRequest.headers.push(...headers);
		harRequest.queryString.push(...queryString);
		harRequest.cookies.push(...cookies);
	}
	let headersSize = 0;
	for (const h of harRequest.headers) headersSize += (h.name?.length ?? 0) + 2 + (h.value?.length ?? 0) + 2;
	harRequest.headersSize = headersSize;
	return harRequest;
};
//#endregion
export { operationToHar };

//# sourceMappingURL=operation-to-har.js.map