/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { trackEvent } from "../utils/analytics.js";
import { getAvailableEditors, getEditorConfig, getRuleContent } from "../utils/rules/config.js";
import { blue, green, red, yellow } from "../lib/colors.js";
import { print } from "../lib/print.js";
import { join } from "node:path";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";

//#region ../cli/dist/commands/editor-rules.mjs
/*! © 2026 Yahoo, Inc. UDS CLI v0.0.0-development */
var editor_rules_default = {
	name: "editor-rules",
	description: "🎯 Generate editor rules for various code editors and AI tools",
	alias: ["rules", "editor"],
	run: async ({ options }) => {
		if (process.argv.includes("--help") || process.argv.includes("-h")) {
			print(blue("🎯 UDS Editor Rules Command"));
			print("");
			print(green("Description: Generate editor rules for various code editors and AI tools"));
			print("");
			print(yellow("Options:"));
			print("  --output, -o    Output directory (defaults to workspace root)");
			print("  --editor, -e    Specific editor/tool (defaults to all)");
			print("");
			print(blue("Available editors/tools:"));
			const editors = getAvailableEditors();
			for (const editor of editors) {
				const config = getEditorConfig(editor);
				if (config) print(`  • ${editor}: ${config.description}`);
			}
			print("");
			print(blue("Examples:"));
			print("  uds editor-rules                    # Generate all rules in current directory");
			print("  uds editor-rules --editor cursor    # Generate only Cursor rules");
			print("  uds editor-rules --editor claude    # Generate only Claude Code rules");
			print("  uds editor-rules --output /path     # Generate rules in specific directory");
			print("");
			print(green("What it generates:"));
			print("  • Cursor Rules (.cursor/rules/uds/)");
			print("  • Claude Code Rules (.claude/rules/uds/)");
			return;
		}
		try {
			const outputDir = options.output || process.cwd();
			const selectedEditor = options.editor?.toLowerCase();
			print(blue(`📁 Generating editor rules in: ${outputDir}`));
			if (selectedEditor) {
				const editorConfig = getEditorConfig(selectedEditor);
				if (!editorConfig) {
					print(red(`❌ Unknown editor: ${selectedEditor}`));
					print(yellow("Available editors:"));
					const editors = getAvailableEditors();
					for (const editor of editors) {
						const config = getEditorConfig(editor);
						if (config) print(`  • ${editor}: ${config.description}`);
					}
					return;
				}
				await generateEditorRules(outputDir, editorConfig);
			} else {
				const editors = getAvailableEditors();
				for (const editor of editors) {
					const editorConfig = getEditorConfig(editor);
					if (editorConfig) await generateEditorRules(outputDir, editorConfig);
				}
			}
			print(green("🎉 Editor rules generated successfully!"));
			print(blue("📝 Rules are now available for your development environment"));
			await trackEvent("editor_rules_generated", {
				output_dir: outputDir,
				editor: selectedEditor || "all"
			});
		} catch (error) {
			print(red(`❌ Error generating editor rules: ${error}`));
			process.exitCode = 1;
		}
	}
};
async function generateEditorRules(outputDir, editorConfig) {
	print(blue(`\n🔧 Generating ${editorConfig.name} rules...`));
	for (const rule of editorConfig.rules) {
		print(yellow(`  📝 ${rule.name}: ${rule.description}`));
		for (const file of rule.files) {
			const targetPath = join(outputDir, file.target);
			const targetDir = join(targetPath, "..");
			if (!existsSync(targetDir)) mkdirSync(targetDir, { recursive: true });
			const exists = existsSync(targetPath);
			try {
				const content = getRuleContent(file.source);
				if (exists) {
					if (readFileSync(targetPath, "utf-8") === content) {
						print(green(`    ✅ Up to date: ${file.target}`));
						continue;
					}
					writeFileSync(targetPath, content);
					print(green(`    ✅ Updated: ${file.target}`));
				} else {
					writeFileSync(targetPath, content);
					print(green(`    ✅ Created: ${file.target}`));
				}
			} catch (error) {
				print(red(`    ❌ Error creating ${file.target}: ${error}`));
			}
		}
	}
}

//#endregion
export { editor_rules_default };