
//#region src/index.ts
const NEWLINE = "\\\n ";
const plugin = { httpRequestActions: [{
	label: "Copy as Curl",
	icon: "copy",
	async onSelect(ctx, args) {
		const data = await convertToCurl(await ctx.httpRequest.render({
			httpRequest: args.httpRequest,
			purpose: "send"
		}));
		await ctx.clipboard.copyText(data);
		await ctx.toast.show({
			message: "Command copied to clipboard",
			icon: "copy",
			color: "success"
		});
	}
}] };
async function convertToCurl(request) {
	const xs = ["curl"];
	if (request.method) xs.push("-X", request.method);
	let finalUrl = request.url || "";
	const urlParams = (request.urlParameters ?? []).filter(onlyEnabled);
	if (urlParams.length > 0) {
		const [base, hash] = finalUrl.split("#");
		const separator = base?.includes("?") ? "&" : "?";
		const queryString = urlParams.map((p) => `${encodeURIComponent(p.name)}=${encodeURIComponent(p.value)}`).join("&");
		finalUrl = base + separator + queryString + (hash ? `#${hash}` : "");
	}
	if (request.authenticationType === "apikey") if (request.authentication?.location === "query") {
		const sep = finalUrl.includes("?") ? "&" : "?";
		finalUrl = [
			finalUrl,
			sep,
			encodeURIComponent(request.authentication?.key ?? "token"),
			"=",
			encodeURIComponent(request.authentication?.value ?? "")
		].join("");
	} else {
		request.headers = request.headers ?? [];
		request.headers.push({
			name: request.authentication?.key ?? "X-Api-Key",
			value: request.authentication?.value ?? ""
		});
	}
	xs.push(quote(finalUrl));
	xs.push(NEWLINE);
	for (const h of (request.headers ?? []).filter(onlyEnabled)) {
		xs.push("--header", quote(`${h.name}: ${h.value}`));
		xs.push(NEWLINE);
	}
	const type = request.bodyType ?? "none";
	if ((type === "multipart/form-data" || type === "application/x-www-form-urlencoded") && Array.isArray(request.body?.form)) {
		const flag = request.bodyType === "multipart/form-data" ? "--form" : "--data";
		for (const p of (request.body?.form ?? []).filter(onlyEnabled)) {
			if (p.file) {
				let v = `${p.name}=@${p.file}`;
				v += p.contentType ? `;type=${p.contentType}` : "";
				xs.push(flag, v);
			} else xs.push(flag, quote(`${p.name}=${p.value}`));
			xs.push(NEWLINE);
		}
	} else if (type === "graphql" && typeof request.body?.query === "string") {
		const body = {
			query: request.body.query || "",
			variables: maybeParseJSON(request.body.variables, void 0),
			operationName: request.body.operationName || void 0
		};
		xs.push("--data", quote(JSON.stringify(body)));
		xs.push(NEWLINE);
	} else if (type !== "none" && typeof request.body?.text === "string") {
		xs.push("--data", quote(request.body.text));
		xs.push(NEWLINE);
	}
	if (request.authentication?.disabled !== true) {
		if (request.authenticationType === "basic" || request.authenticationType === "digest") {
			if (request.authenticationType === "digest") xs.push("--digest");
			xs.push("--user", quote(`${request.authentication?.username ?? ""}:${request.authentication?.password ?? ""}`));
			xs.push(NEWLINE);
		}
		if (request.authenticationType === "bearer") {
			const value = `${request.authentication?.prefix ?? "Bearer"} ${request.authentication?.token ?? ""}`.trim();
			xs.push("--header", quote(`Authorization: ${value}`));
			xs.push(NEWLINE);
		}
		if (request.authenticationType === "auth-aws-sig-v4") {
			xs.push("--aws-sigv4", [
				"aws",
				"amz",
				request.authentication?.region ?? "",
				request.authentication?.service ?? ""
			].join(":"));
			xs.push(NEWLINE);
			xs.push("--user", quote(`${request.authentication?.accessKeyId ?? ""}:${request.authentication?.secretAccessKey ?? ""}`));
			if (request.authentication?.sessionToken) {
				xs.push(NEWLINE);
				xs.push("--header", quote(`X-Amz-Security-Token: ${request.authentication.sessionToken}`));
			}
			xs.push(NEWLINE);
		}
	}
	if (xs[xs.length - 1] === NEWLINE) xs.splice(xs.length - 1, 1);
	return xs.join(" ");
}
function quote(arg) {
	return `'${arg.replace(/'/g, "\\'")}'`;
}
function onlyEnabled(v) {
	return v.enabled !== false && !!v.name;
}
function maybeParseJSON(v, fallback) {
	try {
		return JSON.parse(v);
	} catch {
		return fallback;
	}
}

//#endregion
exports.convertToCurl = convertToCurl;
exports.plugin = plugin;