import { n as showCommandHelp, r as ux } from "./cli-B86kvKcR.mjs";
import { t as runManifestValidateCommand } from "./validate-Dvu_9yan.mjs";
import { n as runPluginAddCommand } from "./add-BuPiTmuX.mjs";
import { n as runPluginListCommand } from "./list-BsV7VNd5.mjs";
import { n as runPluginRemoveCommand } from "./remove--LQ6RIC_.mjs";
import { t as runStatusCommand } from "./status-B89PPN4s.mjs";
import { t as runSyncCommand } from "./sync-X-wbEroJ.mjs";
import { readFileSync } from "node:fs";
import path from "node:path";
import process from "node:process";
import { defineCommand, runMain, showUsage } from "citty";
//#region src/commands/launcher.ts
async function runLauncher(rootCommand) {
	if (!process.stdin.isTTY) {
		await showCommandHelp(rootCommand);
		return;
	}
	ux.intro("Anvil workspace CLI");
	const command = await ux.select({
		message: "Select a command",
		options: [
			{
				value: "sync",
				label: "sync",
				hint: "Sync Anvil workspace setup"
			},
			{
				value: "plugin",
				label: "plugin",
				hint: "Manage native OpenCode plugins"
			},
			{
				value: "manifest",
				label: "manifest",
				hint: "Inspect the Anvil workspace manifest"
			},
			{
				value: "status",
				label: "status",
				hint: "Show Anvil package composition status"
			}
		]
	});
	if (ux.handleCancel(command)) return;
	switch (command) {
		case "manifest": {
			const manifestSubcommand = await ux.select({
				message: "Select a manifest subcommand",
				options: [{
					value: "validate",
					label: "validate",
					hint: "Validate the nearest anvil.yaml file"
				}]
			});
			if (ux.handleCancel(manifestSubcommand)) return;
			if (manifestSubcommand === "validate") runManifestValidateCommand();
			break;
		}
		case "plugin": {
			const pluginSubcommand = await ux.select({
				message: "Select a plugin subcommand",
				options: [
					{
						value: "add",
						label: "add",
						hint: "Install OpenCode plugins from a source"
					},
					{
						value: "list",
						label: "list",
						hint: "List installed OpenCode plugins"
					},
					{
						value: "remove",
						label: "remove",
						hint: "Remove installed OpenCode plugins"
					}
				]
			});
			if (ux.handleCancel(pluginSubcommand)) return;
			switch (pluginSubcommand) {
				case "add": {
					const sourceResult = await ux.text({
						message: "Enter a source such as catalog:notify-on-idle or ./local/path",
						placeholder: "catalog:notify-on-idle"
					});
					if (ux.handleCancel(sourceResult) || typeof sourceResult !== "string") return;
					await runPluginAddCommand({ source: sourceResult.trim() });
					break;
				}
				case "list":
					await runPluginListCommand({
						global: false,
						json: false
					});
					break;
				case "remove":
					await runPluginRemoveCommand({ global: false });
					break;
			}
			break;
		}
		case "status":
			runStatusCommand();
			break;
		case "sync":
			await runSyncCommand({ dryRun: false });
			break;
	}
	ux.outro("Done.");
}
//#endregion
//#region src/cli.ts
function getPackageVersion() {
	try {
		const raw = readFileSync(new URL("../package.json", import.meta.url), "utf8");
		return JSON.parse(raw).version ?? "0.0.0";
	} catch {
		return "0.0.0";
	}
}
function getInvokedBinaryName() {
	return path.basename(process.argv[1] ?? "anvil");
}
await runMain(defineCommand({
	meta: {
		name: "anvil",
		version: getPackageVersion(),
		description: "Anvil workspace library and CLI facade"
	},
	subCommands: {
		catalog: () => import("./catalog-BGCUMN3a.mjs").then((module) => module.default),
		hub: () => import("./hub-C8Kx2r5S.mjs").then((module) => module.default),
		issue: () => import("./issue-LHNkT2F7.mjs").then((module) => module.default),
		manifest: () => import("./manifest-DZ9P6Nxq.mjs").then((module) => module.default),
		plugin: () => import("./plugin-3RFCjGJh.mjs").then((module) => module.default),
		release: () => import("./release-DwEzmo1G.mjs").then((module) => module.default),
		status: () => import("./status-CLM7qq0e.mjs").then((module) => module.default),
		sync: () => import("./sync-DT54vHnL.mjs").then((module) => module.default)
	},
	async run(context) {
		if (process.argv.slice(2).length > 0) return;
		const invokedBinaryName = getInvokedBinaryName();
		if (invokedBinaryName === "anv" || invokedBinaryName === "anv.mjs") {
			if (!process.stdin.isTTY) {
				await showUsage(context.cmd);
				return;
			}
			await runLauncher(context.cmd);
			return;
		}
		await showUsage(context.cmd);
	}
}));
//#endregion
export {};

//# sourceMappingURL=cli.mjs.map