/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { cyan, gray, green, magenta } from "../dist/lib/colors.js";
import { print } from "../dist/lib/print.js";
import { spinStart, spinStop } from "../dist/lib/spinner.js";
import { purgeOptimized } from "../../css/dist/purger/optimized/purge.js";
import { purge } from "../../tailwind-v3/dist/purger/legacy/purgeCSS.js";
import { getPurgeRunOptions, shouldShowHelp } from "./purge.helpers.js";
//#region src/cli/commands/purge.ts
const showHelp = () => {
	print("");
	print(`${green("uds purge")} - Generate a Tailwind safelist from UDS usage`);
	print("");
	print(`${magenta("Usage:")}`);
	print(`  ${cyan("uds purge")} [options]`);
	print("");
	print(`${magenta("Options:")}`);
	print(`  ${cyan("--entry <path>")}       Entry directory or file to scan (default: ./src)`);
	print(`  ${cyan("--output <path>")}      Output safelist file (default: dist/safelist.ts)`);
	print(`  ${cyan("--config <path>")}      Path to uds.config.ts for legacy purge mode`);
	print(`  ${cyan("--optimized")}          Use the optimized AST-based purger implementation`);
	print(`  ${cyan("--help, -h")}           Show this help message`);
	print("");
	print(`${magenta("Examples:")}`);
	print(`  ${gray("uds purge")}`);
	print(`  ${gray("uds purge --optimized")}`);
	print(`  ${gray("uds purge --entry ./src --output ./dist/safelist.ts")}`);
	print(`  ${gray("uds purge --config ./uds.config.ts")}`);
	print("");
};
const makePurgeCommand = (getContext) => ({
	name: "purge",
	description: "Purge unused CSS",
	run: async (props) => {
		if (shouldShowHelp(props.options)) {
			showHelp();
			return;
		}
		const { config, entry, optimized, output } = getPurgeRunOptions(props.options);
		const { variants, autoVariants, componentData, packageJson, generatedPurgeCssData } = await getContext();
		if (optimized) {
			spinStart("Purging CSS with optimized purger...");
			try {
				const result = await purgeOptimized({
					entry,
					output,
					variants,
					autoVariants,
					componentData
				});
				spinStop("✅", `Purging done (optimized)! Generated ${result.stats.classesGenerated} classes in ${result.stats.timeMs}ms`);
				print(`   Files scanned: ${result.stats.filesScanned}`);
				print(`   Spreads traced: ${result.stats.spreadsTraced}`);
				print(`   Expressions resolved: ${result.stats.expressionsResolved}`);
				return;
			} catch (error) {
				if (error instanceof Error) spinStop("❌", error.message);
				else spinStop("❌", "Purging css failed! Please reach out to #uds-ask channel for support.");
				process.exitCode = 1;
				return;
			}
		}
		spinStart("Purging css started...");
		try {
			await purge(variants, packageJson, generatedPurgeCssData, {
				config,
				entry,
				output
			});
			spinStop("✅", "Purging css done!");
		} catch (error) {
			if (error instanceof Error) spinStop("❌", error.message);
			else spinStop("❌", "Purging css failed! Please reach out to #uds-ask channel for support.");
			process.exitCode = 1;
		}
	}
});
//#endregion
export { makePurgeCommand };
