import { replaceTemplateVariables } from "../string-template.js";
//#region src/libs/send-request/create-fetch-query-params.ts
/**
* Populate the query parameters from the example parameters. This is an incomplete implementation that currently
* only supports the following styles and options:
*
* - `form` style with `explode` set to true or false
*
* @see https://spec.openapis.org/oas/v3.1.1.html#style-values
*/
function createFetchQueryParams(example, env, request) {
	const params = new URLSearchParams();
	const parameterSchemaMap = (request?.parameters ?? []).reduce((acc, param) => {
		if (param.in === "query") acc[param.name] = param;
		return acc;
	}, {});
	example.parameters.query.forEach((p) => {
		if (!p.enabled) return;
		const schema = parameterSchemaMap[p.key];
		switch (p.type) {
			case "array": {
				const values = replaceTemplateVariables(p.value ?? "", env).split(/,\ ?/);
				if (schema?.explode === false) {
					const csv = values.join(",");
					params.append(p.key, csv);
				} else values.forEach((value) => {
					params.append(p.key, value.trim());
				});
				break;
			}
			default: {
				const value = replaceTemplateVariables(p.value ?? "", env);
				params.append(p.key, value.trim());
				break;
			}
		}
	});
	return params;
}
//#endregion
export { createFetchQueryParams };

//# sourceMappingURL=create-fetch-query-params.js.map