import { r as ux, t as isInteractiveCli } from "./cli-B86kvKcR.mjs";
import { i as removeInstalledPluginByName, n as getInstalledPluginsForScope } from "./plugins-DtsrBxYU.mjs";
import process from "node:process";
import { defineCommand } from "citty";
//#region src/commands/plugin/remove.ts
async function runPluginRemoveCommand(options = {}) {
	try {
		const selectedPlugins = await selectPluginsToRemove(await ux.running("Loading installed plugins", async () => getInstalledPluginsForScope({ global: options.global })), options);
		if (!selectedPlugins) return;
		if (!await confirmPluginRemoval(selectedPlugins, options)) return;
		const removedPlugins = await ux.running("Removing plugins", async () => {
			for (const plugin of selectedPlugins) await removeInstalledPluginByName(plugin.name, { global: options.global });
			return selectedPlugins;
		});
		ux.log.success(`Removed ${removedPlugins.length} plugin(s).`);
		console.log(removedPlugins.length === 0 ? "No matching plugins found." : removedPlugins.map((plugin) => plugin.name).join(", "));
		return removedPlugins;
	} catch (error) {
		ux.handleError(error);
		return;
	}
}
var remove_default = defineCommand({
	meta: {
		name: "remove",
		description: "Remove installed OpenCode plugins"
	},
	args: {
		name: {
			type: "positional",
			description: "Plugin name",
			required: false
		},
		all: {
			type: "boolean",
			description: "Remove all installed plugins"
		},
		global: {
			alias: "g",
			type: "boolean",
			description: "Remove globally installed plugins"
		},
		yes: {
			alias: "y",
			type: "boolean",
			description: "Skip confirmation prompts"
		}
	},
	async run({ args }) {
		const names = [args.name, ...args._].filter((value) => Boolean(value));
		await runPluginRemoveCommand({
			all: args.all,
			global: args.global,
			names,
			yes: args.yes
		});
		if (!process.exitCode) process.exit(0);
	}
});
async function selectPluginsToRemove(installedPlugins, options) {
	if (installedPlugins.length === 0) return [];
	if (options.all) return installedPlugins;
	if (options.names && options.names.length > 0) {
		const requestedNames = new Set(options.names.map((name) => name.toLowerCase()));
		return installedPlugins.filter((plugin) => requestedNames.has(plugin.name.toLowerCase()));
	}
	if (!isInteractiveCli()) throw new Error("No plugin names provided. Re-run in a TTY to choose plugins, or pass plugin names explicitly or `--all`.");
	const selectedNames = await ux.multiselect({
		message: "Select plugins to remove",
		options: installedPlugins.map((plugin) => ({
			value: plugin.name,
			label: plugin.name
		})),
		required: true
	});
	if (ux.handleCancel(selectedNames, "Removal cancelled.")) return;
	if (!Array.isArray(selectedNames)) return;
	const selectedSet = new Set(selectedNames);
	return installedPlugins.filter((plugin) => selectedSet.has(plugin.name));
}
async function confirmPluginRemoval(selectedPlugins, options) {
	if (selectedPlugins.length === 0) return true;
	if (options.yes) return true;
	const confirmed = await ux.confirm({
		message: `Remove ${selectedPlugins.length} plugin(s): ${selectedPlugins.map((plugin) => plugin.name).join(", ")}?`,
		initialValue: false
	});
	if (ux.handleCancel(confirmed, "Removal cancelled.")) return false;
	if (!confirmed) {
		ux.log.info("Removal cancelled.");
		return false;
	}
	return true;
}
//#endregion
export { runPluginRemoveCommand as n, remove_default as t };

//# sourceMappingURL=remove--LQ6RIC_.mjs.map