/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
//#region ../css/dist/css/postcss.helpers.mjs
/*! © 2026 Yahoo, Inc. UDS CSS v0.0.0-development */
const COLOR_MODE_RE = /^(\.(uds-color-mode-(?:dark|light)))(\s+.+)?$/;
const escapeForRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const SCOPED_SELF_OR_PARENT_RE = /^(\.[^\s>+~]+)(.*)$/;
const getScopedColorModeAlternativeSelector = (selector, scopeClass, variant = "descendant") => {
	const scopeMatch = selector.match(new RegExp(`^(${escapeForRegExp(scopeClass)})\\s+(.+)$`));
	if (!scopeMatch) return null;
	const colorModeMatch = scopeMatch[2].match(COLOR_MODE_RE);
	if (!colorModeMatch) return null;
	const colorModeClass = colorModeMatch[1];
	const rest = colorModeMatch[3] ?? "";
	return variant === "same-element" ? `${colorModeClass}${scopeClass}${rest}` : `${colorModeClass} ${scopeClass}${rest}`;
};
const buildScopedColorModeSelectorList = (ruleSelector, scopeClass) => {
	const selectors = ruleSelector.split(",").map((value) => value.trim());
	return selectors.reduce((acc, selector) => {
		const nextAltSelectors = [getScopedColorModeAlternativeSelector(selector, scopeClass), getScopedColorModeAlternativeSelector(selector, scopeClass, "same-element")].filter((value) => Boolean(value)).filter((altSelector) => !selectors.includes(altSelector) && !acc.includes(altSelector));
		return [
			...acc,
			selector,
			...nextAltSelectors
		];
	}, []).join(", ");
};
const getScopedSelfOrParentAlternativeSelector = (selector, scopeClass) => {
	const scopeMatch = selector.match(new RegExp(`^(${escapeForRegExp(scopeClass)})\\s+(.+)$`));
	if (!scopeMatch) return null;
	const targetMatch = scopeMatch[2].match(SCOPED_SELF_OR_PARENT_RE);
	if (!targetMatch) return null;
	const scopedTarget = targetMatch[1];
	if (/^\.uds-color-mode-(?:dark|light)(?:$|[.#[:])/.test(scopedTarget)) return null;
	return `${scopeClass}${scopedTarget}${targetMatch[2]}`;
};
//#endregion
export { buildScopedColorModeSelectorList, getScopedSelfOrParentAlternativeSelector };
