/*! © 2026 Yahoo, Inc. UDS Tailwind and Purger v0.0.0-development */
import { purgeFromCodeOptimized } from "./purgeFromCode.js";
import { DEFAULT_ENTRY } from "../../utils/entryPoints.js";
import { deduplicateSafelist, getThemeAndScaleClasses, saveSafelistToFile } from "./utils/safelist.js";
import { scanForFiles } from "./utils/files.js";
import fs from "node:fs";
import path from "node:path";

//#region src/purger/optimized/purge.ts
const warnedLocations = /* @__PURE__ */ new Set();
/**
* Clear warned locations (call at start of new purge run)
*/
const clearWarnings = () => {
	warnedLocations.clear();
};
/**
* Main purge function - optimized version
*
* Uses purgeFromCodeOptimized internally to ensure consistent behavior
* between CLI and playground.
*/
const purgeOptimized = async (options) => {
	const { entry = DEFAULT_ENTRY, output = "dist/safelist.ts", colorModes = ["dark"], variants, autoVariants, componentData } = options;
	const startTime = performance.now();
	clearWarnings();
	const workspaceDir = process.env.PWD ?? process.cwd();
	const files = await scanForFiles(entry);
	const allClasses = [];
	const allImports = /* @__PURE__ */ new Set();
	const allComponents = /* @__PURE__ */ new Set();
	const stats = {
		filesScanned: files.length,
		timeMs: 0,
		classesGenerated: 0,
		spreadsTraced: 0,
		expressionsResolved: 0
	};
	(await Promise.all(files.map(async (filePath) => {
		return purgeFromCodeOptimized(fs.readFileSync(filePath, "utf-8"), {
			colorModes,
			variants,
			autoVariants,
			componentData
		});
	}))).forEach((result) => {
		allClasses.push(...result.safelist);
		result.imports.forEach((imp) => allImports.add(imp));
		result.components.forEach((comp) => allComponents.add(comp));
		stats.spreadsTraced += result.stats.spreadsTraced;
		stats.expressionsResolved += result.stats.expressionsResolved;
	});
	allClasses.push(...getThemeAndScaleClasses(colorModes));
	const finalSafelist = deduplicateSafelist(allClasses).sort();
	await saveSafelistToFile(finalSafelist, path.join(workspaceDir, output));
	stats.timeMs = Math.round(performance.now() - startTime);
	stats.classesGenerated = finalSafelist.length;
	return {
		safelist: finalSafelist,
		imports: [...allImports],
		components: [...allComponents],
		stats
	};
};

//#endregion
export { purgeOptimized };
//# sourceMappingURL=purge.js.map