/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { dirname, join } from "node:path";
import { existsSync, readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";

//#region ../cli/dist/utils/rules/config.mjs
/*! © 2026 Yahoo, Inc. UDS CLI v0.0.0-development */
const __dirname = dirname(fileURLToPath(import.meta.url));
const EDITOR_CONFIGS = {
	cursor: {
		name: "Cursor",
		description: "AI-powered coding assistant rules for UDS development",
		rules: [
			{
				name: "UDS Components",
				description: "Component development guidelines and patterns",
				files: [{
					source: "rules/cursor/uds/components.mdc",
					target: ".cursor/rules/uds/components.mdc",
					type: "file"
				}]
			},
			{
				name: "UDS Tailwind",
				description: "Tailwind CSS configuration and setup guidelines",
				files: [{
					source: "rules/cursor/uds/tailwind.mdc",
					target: ".cursor/rules/uds/tailwind.mdc",
					type: "file"
				}]
			},
			{
				name: "UDS Icons",
				description: "Icon usage and documentation guidelines",
				files: [{
					source: "rules/cursor/uds/icons.mdc",
					target: ".cursor/rules/uds/icons.mdc",
					type: "file"
				}]
			},
			{
				name: "UDS Elevation",
				description: "Elevation prop usage for layered surfaces",
				files: [{
					source: "rules/cursor/uds/elevation.mdc",
					target: ".cursor/rules/uds/elevation.mdc",
					type: "file"
				}]
			},
			{
				name: "UDS Styling",
				description: "Color palette and styling guidelines",
				files: [{
					source: "rules/cursor/uds/styling.mdc",
					target: ".cursor/rules/uds/styling.mdc",
					type: "file"
				}]
			}
		]
	},
	claude: {
		name: "Claude Code",
		description: "AI-powered coding assistant rules for UDS development",
		rules: [
			{
				name: "UDS Components",
				description: "Component development guidelines and patterns",
				files: [{
					source: "rules/claude/uds/components.md",
					target: ".claude/rules/uds/components.md",
					type: "file"
				}]
			},
			{
				name: "UDS Tailwind",
				description: "Tailwind CSS configuration and setup guidelines",
				files: [{
					source: "rules/claude/uds/tailwind.md",
					target: ".claude/rules/uds/tailwind.md",
					type: "file"
				}]
			},
			{
				name: "UDS Icons",
				description: "Icon usage and documentation guidelines",
				files: [{
					source: "rules/claude/uds/icons.md",
					target: ".claude/rules/uds/icons.md",
					type: "file"
				}]
			},
			{
				name: "UDS Elevation",
				description: "Elevation prop usage for layered surfaces",
				files: [{
					source: "rules/claude/uds/elevation.md",
					target: ".claude/rules/uds/elevation.md",
					type: "file"
				}]
			},
			{
				name: "UDS Styling",
				description: "Color palette and styling guidelines",
				files: [{
					source: "rules/claude/uds/styling.md",
					target: ".claude/rules/uds/styling.md",
					type: "file"
				}]
			}
		]
	}
};
function getAvailableEditors() {
	return Object.keys(EDITOR_CONFIGS);
}
function getEditorConfig(editor) {
	return EDITOR_CONFIGS[editor];
}
const AUTO_GENERATED_HEADER = "<!-- Auto-generated by UDS CLI (`uds editor-rules`). Do not edit manually. -->";
function getRuleContent(rulePath) {
	return addAutoGeneratedHeader(readFileSync(join(__dirname, "..", "..", "..", rulePath), "utf-8"));
}
function addAutoGeneratedHeader(content) {
	if (content.startsWith("---\n")) {
		const closingIndex = content.indexOf("\n---\n", 4);
		if (closingIndex !== -1) return `${content.slice(0, closingIndex + 5)}${AUTO_GENERATED_HEADER}\n${content.slice(closingIndex + 5)}`;
	}
	return `${AUTO_GENERATED_HEADER}\n\n${content}`;
}
/**
* Checks which editors have installed UDS rules that are out of date
* compared to the latest rules shipped with the CLI.
* Only checks editors whose rule directories already exist.
*/
function getEditorsWithStaleRules(projectDir) {
	const staleEditors = [];
	for (const [_editorId, editorConfig] of Object.entries(EDITOR_CONFIGS)) {
		let hasAnyRule = false;
		let hasStaleRule = false;
		for (const rule of editorConfig.rules) for (const file of rule.files) {
			const targetPath = join(projectDir, file.target);
			if (!existsSync(targetPath)) continue;
			hasAnyRule = true;
			try {
				if (readFileSync(targetPath, "utf-8") !== getRuleContent(file.source)) hasStaleRule = true;
			} catch {}
		}
		if (hasAnyRule && hasStaleRule) staleEditors.push(editorConfig.name);
	}
	return staleEditors;
}

//#endregion
export { getAvailableEditors, getEditorConfig, getEditorsWithStaleRules, getRuleContent };