/*! © 2026 Yahoo, Inc. UDS Tailwind and Purger v0.0.0-development */
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
const require_colors = require('../cli/dist/lib/colors.cjs');
const require_print = require('../cli/dist/lib/print.cjs');
const require_spinner = require('../cli/dist/lib/spinner.cjs');
const require_entryPoints = require('../utils/entryPoints.cjs');
const require_nodeUtils = require('./nodeUtils.cjs');
const require_index = require('../config/dist/index.cjs');
const require_safelist = require('../purger/optimized/utils/safelist.cjs');
const require_utils = require('./utils.cjs');
const require_generate_helpers = require('./generate.helpers.cjs');
let node_fs = require("node:fs");
node_fs = require_runtime.__toESM(node_fs);
let node_path = require("node:path");
node_path = require_runtime.__toESM(node_path);
let node_zlib = require("node:zlib");
let postcss = require("postcss");
postcss = require_runtime.__toESM(postcss);

//#region src/css/generate.ts
const normalizeScope = (scope) => {
	const trimmedScope = scope?.trim();
	return trimmedScope ? trimmedScope : void 0;
};
const getArbitrarySafelistRawContent = (safelist) => {
	const arbitraryClasses = safelist.filter((entry) => typeof entry === "string" && entry.includes("["));
	if (arbitraryClasses.length === 0) return [];
	return [`<div class="${arbitraryClasses.join(" ")}"></div>`];
};
/**
* Generate CSS from a safelist using Tailwind + PostCSS
* @param safelist - UDS component classes to include
* @param config - Token configuration
* @param options - Additional options
*/
const generateCSS = async (safelist, config, options) => {
	const sourceCSS = "@tailwind base; @tailwind components; @tailwind utilities";
	const contentDir = options?.contentDir ?? node_path.default.join(process.cwd(), "src");
	const cssFlags = require_generate_helpers.getCssFeatureFlags(options?.cssOptions);
	const optimizationConfig = options?.cssOptions?.optimization;
	const scopeClass = normalizeScope(options?.scope) ?? normalizeScope(options?.cssOptions?.scope);
	let css = await require_generate_helpers.applyScopedColorModeFix((await (0, postcss.default)(require_generate_helpers.buildPostcssPlugins({
		tailwindPlugin: require_generate_helpers.createTailwindPlugin({
			contentDir,
			rawContents: getArbitrarySafelistRawContent(safelist),
			safelist,
			config,
			enablePreflight: cssFlags.enablePreflight,
			enableFontFaceDeclarations: cssFlags.enableFontFaceDeclarations
		}),
		shouldPruneVars: cssFlags.shouldPruneVars,
		safeVarPrefixes: options?.safeVarPrefixes ?? [],
		scopeClass
	})).process(sourceCSS, { from: void 0 })).css, scopeClass);
	const optimizedCss = await require_generate_helpers.optimizeGeneratedCss({
		css,
		shouldOptimize: cssFlags.shouldOptimize,
		optimizationConfig,
		referenceCss: options?.referenceCss,
		scopeClass
	});
	css = optimizedCss.css;
	const sizeBytes = Buffer.byteLength(css, "utf8");
	const sizeGzipBytes = (0, node_zlib.gzipSync)(css).length;
	return {
		css,
		sizeBytes,
		sizeGzipBytes,
		classCount: safelist.length,
		optimizationStats: optimizedCss.optimizationStats
	};
};
/**
* Generate CSS in simple mode (no uds.theme.ts)
*/
const generateSimpleModeCSS = async (options) => {
	const isWatch = options.isWatch === true;
	const shouldLogStats = !(options.silent === true) && !isWatch;
	const startTime = performance.now();
	const colorModes = ["dark"];
	let tokensConfig = require_index.defaultTokensConfig;
	if (options.configPath) {
		const loadedConfig = await require_nodeUtils.loadConfigFile(options.configPath);
		if (loadedConfig) tokensConfig = loadedConfig;
		else if (shouldLogStats) require_spinner.spinStop("⚠️", `Config file not found: ${options.configPath}, using defaults`);
	}
	const variantDefaults = require_utils.extractVariantDefaults(tokensConfig);
	const resolvedEntries = require_entryPoints.resolveEntryPaths(options.entry, options.workspaceDir);
	const entryDirs = resolvedEntries.map((entry) => entry.absolutePath);
	const scanResult = await require_nodeUtils.scanDirectoriesForSafelist(entryDirs, colorModes, options.variants, options.autoVariants, options.componentData, variantDefaults);
	const totalFilesScanned = scanResult.filesScanned;
	const allClasses = [...scanResult.safelist];
	const allComponents = new Set(scanResult.components);
	const packageDirs = [...new Set(resolvedEntries.map((entry) => entry.kind === "directory" ? entry.absolutePath : entry.watchDirectory))];
	const cssResult = await generateCSS(require_safelist.deduplicateSafelist([...allClasses, ...require_safelist.getThemeAndScaleClasses(colorModes)]), tokensConfig, {
		scope: options.scope,
		contentDir: entryDirs,
		safeVarPrefixes: [...require_utils.getMotionVarPrefixes(options.componentData, [...allComponents]), ...require_utils.getConfigurableCssVariables()]
	});
	const outputPath = node_path.default.isAbsolute(options.outFile) ? options.outFile : node_path.default.join(options.workspaceDir, options.outFile);
	const outputDir = node_path.default.dirname(outputPath);
	if (!node_fs.default.existsSync(outputDir)) node_fs.default.mkdirSync(outputDir, { recursive: true });
	node_fs.default.writeFileSync(outputPath, cssResult.css);
	const duration = Math.round(performance.now() - startTime);
	if (shouldLogStats) require_generate_helpers.printSimpleModeStats({
		outputPath,
		totalFilesScanned,
		classCount: cssResult.classCount,
		sizeBytes: cssResult.sizeBytes,
		sizeGzipBytes: cssResult.sizeGzipBytes,
		duration,
		scope: options.scope,
		optimizationStats: cssResult.optimizationStats,
		print: require_print.print,
		magenta: require_colors.magenta,
		cyan: require_colors.cyan,
		yellow: require_colors.yellow
	});
	return {
		success: true,
		outputPath,
		packageDirs
	};
};

//#endregion
exports.generateCSS = generateCSS;
exports.generateSimpleModeCSS = generateSimpleModeCSS;