import { r as ux, t as isInteractiveCli } from "./cli-B86kvKcR.mjs";
import { a as resolvePluginsFromSource, r as installResolvedPlugins } from "./plugins-DtsrBxYU.mjs";
import process from "node:process";
import { defineCommand } from "citty";
//#region src/commands/plugin/add.ts
async function runPluginAddCommand(options) {
	try {
		const requestedPluginNames = options.plugin ? options.plugin.split(",").map((name) => name.trim()).filter(Boolean) : void 0;
		const resolved = await ux.running("Resolving plugins", async () => resolvePluginsFromSource(options.source, { pluginNames: options.all ? void 0 : requestedPluginNames }));
		try {
			const selectedPlugins = await selectPluginsToInstall(resolved.plugins, options);
			if (!selectedPlugins) return;
			const installed = await ux.running("Installing plugins", async () => installResolvedPlugins({
				...resolved,
				plugins: selectedPlugins
			}, { global: options.global }));
			ux.log.success(`Installed ${installed.length} plugin(s).`);
			console.log(installed.map((plugin) => plugin.name).join(", "));
		} finally {
			await resolved.cleanup?.();
		}
	} catch (error) {
		ux.handleError(error);
	}
}
var add_default = defineCommand({
	meta: {
		name: "add",
		description: "Install OpenCode plugins from a source"
	},
	args: {
		source: {
			type: "positional",
			description: "Plugin source such as catalog:notify-on-idle or ./local/path",
			required: true
		},
		all: {
			type: "boolean",
			description: "Install all discovered plugins from the source"
		},
		global: {
			alias: "g",
			type: "boolean",
			description: "Install globally for OpenCode"
		},
		plugin: {
			alias: "p",
			type: "string",
			description: "Comma-separated plugin names to install"
		}
	},
	async run({ args }) {
		await runPluginAddCommand({
			all: args.all,
			global: args.global,
			plugin: args.plugin,
			source: args.source
		});
		if (!process.exitCode) process.exit(0);
	}
});
async function selectPluginsToInstall(plugins, options) {
	if (options.all || options.plugin) return plugins;
	if (plugins.length === 1) return plugins;
	if (!isInteractiveCli()) throw new Error("Multiple plugins found for this source. Re-run in a TTY to choose plugins, or pass `--plugin <name>` or `--all`.");
	const selectedNames = await ux.multiselect({
		message: "Select plugins to install",
		options: plugins.map((plugin) => ({
			value: plugin.name,
			label: plugin.name
		})),
		required: true
	});
	if (ux.handleCancel(selectedNames, "Installation cancelled.")) return;
	if (!Array.isArray(selectedNames)) return;
	const selectedSet = new Set(selectedNames);
	return plugins.filter((plugin) => selectedSet.has(plugin.name));
}
//#endregion
export { runPluginAddCommand as n, add_default as t };

//# sourceMappingURL=add-BuPiTmuX.mjs.map