/*! © 2026 Yahoo, Inc. UDS Tailwind and Purger v0.0.0-development */
import { print } from "../cli/dist/lib/print.js";
import { analyzeComponent, scanComponentFiles } from "../purger/optimized/utils/componentAnalyzer.js";
import { mkdirSync, writeFileSync } from "node:fs";
import { dirname } from "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();
		print("Scanning components in:", componentsDir);
		const componentPaths = await scanComponentFiles(componentsDir);
		print(`Found ${componentPaths.size} components`);
		const componentDataEntries = await Promise.all([...componentPaths].map(async ([name, filePath]) => {
			try {
				project.addSourceFileAtPath(filePath);
				const info = 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)));
		mkdirSync(dirname(outputFile), { recursive: true });
		writeFileSync(outputFile, JSON.stringify(componentData, null, 2));
		print(`Generated component data for ${Object.keys(componentData).length} components`);
		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
		});
		print("\nStats:");
		print(`  Default props: ${totals.totalDefaults}`);
		print(`  getStyles literals: ${totals.totalLiterals}`);
		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
export { makeGenerateComponentDataCommand };
//# sourceMappingURL=generateComponentData.js.map