import { mergeUrls } from "@scalar/oas-utils/helpers";
//#region src/views/Components/CodeSnippet/helpers/convert-to-har-request.ts
/**
* Takes in a regular request object and returns a HAR request
* We also Titlecase the headers and remove accept header if it's *
*/
var convertToHarRequest = ({ baseUrl = "", method, body, path, cookies, headers, query }) => {
	const url = mergeUrls(baseUrl, path, void 0, true);
	const harRequest = {
		method: method.toUpperCase(),
		url: url.toString(),
		httpVersion: "HTTP/1.1",
		headers: [],
		queryString: [],
		cookies: [],
		headersSize: -1,
		bodySize: -1
	};
	if (cookies.length) harRequest.cookies = cookies.filter((c) => c.enabled).map(({ key, value }) => ({
		name: key,
		value
	}));
	if (headers.length) harRequest.headers = headers.filter((h) => h.enabled && !(h.key.toLowerCase() === "accept" && h.value === "*/*")).map(({ key, value }) => ({
		name: key.replace(/\b\w/g, (letter) => letter.toUpperCase()),
		value
	}));
	if (query.length) harRequest.queryString = query.filter((q) => q.enabled).map(({ key, value }) => ({
		name: key,
		value
	}));
	if (body) try {
		const contentType = headers.find((h) => h.key.toLowerCase() === "content-type")?.value;
		if (body.activeBody === "formData" && body.formData) {
			const params = [];
			body.formData.value.forEach(({ key, value, file, enabled }) => {
				if (!enabled) return;
				if (file) params.push({
					name: key || "blob",
					value: "BINARY",
					fileName: file.name,
					contentType: file.type || "application/octet-stream"
				});
				else params.push({
					name: key,
					value
				});
			});
			if (body.formData?.encoding === "urlencoded") harRequest.postData = {
				mimeType: contentType || "application/x-www-form-urlencoded",
				params
			};
			else harRequest.postData = {
				mimeType: contentType || "multipart/form-data",
				params
			};
		} else if (body.activeBody === "raw" && body.raw) harRequest.postData = {
			mimeType: contentType || "application/json",
			text: body.raw?.value ?? ""
		};
	} catch (_e) {}
	return harRequest;
};
//#endregion
export { convertToHarRequest };

//# sourceMappingURL=convert-to-har-request.js.map