//#region src/v2/blocks/operation-block/helpers/serialize-parameter.ts
/**
* Shared parameter serialization utilities for OpenAPI style values.
* Used by both build-request-parameters and process-parameters.
*
* @see https://spec.openapis.org/oas/v3.1.1.html#style-values
*/
/**
* Serializes a value based on the content type for content-based query parameters.
* Content-based query parameters do not use style serialization and instead follow
* their content type specification (e.g., application/json should be JSON.stringified).
*
* @param value - The value to serialize
* @param contentType - The content type to use for serialization
* @returns The serialized value as a string
*/
var serializeContentValue = (value, contentType) => {
	if (typeof value === "string") return value;
	if (contentType.includes("json") || typeof value === "object" && value !== null && !Array.isArray(value)) return JSON.stringify(value);
	return String(value);
};
/**
* Serializes a value according to OpenAPI simple style.
* Used for path and header parameters.
*
* Simple style with explode: false
* - Primitive: blue
* - Array: blue,black,brown
* - Object: R,100,G,200,B,150
*
* Simple style with explode: true
* - Primitive: blue
* - Array: blue,black,brown
* - Object: R=100,G=200,B=150
*/
var serializeSimpleStyle = (value, explode) => {
	if (Array.isArray(value)) return value.join(",");
	if (typeof value === "object" && value !== null) {
		const entries = Object.entries(value);
		if (explode) return entries.map(([k, v]) => `${k}=${v}`).join(",");
		return entries.map(([k, v]) => `${k},${v}`).join(",");
	}
	return value;
};
/**
* Serializes a value according to OpenAPI form style.
* Used for query and cookie parameters.
*
* Form style with explode: true (default for query)
* - Primitive: color=blue
* - Array: color=blue&color=black&color=brown (multiple entries)
* - Object: R=100&G=200&B=150 (multiple entries)
*
* Form style with explode: false
* - Primitive: color=blue
* - Array: color=blue,black,brown
* - Object: color=R,100,G,200,B,150
*/
var serializeFormStyle = (value, explode) => {
	if (Array.isArray(value) && explode) return value.map((v) => ({
		key: "",
		value: v
	}));
	if (Array.isArray(value)) return value.join(",");
	if (typeof value === "object" && value !== null && explode) return Object.entries(value).map(([k, v]) => ({
		key: k,
		value: v
	}));
	if (typeof value === "object" && value !== null) return Object.entries(value).map(([k, v]) => `${k},${v}`).join(",");
	return value;
};
/**
* Serializes a value according to OpenAPI form style for cookies.
* This is similar to serializeFormStyle but handles nested objects recursively
* and treats null values specially for cookie serialization.
*
* Form style with explode: true (default for cookies)
* - Primitive: color=blue
* - Array: color=blue&color=black&color=brown (multiple entries)
* - Object: R=100&G=200&B=150 (multiple entries)
*
* Form style with explode: false
* - Primitive: color=blue
* - Array: color=blue,black,brown (null becomes "null")
* - Object: color=R,100,G,200,B,150 (recursively flattened)
*/
var serializeFormStyleForCookies = (value, explode) => {
	if (Array.isArray(value) && explode) return value.map((v) => ({
		key: "",
		value: v
	}));
	if (Array.isArray(value)) return value.map((v) => v === null ? "null" : String(v)).join(",");
	if (typeof value === "object" && value !== null && explode) return Object.entries(value).map(([k, v]) => ({
		key: k,
		value: v
	}));
	if (typeof value === "object" && value !== null) {
		const flattenObject = (obj) => {
			const result = [];
			for (const [key, val] of Object.entries(obj)) if (typeof val === "object" && val !== null && !Array.isArray(val)) result.push(key, ...flattenObject(val));
			else result.push(key, val === null ? "null" : String(val));
			return result;
		};
		return flattenObject(value).join(",");
	}
	return value;
};
/**
* Serializes a value according to OpenAPI spaceDelimited style.
* Only valid for query parameters with array or object values.
*
* SpaceDelimited array: blue black brown
* SpaceDelimited object: R 100 G 200 B 150
*/
var serializeSpaceDelimitedStyle = (value) => {
	if (Array.isArray(value)) return value.join(" ");
	if (typeof value === "object" && value !== null) return Object.entries(value).map(([k, v]) => `${k} ${v}`).join(" ");
	return String(value);
};
/**
* Serializes a value according to OpenAPI pipeDelimited style.
* Only valid for query parameters with array or object values.
*
* PipeDelimited array: blue|black|brown
* PipeDelimited object: R|100|G|200|B|150
*/
var serializePipeDelimitedStyle = (value) => {
	if (Array.isArray(value)) return value.join("|");
	if (typeof value === "object" && value !== null) return Object.entries(value).flat().join("|");
	return String(value);
};
/**
* Serializes a value according to OpenAPI deepObject style.
* Only valid for query parameters with explode: true.
*
* DeepObject: color[R]=100&color[G]=200&color[B]=150
* Nested: user[name][first]=Alex&user[name][last]=Smith&user[role]=admin
*/
var serializeDeepObjectStyle = (paramName, value) => {
	const result = [];
	/**
	* Recursively flattens nested objects into deepObject notation.
	*/
	const flatten = (obj, prefix) => {
		for (const [key, val] of Object.entries(obj)) {
			const fullKey = `${prefix}[${key}]`;
			if (typeof val === "object" && val !== null && !Array.isArray(val)) flatten(val, fullKey);
			else result.push({
				key: fullKey,
				value: String(val)
			});
		}
	};
	if (typeof value === "object" && value !== null && !Array.isArray(value)) flatten(value, paramName);
	return result;
};
//#endregion
export { serializeContentValue, serializeDeepObjectStyle, serializeFormStyle, serializeFormStyleForCookies, serializePipeDelimitedStyle, serializeSimpleStyle, serializeSpaceDelimitedStyle };

//# sourceMappingURL=serialize-parameter.js.map