import { getExample } from "../../../operation-block/helpers/get-example.js";
import { getExampleFromSchema } from "../get-example-from-schema.js";
import { deSerializeParameter } from "../../../operation-block/helpers/de-serialize-parameter.js";
import { isParamDisabled } from "../../../request-block/helpers/is-param-disabled.js";
import { serializeContentValue, serializeDeepObjectStyle, serializeFormStyle, serializeFormStyleForCookies, serializePipeDelimitedStyle, serializeSimpleStyle, serializeSpaceDelimitedStyle } from "../../../operation-block/helpers/serialize-parameter.js";
import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
//#region src/v2/blocks/operation-code-sample/helpers/operation-to-har/process-parameters.ts
/** Ensures we don't have any references in the parameters */
var deReferenceParams = (params) => (params ?? []).map((param) => getResolvedRef(param));
/**
* Get the style and explode values for a parameter according to OpenAPI 3.1.1 specification.
* Handles defaults and validation for parameter location restrictions.
*/
var getParameterStyleAndExplode = (param) => {
	if (param.in === "header") return {
		style: "simple",
		explode: "explode" in param && param.explode !== void 0 ? param.explode : false
	};
	if (param.in === "cookie") return {
		style: "form",
		explode: "explode" in param && param.explode !== void 0 ? param.explode : true
	};
	const defaultStyle = {
		path: "simple",
		query: "form",
		header: "simple",
		cookie: "form"
	}[param.in];
	const style = "style" in param && param.style ? param.style : defaultStyle;
	return {
		style,
		explode: "explode" in param && param.explode !== void 0 ? param.explode : style === "form"
	};
};
/**
* Extract the value for a parameter from example data or schema.
* Prioritizes example data over schema examples.
* Returns null if the parameter is disabled so we can skip it.
*/
var getParameterValue = (param, example, contentType) => {
	const exampleValue = getExample(param, example, contentType);
	if (isParamDisabled(param, exampleValue)) return;
	if (exampleValue?.value !== void 0) return deSerializeParameter(exampleValue.value, param);
	if (!("schema" in param) || !param.schema) return;
	const options = param.in === "path" ? { emptyString: `{${param.name}}` } : {};
	return getExampleFromSchema(getResolvedRef(param.schema), options);
};
/**
* Process OpenAPI parameters and return the updated properties.
* Handles path, query, and header parameters with various styles and explode options.
*
* @see https://spec.openapis.org/oas/latest.html#style-values
*/
var processParameters = ({ harRequest, parameters, example }) => {
	const newHeaders = [...harRequest.headers];
	const newQueryString = [...harRequest.queryString];
	let newUrl = harRequest.url;
	const deReferencedParams = deReferenceParams(parameters);
	for (const param of deReferencedParams) {
		if (!param.in || !param.name) continue;
		const paramValue = getParameterValue(param, example, void 0);
		if (paramValue === void 0) continue;
		const { style, explode } = getParameterStyleAndExplode(param);
		switch (param.in) {
			case "path":
				newUrl = processPathParameters(newUrl, param, paramValue, style, explode);
				break;
			case "query":
				if ("content" in param && param.content) {
					const serializedValue = serializeContentValue(paramValue, Object.keys(param.content)[0] ?? "application/json");
					newQueryString.push({
						name: param.name,
						value: serializedValue
					});
					break;
				}
				switch (style) {
					case "form": {
						const serialized = serializeFormStyle(paramValue, explode);
						if (Array.isArray(serialized)) for (const entry of serialized) {
							const key = entry.key || param.name;
							newQueryString.push({
								name: key,
								value: String(entry.value)
							});
						}
						else newQueryString.push({
							name: param.name,
							value: String(serialized)
						});
						break;
					}
					case "spaceDelimited": {
						const serialized = serializeSpaceDelimitedStyle(paramValue);
						newQueryString.push({
							name: param.name,
							value: serialized
						});
						break;
					}
					case "pipeDelimited": {
						const serialized = serializePipeDelimitedStyle(paramValue);
						newQueryString.push({
							name: param.name,
							value: serialized
						});
						break;
					}
					case "deepObject":
						if (explode) {
							const entries = serializeDeepObjectStyle(param.name, paramValue);
							for (const entry of entries) newQueryString.push({
								name: entry.key,
								value: entry.value
							});
						}
						break;
					default: newQueryString.push({
						name: param.name,
						value: String(paramValue)
					});
				}
				break;
			case "header": {
				const serialized = serializeSimpleStyle(paramValue, explode);
				newHeaders.push({
					name: param.name,
					value: String(serialized)
				});
				break;
			}
			case "cookie": {
				const serialized = serializeFormStyleForCookies(paramValue, explode);
				if (Array.isArray(serialized)) for (const entry of serialized) {
					const key = entry.key || param.name;
					const value = entry.value === null ? "null" : String(entry.value);
					harRequest.cookies.push({
						name: key,
						value
					});
				}
				else {
					const value = serialized === null ? "null" : String(serialized);
					harRequest.cookies.push({
						name: param.name,
						value
					});
				}
				break;
			}
		}
	}
	return {
		url: newUrl,
		headers: newHeaders,
		queryString: newQueryString,
		cookies: harRequest.cookies
	};
};
/**
* Process path parameters according to OpenAPI specification.
* Handles matrix, label, and simple styles with explode options.
*
* @param url - The URL to process
* @param param - The parameter object
* @param paramValue - The value of the parameter
* @param style - The style of the parameter (matrix, label, simple)
* @param explode - Whether to explode the parameter
* @returns The updated URL with processed path parameters
*/
var processPathParameters = (url, param, paramValue, style, explode) => {
	switch (style) {
		case "matrix":
			if (explode) {
				if (Array.isArray(paramValue)) {
					const values = paramValue.map((v) => `${param.name}=${v}`).join(";");
					return url.replace(`{;${param.name}}`, `;${values}`);
				}
				if (typeof paramValue === "object" && paramValue !== null) {
					const values = Object.entries(paramValue).map(([k, v]) => `${k}=${v}`).join(";");
					return url.replace(`{;${param.name}}`, `;${values}`);
				}
				return url.replace(`{;${param.name}}`, `;${param.name}=${paramValue}`);
			}
			if (Array.isArray(paramValue)) return url.replace(`{;${param.name}}`, `;${param.name}=${paramValue.join(",")}`);
			if (typeof paramValue === "object" && paramValue !== null) {
				const values = Object.entries(paramValue).map(([k, v]) => `${k},${v}`).join(",");
				return url.replace(`{;${param.name}}`, `;${param.name}=${values}`);
			}
			return url.replace(`{;${param.name}}`, `;${param.name}=${paramValue}`);
		case "label":
			if (explode) {
				if (Array.isArray(paramValue)) return url.replace(`{.${param.name}}`, `.${paramValue.join(".")}`);
				if (typeof paramValue === "object" && paramValue !== null) {
					const values = Object.entries(paramValue).map(([k, v]) => `${k}=${v}`).join(".");
					return url.replace(`{.${param.name}}`, `.${values}`);
				}
				return url.replace(`{.${param.name}}`, `.${paramValue}`);
			}
			if (Array.isArray(paramValue)) return url.replace(`{.${param.name}}`, `.${paramValue.join(",")}`);
			if (typeof paramValue === "object" && paramValue !== null) {
				const values = Object.entries(paramValue).map(([k, v]) => `${k},${v}`).join(",");
				return url.replace(`{.${param.name}}`, `.${values}`);
			}
			return url.replace(`{.${param.name}}`, `.${paramValue}`);
		case "simple": {
			const serialized = serializeSimpleStyle(paramValue, explode);
			return url.replace(`{${param.name}}`, String(serialized));
		}
		default: return url.replace(`{${param.name}}`, String(paramValue));
	}
};
//#endregion
export { processParameters };

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