/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { trackEvent } from "./utils/analytics.js";
import { parseArgs } from "./lib/args.js";
import { cyan, gray, green, magenta, red, white } from "./lib/colors.js";
import { print } from "./lib/print.js";
import { CODEMOD_SUBCOMMANDS } from "./utils/getCommandHelp.js";
import { codemod_default } from "./commands/codemod/codemod.js";
import { editor_rules_default } from "./commands/editor-rules.js";

//#region ../cli/dist/cli.mjs
/*! © 2026 Yahoo, Inc. UDS CLI v0.0.0-development */
/**
* UDS CLI - Universal Design System Command Line Interface
*
* This is the main entry point for the CLI. It uses manual argument parsing
* (no external framework) following the packages/mobile pattern.
*/
const { positional, options } = parseArgs(process.argv.slice(2));
const command = positional[0];
const banner = `
█ █ █▀▄ ▄▀▀   ▄▀▀ █   █
▀▄█ █▄▀ ▄██   ▀▄▄ █▄▄ █
Universal Design System
`.trim();
const LOCAL_COMMANDS = [{
	name: "codemod",
	description: "Apply a codemod",
	run: (props) => codemod_default.run({
		...props,
		first: positional[1]
	})
}, {
	name: "editor-rules",
	description: "🎯 Generate editor rules for various code editors and AI tools",
	aliases: ["rules", "editor"],
	run: editor_rules_default.run
}];
function showHelp(commands) {
	const widest = Math.max(...commands.map((c) => c.name.length)) + 4;
	const subcommandWidest = Math.max(...CODEMOD_SUBCOMMANDS.map((c) => c.name.length)) + 4;
	print(`\n${green(banner)}\n`);
	print(`${magenta("Usage:")} ${white("uds <command> [options]")}\n`);
	print(`${magenta("Commands:")}`);
	for (const cmd of commands) {
		print(`  ${cyan(cmd.name.padEnd(widest))} ${gray(cmd.description)}`);
		if (cmd.name === "codemod") for (const sub of CODEMOD_SUBCOMMANDS) print(`      ${cyan(sub.name.padEnd(subcommandWidest))} ${gray(sub.description)}`);
	}
	print("");
}
async function run(externalCommands) {
	const commands = [...LOCAL_COMMANDS, ...externalCommands];
	const helpCommands = commands.filter((c) => c.tag !== "hidden");
	const showHelpForCommands = () => showHelp(helpCommands);
	const props = {
		name: "uds",
		commandPath: positional.slice(0, 1),
		first: positional[1],
		second: positional[2],
		third: positional[3],
		options
	};
	if (command === "help" || command === "--help" || command === "-h" || !command) {
		showHelpForCommands();
		return;
	}
	const commandDefinition = commands.find((c) => c.name === command || c.aliases?.includes(command));
	if (!commandDefinition) {
		print(red(`Unknown command: ${command}`));
		await trackEvent("unknown_cmd", { cmd: command });
		showHelpForCommands();
		process.exitCode = 1;
		return;
	}
	await commandDefinition.run(props);
}

//#endregion
export { run };