/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { DARK_COLOR_MODE_CLASSNAME, LIGHT_COLOR_MODE_CLASSNAME } from "../../../packages/css-tokens/dist/index.js";
import path from "node:path";
import { mkdirSync, writeFileSync } from "node:fs";
//#region ../css/dist/purger/optimized/utils/safelist.mjs
/*! © 2026 Yahoo, Inc. UDS CSS v0.0.0-development */
const colorModeToClass = {
	dark: DARK_COLOR_MODE_CLASSNAME,
	light: LIGHT_COLOR_MODE_CLASSNAME
};
const internalSafelistClasses = ["uds-light-mode-icon", "uds-dark-mode-icon"];
/**
* 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)
*
* The heuristics below target garbage fragments produced when escaped selectors
* (e.g. the comma escape `\2c `) are stripped and split — for example `color2c`
* or `0invalid`. They must NOT run against arbitrary-value classes, which
* legitimately contain brackets, commas, and substrings like `2c`, `2ch`, or
* `2cm` (e.g. `bg-[#2c2c2c]`, `w-[2ch]`, `[transition-timing-function:cubic-bezier(0,0,0.2,1)]`).
*/
const isValidClassName = (cls) => {
	if (cls.includes("[")) return true;
	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));
};
/**
* Get internal framework classes that are required even when plugin safelists
* are intentionally ignored by the optimized CSS generation path.
*/
const getInternalSafelistClasses = () => [...internalSafelistClasses];
/**
* 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, getInternalSafelistClasses, getThemeAndScaleClasses, saveSafelistToFile };
