/*! © 2026 Yahoo, Inc. UDS Tailwind and Purger v0.0.0-development */
import { DARK_COLOR_MODE_CLASSNAME, LIGHT_COLOR_MODE_CLASSNAME } from "../../../css-tokens/dist/index.js";
import { mkdirSync, writeFileSync } from "node:fs";
import path from "node:path";

//#region src/purger/optimized/utils/safelist.ts
const colorModeToClass = {
	dark: DARK_COLOR_MODE_CLASSNAME,
	light: LIGHT_COLOR_MODE_CLASSNAME
};
/**
* Deduplicate and clean a safelist
*/
const deduplicateSafelist = (safelist) => {
	const deduped = safelist.map((cls) => cls.replaceAll("\\", "").trim()).filter((cleaned) => cleaned && isValidClassName(cleaned));
	return Array.from(new Set(deduped));
};
/**
* Check if a class name is valid (not malformed from CSS escape sequences)
*/
const isValidClassName = (cls) => {
	if (cls.match(/^[0-9]/)) return false;
	if (cls.match(/[0-9][a-z]$/)) return false;
	if (cls.includes("2c")) return false;
	return true;
};
/**
* Get classes for enabled color modes.
* Note: Light mode is not included by default since :root already defines light mode styles.
* Only dark mode class is needed to enable dark mode switching.
*/
const getThemeAndScaleClasses = (colorModes) => {
	if (!colorModes || colorModes.length === 0) return [colorModeToClass.dark];
	return colorModes.map((mode) => colorModeToClass[mode]).filter((className) => Boolean(className));
};
/**
* Save safelist to a file
*/
const saveSafelistToFile = async (safelist, outputPath) => {
	const content = `
//! This file is generated by the optimized purger from @yahoo/uds
//! Do not edit directly
//! If there is issue with this file please report to #ask-uds
export const safelist = ${JSON.stringify(safelist)};
`;
	mkdirSync(path.dirname(outputPath), { recursive: true });
	writeFileSync(outputPath, content);
};

//#endregion
export { deduplicateSafelist, getThemeAndScaleClasses, saveSafelistToFile };
//# sourceMappingURL=safelist.js.map