//#region rolldown:runtime
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
	if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
		key = keys[i];
		if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
			get: ((k) => from[k]).bind(null, key),
			enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
		});
	}
	return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
	value: mod,
	enumerable: true
}) : target, mod));

//#endregion
let node_path = require("node:path");
node_path = __toESM(node_path);

//#region src/index.ts
const NEWLINE = "\\\n ";
const plugin = { grpcRequestActions: [{
	label: "Copy as gRPCurl",
	icon: "copy",
	async onSelect(ctx, args) {
		const data = await convert(await ctx.grpcRequest.render({
			grpcRequest: args.grpcRequest,
			purpose: "send"
		}), args.protoFiles);
		await ctx.clipboard.copyText(data);
		await ctx.toast.show({
			message: "Command copied to clipboard",
			icon: "copy",
			color: "success"
		});
	}
}] };
async function convert(request, allProtoFiles) {
	const xs = ["grpcurl"];
	if (request.url?.startsWith("http://")) xs.push("-plaintext");
	const protoIncludes = allProtoFiles.filter((f) => !f.endsWith(".proto"));
	const protoFiles = allProtoFiles.filter((f) => f.endsWith(".proto"));
	const inferredIncludes = /* @__PURE__ */ new Set();
	for (const f of protoFiles) {
		const protoDir = findParentProtoDir(f);
		if (protoDir) inferredIncludes.add(protoDir);
		else {
			inferredIncludes.add(node_path.default.posix.join(f, ".."));
			inferredIncludes.add(node_path.default.posix.join(f, "..", ".."));
		}
	}
	for (const f of protoIncludes) {
		xs.push("-import-path", quote(f));
		xs.push(NEWLINE);
	}
	for (const f of inferredIncludes.values()) {
		xs.push("-import-path", quote(f));
		xs.push(NEWLINE);
	}
	for (const f of protoFiles) {
		xs.push("-proto", quote(f));
		xs.push(NEWLINE);
	}
	for (const h of (request.metadata ?? []).filter(onlyEnabled)) {
		xs.push("-H", quote(`${h.name}: ${h.value}`));
		xs.push(NEWLINE);
	}
	if (request.authentication?.disabled !== true) {
		if (request.authenticationType === "basic") {
			const user = request.authentication?.username ?? "";
			const pass = request.authentication?.password ?? "";
			const encoded = btoa(`${user}:${pass}`);
			xs.push("-H", quote(`Authorization: Basic ${encoded}`));
			xs.push(NEWLINE);
		} else if (request.authenticationType === "bearer") {
			xs.push("-H", quote(`Authorization: Bearer ${request.authentication?.token ?? ""}`));
			xs.push(NEWLINE);
		} else if (request.authenticationType === "apikey") {
			if (request.authentication?.location === "query") {
				const sep = request.url?.includes("?") ? "&" : "?";
				request.url = [
					request.url,
					sep,
					encodeURIComponent(request.authentication?.key ?? "token"),
					"=",
					encodeURIComponent(request.authentication?.value ?? "")
				].join("");
			} else xs.push("-H", quote(`${request.authentication?.key ?? "X-Api-Key"}: ${request.authentication?.value ?? ""}`));
			xs.push(NEWLINE);
		}
	}
	if (request.message) {
		xs.push("-d", quote(request.message));
		xs.push(NEWLINE);
	}
	if (request.url) {
		const server = request.url.replace(/^https?:\/\//, "");
		xs.push(server);
		xs.push(NEWLINE);
	}
	if (request.service && request.method) {
		xs.push(`${request.service}/${request.method}`);
		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 findParentProtoDir(startPath) {
	let dir = node_path.default.resolve(startPath);
	while (true) {
		if (node_path.default.basename(dir) === "proto") return dir;
		const parent = node_path.default.dirname(dir);
		if (parent === dir) return null;
		dir = parent;
	}
}

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