/*! © 2026 Yahoo, Inc. UDS Tailwind and Purger v0.0.0-development */
import { buildScopedColorModeSelectorList, getScopedSelfOrParentAlternativeSelector } from "./postcss.helpers.js";
//#region src/css/postcss.ts
const fixScopedSelfOrParentSelectorsPlugin = (scopeClass) => {
	return {
		postcssPlugin: "fix-scoped-self-or-parent-selectors",
		Once(root) {
			root.walkRules((rule) => {
				if (!rule.selector.includes(scopeClass)) return;
				const nextSelectors = rule.selectors.reduce((acc, selector) => {
					const sameElementSelector = getScopedSelfOrParentAlternativeSelector(selector, scopeClass);
					if (sameElementSelector && !rule.selectors.includes(sameElementSelector)) return [
						...acc,
						selector,
						sameElementSelector
					];
					return [...acc, selector];
				}, []);
				if (nextSelectors.length !== rule.selectors.length) rule.selectors = nextSelectors;
			});
		}
	};
};
/**
* PostCSS plugin to fix color mode selectors for scoped CSS.
*
* postcss-scope converts selectors like:
*   `.uds-color-mode-dark .foo` → `.scope .uds-color-mode-dark .foo`
*
* But the color mode class is typically on <html> or <body>, which is an
* ANCESTOR of the scope, not a descendant. This plugin adds alternative
* selectors so color modes work correctly:
*
*   `.scope .uds-color-mode-dark .foo` → `.scope .uds-color-mode-dark .foo, .uds-color-mode-dark .scope .foo, .uds-color-mode-dark.scope .foo`
*
* This ensures styles apply whether the color mode class is inside or outside the scope.
*/
const fixScopedColorModeSelectorsPlugin = (scopeClass) => {
	const colorModePattern = /\.(uds-color-mode-(?:dark|light))/;
	return {
		postcssPlugin: "fix-scoped-color-mode-selectors",
		Once(root) {
			root.walkRules((rule) => {
				if (!rule.selector.includes(scopeClass) || !colorModePattern.test(rule.selector)) return;
				const nextSelector = buildScopedColorModeSelectorList(rule.selector, scopeClass);
				if (nextSelector !== rule.selector) rule.selector = nextSelector;
			});
		}
	};
};
//#endregion
export { fixScopedColorModeSelectorsPlugin, fixScopedSelfOrParentSelectorsPlugin };

//# sourceMappingURL=postcss.js.map