/*! © 2026 Yahoo, Inc. UDS Tailwind and Purger v0.0.0-development */
//#region src/utils/postcssPreserveVars.ts
const PRESERVE_SELECTOR = "._uds-preserve-vars";
/**
* PostCSS plugin to preserve CSS variables from being pruned.
*
* Runs BEFORE postcss-prune-var to inject fake usages for preserved variables,
* preventing them from being removed.
*
* @example
* ```ts
* postcss([
*   preserveVars({ preserve: ['--uds-motion-'] }),
*   pruneVar(),
*   preserveVarsCleanup(),
* ])
* ```
*/
function preserveVars(options = {}) {
	const { preserve = [] } = options;
	return {
		postcssPlugin: "postcss-preserve-vars",
		Once(root) {
			if (preserve.length === 0) return;
			const declaredVars = /* @__PURE__ */ new Set();
			root.walkDecls((decl) => {
				if (decl.prop.startsWith("--")) declaredVars.add(decl.prop);
			});
			const varsToPreserve = [];
			for (const varName of declaredVars) for (const pattern of preserve) if (pattern instanceof RegExp ? pattern.test(varName) : varName.startsWith(pattern)) {
				varsToPreserve.push(varName);
				break;
			}
			if (varsToPreserve.length === 0) return;
			const varUsages = varsToPreserve.map((v) => `var(${v})`).join(" ");
			root.append({
				selector: PRESERVE_SELECTOR,
				nodes: [{
					prop: "content",
					value: `"${varUsages}"`
				}]
			});
		}
	};
}
preserveVars.postcss = true;
/**
* PostCSS plugin to remove the temporary preserve rule after pruning.
* Run this AFTER postcss-prune-var.
*/
function preserveVarsCleanup() {
	return {
		postcssPlugin: "postcss-preserve-vars-cleanup",
		Once(root) {
			root.walkRules((rule) => {
				if (rule.selector === PRESERVE_SELECTOR) rule.remove();
			});
		}
	};
}
preserveVarsCleanup.postcss = true;

//#endregion
export { preserveVars, preserveVarsCleanup };
//# sourceMappingURL=postcssPreserveVars.js.map