/*! © 2026 Yahoo, Inc. UDS Tailwind and Purger v0.0.0-development */
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
const require_print = require('../cli/dist/lib/print.cjs');
const require_componentAnalyzer = require('../purger/optimized/utils/componentAnalyzer.cjs');
let node_fs = require("node:fs");
let node_path = require("node:path");

//#region src/commands/generateComponentData.ts
/**
* Pre-generates component analysis data at build time.
* This data is used by the optimized purger to know:
* - Default prop values for each component
* - Hardcoded literals in getStyles() calls
* - Internal component dependencies
* - Literal props passed to internal components
*/
const makeGenerateComponentDataCommand = (getContext) => ({
	name: "generateComponentData",
	description: `Generates component data for UDS components. This is an internal command used to by the optimized purger.`,
	tag: "hidden",
	run: async (_props) => {
		const { project, componentsDir, outputFile } = await getContext();
		require_print.print("Scanning components in:", componentsDir);
		const componentPaths = await require_componentAnalyzer.scanComponentFiles(componentsDir);
		require_print.print(`Found ${componentPaths.size} components`);
		const componentDataEntries = await Promise.all([...componentPaths].map(async ([name, filePath]) => {
			try {
				project.addSourceFileAtPath(filePath);
				const info = require_componentAnalyzer.analyzeComponent(project, filePath);
				return info ? [name, serializeComponentInfo(info)] : null;
			} catch (error) {
				console.warn(`Warning: Could not analyze ${name}:`, error);
				return null;
			}
		}));
		const componentData = Object.fromEntries(componentDataEntries.filter((entry) => Boolean(entry)));
		(0, node_fs.mkdirSync)((0, node_path.dirname)(outputFile), { recursive: true });
		(0, node_fs.writeFileSync)(outputFile, JSON.stringify(componentData, null, 2));
		require_print.print(`Generated component data for ${Object.keys(componentData).length} components`);
		require_print.print(`Output written to: ${outputFile}`);
		const totals = Object.values(componentData).reduce((acc, comp) => ({
			totalDefaults: acc.totalDefaults + Object.keys(comp.defaultProps).length,
			totalLiterals: acc.totalLiterals + Object.keys(comp.getStylesLiterals).length,
			totalInternalComponents: acc.totalInternalComponents + comp.internalComponents.length
		}), {
			totalDefaults: 0,
			totalLiterals: 0,
			totalInternalComponents: 0
		});
		require_print.print("\nStats:");
		require_print.print(`  Default props: ${totals.totalDefaults}`);
		require_print.print(`  getStyles literals: ${totals.totalLiterals}`);
		require_print.print(`  Internal component refs: ${totals.totalInternalComponents}`);
	}
});
const serializeComponentInfo = (info) => ({
	name: info.name,
	defaultProps: Object.fromEntries(info.defaultProps),
	getStylesLiterals: Object.fromEntries(info.getStylesLiterals),
	cxLiterals: info.cxLiterals,
	internalComponents: info.internalComponents,
	internalComponentProps: Object.fromEntries(Array.from(info.internalComponentProps.entries()).map(([key, propsMap]) => [key, Object.fromEntries(propsMap)])),
	propToVariantKeys: Object.fromEntries(info.propToVariantKeys),
	motionVarPrefixes: info.motionVarPrefixes
});

//#endregion
exports.makeGenerateComponentDataCommand = makeGenerateComponentDataCommand;