/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { cyan, gray, yellow } from "../../cli/dist/lib/colors.js";
import { print } from "../../cli/dist/lib/print.js";
import { generateThemeResult } from "./generateTheme-DkTgUwTM.js";
import path from "node:path";
import fs from "node:fs/promises";
//#region ../tailwind-v4/dist/cli.js
/*! © 2026 Yahoo, Inc. UDS Tailwind v4 v0.0.0-development */
const ALLOWED_DISABLE_OVERRIDES = new Set([
	"color",
	"border",
	"elevation",
	"font",
	"text",
	"spacing",
	"radius",
	"shadow",
	"breakpoint"
]);
function parseDisableOverride(raw) {
	if (!raw || raw === true) return [];
	const values = String(raw).split(",").map((value) => value.trim()).filter(Boolean);
	for (const value of values) if (!ALLOWED_DISABLE_OVERRIDES.has(value)) throw new Error(`Invalid --disable-override entry '${value}'. Allowed values: ${Array.from(ALLOWED_DISABLE_OVERRIDES).join(", ")}.`);
	return values;
}
function isTruthyFlag(value) {
	if (value === true) return true;
	if (typeof value !== "string") return false;
	const normalized = value.trim().toLowerCase();
	return normalized === "true" || normalized === "1" || normalized === "";
}
function parseColorScheme(raw) {
	if (!raw || raw === true) return "system";
	const value = String(raw).trim().toLowerCase();
	if (value === "class" || value === "system" || value === "light" || value === "dark") return value;
	throw new Error(`Invalid --color-scheme value '${value}'. Allowed: "system", "class", "light", "dark".`);
}
function parseColorMode(raw) {
	if (!raw || raw === true) return "light";
	const value = String(raw).trim().toLowerCase();
	if (value === "light" || value === "dark") return value;
	throw new Error(`Invalid --default-color-mode value '${value}'. Allowed: "light", "dark".`);
}
function showHelp() {
	print("Usage:\n");
	print(`  ${cyan("uds tailwind-v4 [options]")}\n`);
	print("Options:\n");
	print(`  ${cyan("--out <path>")}                        Output path (default: dist/uds-theme.css)`);
	print(`  ${cyan("--color-scheme <mode>")}             Color mode strategy: "system" (media query, default), "class" (CSS classes), "light", or "dark"`);
	print(`  ${cyan("--default-color-mode <mode>")}       Default color mode: "light" (default) or "dark"`);
	print(`  ${cyan("--light-mode-class <name>")}         CSS class name for light mode (default: light)`);
	print(`  ${cyan("--dark-mode-class <name>")}          CSS class name for dark mode (default: dark)`);
	print(`  ${cyan("--system-mode-class <name>")}        CSS class name for system mode (default: system)`);
	print(`  ${cyan("--disable-override <list>")}          Comma-separated categories to suppress. Allowed: color, border, elevation, font, text, spacing, radius, shadow, breakpoint`);
	print(`  ${cyan("--skip-fonts")}                       Skip emitting @font-face declarations`);
	print(`  ${cyan("--help, -h")}                         Show this help message\n`);
	print("Examples:\n");
	print(`  ${gray("uds tailwind-v4")}`);
	print(`  ${gray("uds tailwind-v4 --skip-fonts")}`);
	print(`  ${gray("uds tailwind-v4 --color-scheme system")}`);
	print(`  ${gray("uds tailwind-v4 --color-scheme class")}`);
	print(`  ${gray("uds tailwind-v4 --color-scheme class --dark-mode-class dark2 --light-mode-class light2")}`);
	print(`  ${gray("uds tailwind-v4 --default-color-mode dark")}`);
	print(`  ${gray("uds tailwind-v4 --color-scheme light")}`);
	print(`  ${gray("uds tailwind-v4 --color-scheme dark")}`);
}
function makeTailwindV4ThemeCommand({ getConfig }) {
	return {
		name: "tailwind-v4",
		description: "Generate Tailwind v4 @theme CSS (with optional @font-face declarations)",
		run: async ({ options }) => {
			if (isTruthyFlag(options.help) || isTruthyFlag(options.h)) {
				showHelp();
				return;
			}
			const outputPath = path.resolve(process.cwd(), String(options.out ?? "dist/uds-theme.css"));
			await fs.mkdir(path.dirname(outputPath), { recursive: true });
			const tailwindOptions = {
				disableOverrideTailwindTheme: parseDisableOverride(options["disable-override"]),
				includeFontFace: !isTruthyFlag(options["skip-fonts"]),
				colorScheme: parseColorScheme(options["color-scheme"]),
				defaultColorMode: parseColorMode(options["default-color-mode"]),
				lightModeClass: String(options["light-mode-class"] ?? "light"),
				darkModeClass: String(options["dark-mode-class"] ?? "dark"),
				systemModeClass: String(options["system-mode-class"] ?? "system")
			};
			const { css, warnings } = generateThemeResult(await getConfig(), tailwindOptions);
			await fs.writeFile(outputPath, css, "utf8");
			print(`✅ Generated Tailwind v4 theme CSS at ${cyan(outputPath)}`);
			if (warnings.length > 0) {
				print(yellow(`⚠ ${warnings.length} warnings encountered during theme generation:`));
				for (const warning of warnings.slice(0, 10)) print(yellow(`- ${warning.path}: ${warning.message}`));
				if (warnings.length > 10) print(yellow(`- ...and ${warnings.length - 10} more warnings.`));
			}
		}
	};
}
//#endregion
export { makeTailwindV4ThemeCommand };
