/*! © 2026 Yahoo, Inc. UDS Tailwind and Purger v0.0.0-development */
import { DARK_COLOR_MODE_CLASSNAME, LARGE_SCALE_MODE_CLASSNAME, LIGHT_COLOR_MODE_CLASSNAME } from "../../css-tokens/dist/index.js";
import { getComponentsToConvertToTW, getTailwindSafelist, parseFiles } from "./purgeCSS.js";
import { Project, ts } from "ts-morph";

//#region src/purger/legacy/purgeFromCode.ts
const COLOR_MODE_CLASSES = {
	dark: DARK_COLOR_MODE_CLASSNAME,
	light: LIGHT_COLOR_MODE_CLASSNAME
};
/**
* Run the legacy purger on a code string and return the safelist.
* This uses the same logic as the CLI `uds purge` command but with in-memory files.
*
* @param code - The TypeScript/TSX code to analyze
* @param options - Configuration options for color modes
* @returns The purge result containing safelist, imports, and components
*/
function purgeFromCode(code, options) {
	const { colorModes = ["dark", "light"] } = options;
	try {
		const project = new Project({
			useInMemoryFileSystem: true,
			compilerOptions: {
				jsx: ts.JsxEmit.ReactJSX,
				jsxImportSource: "react",
				target: ts.ScriptTarget.ESNext,
				module: ts.ModuleKind.ESNext,
				moduleResolution: ts.ModuleResolutionKind.NodeJs,
				esModuleInterop: true,
				strict: true
			}
		});
		const imports = parseFiles(project, [project.createSourceFile("user-code.tsx", code).getFilePath()]);
		const components = getComponentsToConvertToTW(imports, options.generatedPurgeCssData);
		const safelist = getTailwindSafelist(project, components, options.variants, options.generatedPurgeCssData, options.packageJson);
		for (const mode of colorModes) {
			const cls = COLOR_MODE_CLASSES[mode];
			if (cls) safelist.push(cls);
		}
		safelist.push(LARGE_SCALE_MODE_CLASSNAME);
		return {
			safelist: Array.from(new Set(safelist)),
			imports,
			components
		};
	} catch (error) {
		return {
			safelist: [],
			imports: [],
			components: [],
			error: error instanceof Error ? error.message : "Unknown error"
		};
	}
}

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