/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { DEFAULT_ENTRY } from "../../../../tailwind-internal/dist/utils/entryPoints.js";
import { purgeFromCodeOptimized, resolveWrapperSpreadSafelist } from "./purgeFromCode.js";
import { TEST_FILE_IGNORE_GLOBS, scanForFiles } from "./utils/files.js";
import { deduplicateSafelist, getInternalSafelistClasses, getThemeAndScaleClasses, saveSafelistToFile } from "./utils/safelist.js";
import fs from "node:fs";
import path from "node:path";
//#region ../css/dist/purger/optimized/purge.mjs
/*! © 2026 Yahoo, Inc. UDS CSS v0.0.0-development */
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"], ignoreDevFiles = true, ignore = [], includeStories = false, variants, autoVariants, componentData } = options;
	const startTime = performance.now();
	clearWarnings();
	const workspaceDir = process.env.PWD ?? process.cwd();
	const files = await scanForFiles(entry, ignoreDevFiles && !includeStories, ignoreDevFiles && includeStories ? [...ignore, ...TEST_FILE_IGNORE_GLOBS] : ignore);
	const allClasses = [];
	const allImports = /* @__PURE__ */ new Set();
	const allComponents = /* @__PURE__ */ new Set();
	const stats = {
		filesScanned: files.length,
		timeMs: 0,
		classesGenerated: 0,
		spreadsTraced: 0,
		spreadsUnresolved: 0,
		expressionsResolved: 0
	};
	const fileContents = files.map((filePath) => fs.readFileSync(filePath, "utf-8"));
	const results = await Promise.all(fileContents.map(async (code) => {
		return purgeFromCodeOptimized(code, {
			colorModes,
			variants,
			autoVariants,
			componentData,
			deferWrapperSpreadResolution: true
		});
	}));
	results.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.spreadsUnresolved += result.stats.spreadsUnresolved;
		stats.expressionsResolved += result.stats.expressionsResolved;
	});
	allClasses.push(...resolveWrapperSpreadSafelist(results.flatMap((result) => result.unresolvedWrapperSpreads), fileContents, variants));
	allClasses.push(...getThemeAndScaleClasses(colorModes));
	allClasses.push(...getInternalSafelistClasses());
	const finalSafelist = deduplicateSafelist(allClasses).sort();
	await saveSafelistToFile(finalSafelist, path.isAbsolute(output) ? output : 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 };
