import { getSelectedBodyContentType } from "./get-selected-body-content-type.js";
import { getExampleFromBody } from "../../request-block/helpers/get-request-body-example.js";
import { replaceEnvVariables } from "@scalar/helpers/regex/replace-variables";
import { unpackProxyObject } from "@scalar/workspace-store/helpers/unpack-proxy";
//#region src/v2/blocks/operation-block/helpers/build-request-body.ts
var getMultipartEncodingContentType = (requestBody, bodyContentType, fieldName, replacedFieldName) => requestBody.content[bodyContentType]?.encoding?.[fieldName]?.contentType ?? requestBody.content[bodyContentType]?.encoding?.[replacedFieldName]?.contentType;
var serializeMultipartValue = (value, env) => {
	if (typeof value === "string") return replaceEnvVariables(value, env);
	return replaceEnvVariables(typeof value === "object" && value !== null ? JSON.stringify(unpackProxyObject(value)) : String(value), env);
};
/**
* Create the fetch request body
*/
var buildRequestBody = (requestBody, env = {}, exampleKey = "default", requestBodyCompositionSelection) => {
	if (!requestBody) return null;
	/** Selected content type for the body from the dropdown, stored as x-scalar-selected-content-type */
	const bodyContentType = getSelectedBodyContentType(requestBody, exampleKey);
	if (!bodyContentType) return null;
	/** An example value or generated example from the schema */
	const example = getExampleFromBody(requestBody, bodyContentType, exampleKey, requestBodyCompositionSelection);
	if (!example) return null;
	if ((bodyContentType === "multipart/form-data" || bodyContentType === "application/x-www-form-urlencoded") && Array.isArray(example.value)) {
		const exampleValue = example.value.filter((item) => !item.isDisabled);
		const form = bodyContentType === "multipart/form-data" ? new FormData() : new URLSearchParams();
		exampleValue.forEach(({ name, value }) => {
			if (!name) return;
			const replacedName = replaceEnvVariables(name, env);
			const partContentType = form instanceof FormData ? getMultipartEncodingContentType(requestBody, bodyContentType, name, replacedName) : void 0;
			if (value instanceof File && form instanceof FormData) {
				/**
				* We need to unwrap the proxies to get the file name due to the
				* "this" context in the proxy causing an illegal invocation error
				*/
				const unwrappedValue = unpackProxyObject(value);
				const encodedValue = partContentType && partContentType !== unwrappedValue.type ? new File([unwrappedValue], unwrappedValue.name, {
					type: partContentType,
					lastModified: unwrappedValue.lastModified
				}) : unwrappedValue;
				form.append(replacedName, encodedValue, encodedValue.name);
			} else if (value !== void 0 && value !== null) {
				const serializedValue = serializeMultipartValue(value, env);
				if (form instanceof FormData && partContentType) form.append(replacedName, new Blob([serializedValue], { type: partContentType }));
				else form.append(replacedName, serializedValue);
			}
		});
		return form;
	}
	if (bodyContentType === "application/x-www-form-urlencoded" && example.value !== null && typeof example.value === "object" && !Array.isArray(example.value)) {
		const form = new URLSearchParams();
		for (const [key, value] of Object.entries(example.value)) if (key && value !== void 0 && value !== null) {
			const replacedKey = replaceEnvVariables(key, env);
			const stringValue = typeof value === "string" ? value : String(value);
			form.append(replacedKey, replaceEnvVariables(stringValue, env));
		}
		return form;
	}
	const exampleValue = example.value !== null && typeof example.value === "object" ? unpackProxyObject(example.value) : example.value;
	if (exampleValue instanceof File) return exampleValue;
	if (typeof exampleValue === "object") return replaceEnvVariables(JSON.stringify(exampleValue), env);
	return typeof exampleValue === "string" ? replaceEnvVariables(exampleValue, env) : exampleValue;
};
//#endregion
export { buildRequestBody };

//# sourceMappingURL=build-request-body.js.map