import { replaceTemplateVariables } from "../string-template.js";
import { canMethodHaveBody } from "@scalar/oas-utils/helpers";
//#region src/libs/send-request/create-fetch-body.ts
/**
* Create the fetch request body from an example
*
* TODO: Should we be setting the content type headers here?
* If so we must allow the user to override the content type header
*/
function createFetchBody(method, example, env) {
	if (!canMethodHaveBody(method)) return {
		body: void 0,
		contentType: void 0
	};
	if (example.body.activeBody === "formData" && example.body.formData) {
		const contentType = example.body.formData.encoding === "form-data" ? "multipart/form-data" : "application/x-www-form-urlencoded";
		const form = example.body.formData.encoding === "form-data" ? new FormData() : new URLSearchParams();
		example.body.formData.value.forEach((entry) => {
			if (!entry.enabled || !entry.key) return;
			if (entry.file && form instanceof FormData) form.append(entry.key, entry.file, entry.file.name);
			else if (entry.value !== void 0) form.append(entry.key, replaceTemplateVariables(entry.value, env));
		});
		return {
			body: form,
			contentType
		};
	}
	if (example.body.activeBody === "raw") return {
		body: replaceTemplateVariables(example.body.raw?.value ?? "", env),
		contentType: example.body.raw?.encoding
	};
	if (example.body.activeBody === "binary") return {
		body: example.body.binary,
		contentType: example.body.binary?.type
	};
	return {
		body: void 0,
		contentType: void 0
	};
}
//#endregion
export { createFetchBody };

//# sourceMappingURL=create-fetch-body.js.map