import { getResolvedRefDeep } from "../get-resolved-ref-deep.js";
import { getExample } from "../../../operation-block/helpers/get-example.js";
import { getExampleFromSchema } from "../get-example-from-schema.js";
import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { unpackProxyObject } from "@scalar/workspace-store/helpers/unpack-proxy";
import { json2xml } from "@scalar/helpers/file/json2xml";
//#region src/v2/blocks/operation-code-sample/helpers/operation-to-har/process-body.ts
/**
* Converts an object to an array of form parameters
* @param obj - The object to convert
* @returns Array of form parameters with name and value properties
*/
var objectToFormParams = (obj, encoding, parentKey) => {
	const params = [];
	/** Ensure we do not include disabled items */
	const entries = Array.isArray(obj) ? obj.filter((item) => !item.isDisabled).map((item) => [item.name, item.value]) : Object.entries(obj);
	for (const [key, value] of entries) {
		if (value === void 0 || value === null) continue;
		const partContentType = parentKey ? void 0 : encoding?.[key]?.contentType;
		if (value instanceof File) {
			const file = unpackProxyObject(value);
			params.push({
				name: key,
				value: `@${file.name}`,
				...partContentType ? { contentType: partContentType } : {}
			});
		} else if (partContentType && typeof value === "object") params.push({
			name: key,
			value: JSON.stringify(unpackProxyObject(value)),
			contentType: partContentType
		});
		else if (Array.isArray(value)) for (const item of value) if (item instanceof File) {
			const file = unpackProxyObject(item);
			params.push({
				name: key,
				value: `@${file.name}`
			});
		} else params.push({
			name: key,
			value: String(item)
		});
		else if (typeof value === "object") {
			const nestedParams = objectToFormParams(value, void 0, key);
			for (const param of nestedParams) params.push({
				name: `${key}.${param.name}`,
				value: param.value
			});
		} else params.push({
			name: key,
			value: String(value),
			...partContentType ? { contentType: partContentType } : {}
		});
	}
	return params;
};
/**
* Processes the request body and returns the processed data
* Returns undefined if no example is found
*/
var processBody = ({ requestBody, contentType, example, requestBodyCompositionSelection }) => {
	const _contentType = contentType || Object.keys(requestBody.content)[0] || "";
	const formatBinaryFile = (file) => {
		return `@${unpackProxyObject(file).name || "filename"}`;
	};
	const encoding = requestBody.content[_contentType]?.encoding;
	const isFormData = _contentType === "multipart/form-data" || _contentType === "application/x-www-form-urlencoded";
	const isXml = _contentType === "application/xml";
	const _example = getExample(requestBody, example, contentType)?.value;
	if (typeof _example !== "undefined") {
		const exampleValue = _example !== null && typeof _example === "object" ? unpackProxyObject(_example) : _example;
		if (isFormData && typeof exampleValue === "object" && exampleValue !== null) return {
			mimeType: _contentType,
			params: objectToFormParams(exampleValue, _contentType === "multipart/form-data" ? encoding : void 0)
		};
		if (isXml && typeof exampleValue === "object" && exampleValue !== null) return {
			mimeType: _contentType,
			text: json2xml(exampleValue)
		};
		if (exampleValue instanceof File) return {
			mimeType: _contentType,
			text: formatBinaryFile(exampleValue)
		};
		return {
			mimeType: _contentType,
			text: typeof exampleValue === "string" ? exampleValue : JSON.stringify(exampleValue)
		};
	}
	const contentSchema = getResolvedRef(requestBody.content[_contentType]?.schema);
	if (typeof contentSchema !== "undefined") {
		const extractedExample = getExampleFromSchema(getResolvedRefDeep(contentSchema), {
			compositionSelection: requestBodyCompositionSelection,
			mode: "write",
			xml: isXml
		}, { schemaPath: ["requestBody"] });
		if (extractedExample !== void 0) {
			if (isFormData && typeof extractedExample === "object" && extractedExample !== null) return {
				mimeType: _contentType,
				params: objectToFormParams(extractedExample, _contentType === "multipart/form-data" ? encoding : void 0)
			};
			if (isXml && typeof extractedExample === "object" && extractedExample !== null) return {
				mimeType: _contentType,
				text: json2xml(extractedExample)
			};
			return {
				mimeType: _contentType,
				text: typeof extractedExample === "string" ? extractedExample : JSON.stringify(extractedExample)
			};
		}
	}
};
//#endregion
export { processBody };

//# sourceMappingURL=process-body.js.map