import { parseCurlCommand } from "../../../../libs/parse-curl.js";
//#region src/v2/features/command-palette/helpers/get-operation-from-curl.ts
/**
* Parses request body data from a string.
* Attempts JSON parsing first, then falls back to form-encoded parsing.
*
* Form-encoded data is expected in the format: key1=value1&key2=value2
*/
var parseRequestBodyData = (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;
	}
};
/**
* Determines the content type based on the request body structure.
* Returns form-encoded if the body looks like form data, JSON if the body looks like JSON,
* otherwise uses the header value.
*/
var detectContentType = (body, headers) => {
	if (headers["Content-Type"]) return headers["Content-Type"];
	const trimmedBody = body.trim();
	if (trimmedBody.startsWith("{") || trimmedBody.startsWith("[")) return "application/json";
	return body.includes("=") && !trimmedBody.startsWith("{") ? "application/x-www-form-urlencoded" : "";
};
/**
* Infers a JSON Schema type from a JavaScript value.
* Maps JavaScript typeof results to JSON Schema primitive types.
*/
var inferSchemaType = (value) => {
	const jsType = typeof value;
	if (jsType === "string") return "string";
	if (jsType === "number") return "number";
	if (jsType === "boolean") return "boolean";
	if (value === null) return "null";
	if (Array.isArray(value)) return "array";
	if (jsType === "object") return "object";
	return "string";
};
/**
* Creates query and header parameters from parsed cURL data.
* Each parameter includes a schema with inferred type and an example value.
*/
var createParameters = (queryParameters, headers, exampleKey) => {
	const queryParams = Array.isArray(queryParameters) ? queryParameters.map(({ key, value }) => ({
		name: key,
		in: "query",
		schema: { type: inferSchemaType(value) },
		examples: { [exampleKey]: { value } }
	})) : [];
	const headerParams = Object.entries(headers || {}).map(([key, value]) => ({
		name: key,
		in: "header",
		schema: { type: inferSchemaType(value) },
		examples: { [exampleKey]: { value } }
	}));
	return [...queryParams, ...headerParams];
};
/**
* Creates a request body schema from parsed data.
* Generates an object schema with properties inferred from the data structure.
*/
var createRequestBodySchema = (data) => {
	return {
		type: "object",
		properties: Object.fromEntries(Object.entries(data).map(([key, value]) => [key, { type: inferSchemaType(value) }]))
	};
};
/** Don't want to throw for invalid URLs */
var safePathname = (url) => {
	try {
		return new URL(url).pathname;
	} catch {
		return "/";
	}
};
/**
* Converts a cURL command string into an OpenAPI operation object.
*
* This function parses a cURL command and extracts all relevant information
* (path, method, headers, query parameters, request body) to create a complete
* OpenAPI operation that can be used to populate a request in the API client.
*
* The exampleKey is used to namespace the example values in the operation object,
* allowing multiple examples to coexist if needed.
*/
var getOperationFromCurl = (curl, exampleKey = "curl") => {
	const { method = "get", url, body = "", headers = {}, servers = [], queryParameters = [] } = parseCurlCommand(curl);
	const path = safePathname(url);
	const contentType = detectContentType(body, headers);
	const requestBodyData = body ? parseRequestBodyData(body) : {};
	const parameters = createParameters(queryParameters, headers, exampleKey);
	const serverObjects = servers.map((server) => ({ url: server }));
	return {
		path,
		method,
		operation: {
			parameters,
			requestBody: {
				content: { [contentType]: {
					schema: createRequestBodySchema(requestBodyData),
					examples: { [exampleKey]: { value: requestBodyData } }
				} },
				"x-scalar-selected-content-type": { [exampleKey]: contentType }
			},
			servers: serverObjects.length ? serverObjects : void 0
		}
	};
};
//#endregion
export { getOperationFromCurl };

//# sourceMappingURL=get-operation-from-curl.js.map