import { parseCurlCommand } from "../parse-curl.js";
//#region src/libs/importers/curl.ts
/** Data parsing for request body */
function parseData(data) {
	try {
		return JSON.parse(data);
	} catch {
		const result = {};
		data.split("&").forEach((pair) => {
			const [key, value] = pair.split("=");
			if (key && value) result[decodeURIComponent(key)] = decodeURIComponent(value);
		});
		return result;
	}
}
/** Make a usable object from a curl command to create a request */
function importCurlCommand(curlCommand) {
	const { method = "get", url, body = "", headers = {}, servers, queryParameters = [] } = parseCurlCommand(curlCommand);
	const path = new URL(url).pathname;
	const contentType = body?.includes("=") && !body.startsWith("{") ? "application/x-www-form-urlencoded" : headers["Content-Type"] || "";
	const requestBody = body ? parseData(body) : {};
	const parameters = [...Array.isArray(queryParameters) ? queryParameters.map(({ key, value }) => ({
		name: key,
		in: "query",
		schema: {
			type: typeof value,
			examples: [value]
		}
	})) : [], ...Object.entries(headers || {}).map(([key, value]) => ({
		name: key,
		in: "header",
		schema: { type: typeof value },
		example: value
	}))];
	return {
		method,
		url,
		path,
		headers,
		servers: servers ?? [],
		...Object.keys(requestBody).length > 0 && { requestBody: { content: { [contentType]: {
			schema: {
				type: "object",
				properties: Object.fromEntries(Object.entries(requestBody).map(([key, value]) => [key, { type: typeof value }]))
			},
			example: requestBody
		} } } },
		parameters
	};
}
//#endregion
export { importCurlCommand };

//# sourceMappingURL=curl.js.map