/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-b859c78 | Cube Dev Team */
import { Lru } from "../parser/lru.js";

//#region src/tasty/pipeline/materialize.ts
/**
* CSS Materialization
*
* Converts condition trees into CSS selectors and at-rules.
* This is the final stage that produces actual CSS output.
*/
const conditionCache = new Lru(3e3);
/**
* Convert a condition tree to CSS components
*/
function conditionToCSS(node) {
	const key = getConditionKey(node);
	const cached = conditionCache.get(key);
	if (cached) return cached;
	const result = conditionToCSSInner(node);
	conditionCache.set(key, result);
	return result;
}
/**
* Create an empty selector variant
*/
function emptyVariant() {
	return {
		modifierConditions: [],
		pseudoConditions: [],
		ownConditions: [],
		mediaConditions: [],
		containerConditions: [],
		supportsConditions: [],
		rootConditions: [],
		startingStyle: false
	};
}
function conditionToCSSInner(node) {
	if (node.kind === "true") return {
		variants: [emptyVariant()],
		isImpossible: false
	};
	if (node.kind === "false") return {
		variants: [],
		isImpossible: true
	};
	if (node.kind === "state") return stateToCSS(node);
	if (node.kind === "compound") if (node.operator === "AND") return andToCSS(node.children);
	else return orToCSS(node.children);
	return {
		variants: [emptyVariant()],
		isImpossible: false
	};
}
/**
* Convert a state condition to CSS
*/
function stateToCSS(state) {
	switch (state.type) {
		case "media": return {
			variants: mediaToParsed(state).map((mediaCond) => {
				const v = emptyVariant();
				v.mediaConditions.push(mediaCond);
				return v;
			}),
			isImpossible: false
		};
		default: {
			const variant = emptyVariant();
			switch (state.type) {
				case "modifier":
					variant.modifierConditions.push(modifierToParsed(state));
					break;
				case "pseudo":
					variant.pseudoConditions.push(pseudoToParsed(state));
					break;
				case "container":
					variant.containerConditions.push(containerToParsed(state));
					break;
				case "supports":
					variant.supportsConditions.push(supportsToParsed(state));
					break;
				case "root":
					variant.rootConditions.push(rootToParsed(state));
					break;
				case "own":
					variant.ownConditions.push(...ownToParsed(state));
					break;
				case "starting":
					variant.startingStyle = !state.negated;
					break;
			}
			return {
				variants: [variant],
				isImpossible: false
			};
		}
	}
}
/**
* Convert modifier condition to parsed structure
*/
function modifierToParsed(state) {
	return {
		attribute: state.attribute,
		value: state.value,
		operator: state.operator,
		negated: state.negated ?? false
	};
}
/**
* Convert parsed modifier to CSS selector string (for final output)
*/
function modifierToCSS(mod) {
	let selector;
	if (mod.value !== void 0) {
		const op = mod.operator || "=";
		selector = `[${mod.attribute}${op}"${mod.value}"]`;
	} else selector = `[${mod.attribute}]`;
	if (mod.negated) return `:not(${selector})`;
	return selector;
}
/**
* Convert pseudo condition to parsed structure
*/
function pseudoToParsed(state) {
	return {
		pseudo: state.pseudo,
		negated: state.negated ?? false
	};
}
/**
* Convert parsed pseudo to CSS selector string (for final output)
*/
function pseudoToCSS(pseudo) {
	if (pseudo.negated) {
		if (pseudo.pseudo.startsWith(":not(")) return pseudo.pseudo.slice(5, -1);
		return `:not(${pseudo.pseudo})`;
	}
	return pseudo.pseudo;
}
/**
* Convert media condition to parsed structure(s)
* Returns an array because negated ranges produce OR branches (two separate conditions)
*/
function mediaToParsed(state) {
	if (state.subtype === "type") {
		const mediaType = state.mediaType || "all";
		return [{
			subtype: "type",
			negated: state.negated ?? false,
			condition: mediaType,
			mediaType: state.mediaType
		}];
	} else if (state.subtype === "feature") {
		let condition;
		if (state.featureValue) condition = `(${state.feature}: ${state.featureValue})`;
		else condition = `(${state.feature})`;
		return [{
			subtype: "feature",
			negated: state.negated ?? false,
			condition,
			feature: state.feature,
			featureValue: state.featureValue
		}];
	} else return dimensionToMediaParsed(state.dimension || "width", state.lowerBound, state.upperBound, state.negated ?? false);
}
/**
* Convert dimension bounds to parsed media condition(s)
* Uses CSS Media Queries Level 4 `not (condition)` syntax for negation.
*/
function dimensionToMediaParsed(dimension, lowerBound, upperBound, negated) {
	let condition;
	if (lowerBound && upperBound) {
		const lowerOp = lowerBound.inclusive ? "<=" : "<";
		const upperOp = upperBound.inclusive ? "<=" : "<";
		condition = `(${lowerBound.value} ${lowerOp} ${dimension} ${upperOp} ${upperBound.value})`;
	} else if (upperBound) condition = `(${dimension} ${upperBound.inclusive ? "<=" : "<"} ${upperBound.value})`;
	else if (lowerBound) condition = `(${dimension} ${lowerBound.inclusive ? ">=" : ">"} ${lowerBound.value})`;
	else condition = `(${dimension})`;
	return [{
		subtype: "dimension",
		negated: negated ?? false,
		condition,
		dimension,
		lowerBound,
		upperBound
	}];
}
/**
* Convert container condition to parsed structure
* This enables structured analysis for contradiction detection and condition combining
*/
function containerToParsed(state) {
	let condition;
	if (state.subtype === "style") if (state.propertyValue) condition = `style(--${state.property}: ${state.propertyValue})`;
	else condition = `style(--${state.property})`;
	else if (state.subtype === "raw") condition = state.rawCondition;
	else condition = dimensionToContainerCondition(state.dimension || "width", state.lowerBound, state.upperBound);
	return {
		name: state.containerName,
		condition,
		negated: state.negated ?? false,
		subtype: state.subtype,
		property: state.property,
		propertyValue: state.propertyValue
	};
}
/**
* Convert dimension bounds to container query condition (single string)
* Container queries support "not (condition)", so no need to invert manually
*/
function dimensionToContainerCondition(dimension, lowerBound, upperBound) {
	if (lowerBound && upperBound) {
		const lowerOp = lowerBound.inclusive ? "<=" : "<";
		const upperOp = upperBound.inclusive ? "<=" : "<";
		return `(${lowerBound.value} ${lowerOp} ${dimension} ${upperOp} ${upperBound.value})`;
	} else if (upperBound) return `(${dimension} ${upperBound.inclusive ? "<=" : "<"} ${upperBound.value})`;
	else if (lowerBound) return `(${dimension} ${lowerBound.inclusive ? ">=" : ">"} ${lowerBound.value})`;
	return "(width)";
}
/**
* Convert supports condition to parsed structure
*/
function supportsToParsed(state) {
	return {
		subtype: state.subtype,
		condition: state.condition,
		negated: state.negated ?? false
	};
}
/**
* Convert root condition to parsed structure
*/
function rootToParsed(state) {
	return {
		selector: state.selector,
		negated: state.negated ?? false
	};
}
/**
* Convert parsed root conditions to CSS selector prefix (for final output)
*/
function rootConditionsToCSS(roots) {
	if (roots.length === 0) return void 0;
	let prefix = ":root";
	for (const root of roots) if (root.negated) prefix += `:not(${root.selector})`;
	else prefix += root.selector;
	return prefix;
}
/**
* Convert own condition to parsed structures for sub-element
*/
function ownToParsed(state) {
	const innerCSS = conditionToCSS(state.innerCondition);
	if (innerCSS.isImpossible || innerCSS.variants.length === 0) return [];
	const allConditions = [];
	for (const variant of innerCSS.variants) {
		for (const mod of variant.modifierConditions) allConditions.push({
			...mod,
			negated: state.negated ? !mod.negated : mod.negated
		});
		for (const pseudo of variant.pseudoConditions) allConditions.push({
			...pseudo,
			negated: state.negated ? !pseudo.negated : pseudo.negated
		});
	}
	return allConditions;
}
/**
* Get unique key for a modifier condition
*/
function getModifierKey(mod) {
	const base = mod.value ? `${mod.attribute}${mod.operator || "="}${mod.value}` : mod.attribute;
	return mod.negated ? `!${base}` : base;
}
/**
* Get unique key for a pseudo condition
*/
function getPseudoKey(pseudo) {
	return pseudo.negated ? `!${pseudo.pseudo}` : pseudo.pseudo;
}
/**
* Get unique key for a root condition
*/
function getRootKey(root) {
	return root.negated ? `!${root.selector}` : root.selector;
}
/**
* Deduplicate modifier conditions
*/
function dedupeModifierConditions(conditions) {
	const seen = /* @__PURE__ */ new Set();
	const result = [];
	for (const c of conditions) {
		const key = getModifierKey(c);
		if (!seen.has(key)) {
			seen.add(key);
			result.push(c);
		}
	}
	return result;
}
/**
* Deduplicate pseudo conditions
*/
function dedupePseudoConditions(conditions) {
	const seen = /* @__PURE__ */ new Set();
	const result = [];
	for (const c of conditions) {
		const key = getPseudoKey(c);
		if (!seen.has(key)) {
			seen.add(key);
			result.push(c);
		}
	}
	return result;
}
/**
* Deduplicate root conditions
*/
function dedupeRootConditions(conditions) {
	const seen = /* @__PURE__ */ new Set();
	const result = [];
	for (const c of conditions) {
		const key = getRootKey(c);
		if (!seen.has(key)) {
			seen.add(key);
			result.push(c);
		}
	}
	return result;
}
/**
* Deduplicate own conditions (modifiers or pseudos)
*/
function dedupeOwnConditions(conditions) {
	const seen = /* @__PURE__ */ new Set();
	const result = [];
	for (const c of conditions) {
		const key = "attribute" in c ? `mod:${getModifierKey(c)}` : `pseudo:${getPseudoKey(c)}`;
		if (!seen.has(key)) {
			seen.add(key);
			result.push(c);
		}
	}
	return result;
}
/**
* Check for modifier contradiction: same attribute with opposite negation
*/
function hasModifierContradiction(conditions) {
	const byKey = /* @__PURE__ */ new Map();
	for (const mod of conditions) {
		const baseKey = mod.value ? `${mod.attribute}${mod.operator || "="}${mod.value}` : mod.attribute;
		const existing = byKey.get(baseKey);
		if (existing !== void 0 && existing !== !mod.negated) return true;
		byKey.set(baseKey, !mod.negated);
	}
	return false;
}
/**
* Check for pseudo contradiction: same pseudo with opposite negation
*/
function hasPseudoContradiction(conditions) {
	const byKey = /* @__PURE__ */ new Map();
	for (const pseudo of conditions) {
		const existing = byKey.get(pseudo.pseudo);
		if (existing !== void 0 && existing !== !pseudo.negated) return true;
		byKey.set(pseudo.pseudo, !pseudo.negated);
	}
	return false;
}
/**
* Check for root condition contradiction: same selector with opposite negation
*/
function hasRootContradiction(conditions) {
	const byKey = /* @__PURE__ */ new Map();
	for (const root of conditions) {
		const existing = byKey.get(root.selector);
		if (existing !== void 0 && existing !== !root.negated) return true;
		byKey.set(root.selector, !root.negated);
	}
	return false;
}
/**
* Check for own condition contradiction
*/
function hasOwnConditionContradiction(conditions) {
	const modifiers = [];
	const pseudos = [];
	for (const c of conditions) if ("attribute" in c) modifiers.push(c);
	else pseudos.push(c);
	return hasModifierContradiction(modifiers) || hasPseudoContradiction(pseudos);
}
/**
* Merge two selector variants (AND operation)
* Deduplicates conditions and checks for contradictions
*/
function mergeVariants(a, b) {
	const mergedMedia = dedupeMediaConditions([...a.mediaConditions, ...b.mediaConditions]);
	if (hasMediaContradiction(mergedMedia)) return null;
	const mergedRoots = dedupeRootConditions([...a.rootConditions, ...b.rootConditions]);
	if (hasRootContradiction(mergedRoots)) return null;
	const mergedModifiers = dedupeModifierConditions([...a.modifierConditions, ...b.modifierConditions]);
	if (hasModifierContradiction(mergedModifiers)) return null;
	const mergedPseudos = dedupePseudoConditions([...a.pseudoConditions, ...b.pseudoConditions]);
	if (hasPseudoContradiction(mergedPseudos)) return null;
	const mergedOwn = dedupeOwnConditions([...a.ownConditions, ...b.ownConditions]);
	if (hasOwnConditionContradiction(mergedOwn)) return null;
	const mergedContainers = dedupeContainerConditions([...a.containerConditions, ...b.containerConditions]);
	if (hasContainerStyleContradiction(mergedContainers)) return null;
	const mergedSupports = dedupeSupportsConditions([...a.supportsConditions, ...b.supportsConditions]);
	if (hasSupportsContradiction(mergedSupports)) return null;
	return {
		modifierConditions: mergedModifiers,
		pseudoConditions: mergedPseudos,
		ownConditions: mergedOwn,
		mediaConditions: mergedMedia,
		containerConditions: mergedContainers,
		supportsConditions: mergedSupports,
		rootConditions: mergedRoots,
		startingStyle: a.startingStyle || b.startingStyle
	};
}
/**
* Deduplicate media conditions by their key (subtype + condition + negated)
*/
function dedupeMediaConditions(conditions) {
	const seen = /* @__PURE__ */ new Set();
	const result = [];
	for (const c of conditions) {
		const key = `${c.subtype}|${c.condition}|${c.negated}`;
		if (!seen.has(key)) {
			seen.add(key);
			result.push(c);
		}
	}
	return result;
}
/**
* Deduplicate container conditions by their key (name + condition + negated)
*/
function dedupeContainerConditions(conditions) {
	const seen = /* @__PURE__ */ new Set();
	const result = [];
	for (const c of conditions) {
		const key = `${c.name ?? ""}|${c.condition}|${c.negated}`;
		if (!seen.has(key)) {
			seen.add(key);
			result.push(c);
		}
	}
	return result;
}
/**
* Deduplicate supports conditions by their key (subtype + condition + negated)
*/
function dedupeSupportsConditions(conditions) {
	const seen = /* @__PURE__ */ new Set();
	const result = [];
	for (const c of conditions) {
		const key = `${c.subtype}|${c.condition}|${c.negated}`;
		if (!seen.has(key)) {
			seen.add(key);
			result.push(c);
		}
	}
	return result;
}
/**
* Check if supports conditions contain contradictions
* e.g., @supports(display: grid) AND NOT @supports(display: grid)
*/
function hasSupportsContradiction(conditions) {
	const conditionMap = /* @__PURE__ */ new Map();
	for (const cond of conditions) {
		const key = `${cond.subtype}|${cond.condition}`;
		const existing = conditionMap.get(key);
		if (existing !== void 0 && existing !== !cond.negated) return true;
		conditionMap.set(key, !cond.negated);
	}
	return false;
}
/**
* Check if a set of media conditions contains contradictions
* e.g., (prefers-color-scheme: light) AND NOT (prefers-color-scheme: light)
* or (width >= 900px) AND (width < 600px)
*
* Uses parsed media conditions for efficient analysis without regex parsing.
*/
function hasMediaContradiction(conditions) {
	const featureConditions = /* @__PURE__ */ new Map();
	const typeConditions = /* @__PURE__ */ new Map();
	const dimensionConditions = /* @__PURE__ */ new Map();
	const dimensionsByDim = /* @__PURE__ */ new Map();
	for (const cond of conditions) if (cond.subtype === "type") {
		const key = cond.mediaType || "all";
		const existing = typeConditions.get(key);
		if (existing !== void 0 && existing !== !cond.negated) return true;
		typeConditions.set(key, !cond.negated);
	} else if (cond.subtype === "feature") {
		const key = cond.condition;
		const existing = featureConditions.get(key);
		if (existing !== void 0 && existing !== !cond.negated) return true;
		featureConditions.set(key, !cond.negated);
	} else if (cond.subtype === "dimension") {
		const condKey = cond.condition;
		const existing = dimensionConditions.get(condKey);
		if (existing !== void 0 && existing !== !cond.negated) return true;
		dimensionConditions.set(condKey, !cond.negated);
		if (!cond.negated) {
			const dim = cond.dimension || "width";
			let bounds = dimensionsByDim.get(dim);
			if (!bounds) {
				bounds = {
					lowerBound: null,
					upperBound: null
				};
				dimensionsByDim.set(dim, bounds);
			}
			if (cond.lowerBound?.valueNumeric != null) {
				const value = cond.lowerBound.valueNumeric;
				if (bounds.lowerBound === null || value > bounds.lowerBound) bounds.lowerBound = value;
			}
			if (cond.upperBound?.valueNumeric != null) {
				const value = cond.upperBound.valueNumeric;
				if (bounds.upperBound === null || value < bounds.upperBound) bounds.upperBound = value;
			}
			if (bounds.lowerBound !== null && bounds.upperBound !== null && bounds.lowerBound >= bounds.upperBound) return true;
		}
	}
	return false;
}
/**
* Check if container conditions contain contradictions in style queries
* e.g., style(--variant: danger) and style(--variant: success) together
* Same property with different values = always false
*
* Uses parsed container conditions for efficient analysis without regex parsing.
*/
function hasContainerStyleContradiction(conditions) {
	const styleQueries = /* @__PURE__ */ new Map();
	for (const cond of conditions) {
		if (cond.subtype !== "style" || !cond.property) continue;
		const property = cond.property;
		const value = cond.propertyValue;
		if (!styleQueries.has(property)) styleQueries.set(property, {
			hasExistence: false,
			values: /* @__PURE__ */ new Set(),
			hasNegatedExistence: false
		});
		const entry = styleQueries.get(property);
		if (cond.negated) {
			if (value === void 0) entry.hasNegatedExistence = true;
		} else if (value === void 0) entry.hasExistence = true;
		else entry.values.add(value);
	}
	for (const [, entry] of styleQueries) {
		if (entry.hasExistence && entry.hasNegatedExistence) return true;
		if (entry.values.size > 1) return true;
		if (entry.hasNegatedExistence && entry.values.size > 0) return true;
	}
	return false;
}
/**
* Get a unique key for a variant (for deduplication)
*/
function getVariantKey(v) {
	const modifierKey = v.modifierConditions.map(getModifierKey).sort().join("|");
	const pseudoKey = v.pseudoConditions.map(getPseudoKey).sort().join("|");
	const ownKey = v.ownConditions.map((c) => "attribute" in c ? `mod:${getModifierKey(c)}` : `pseudo:${getPseudoKey(c)}`).sort().join("|");
	const containerKey = v.containerConditions.map((c) => `${c.name ?? ""}:${c.negated ? "!" : ""}${c.condition}`).sort().join("|");
	return [
		modifierKey,
		pseudoKey,
		ownKey,
		v.mediaConditions.map((c) => `${c.subtype}:${c.negated ? "!" : ""}${c.condition}`).sort().join("|"),
		containerKey,
		v.supportsConditions.map((c) => `${c.subtype}:${c.negated ? "!" : ""}${c.condition}`).sort().join("|"),
		v.rootConditions.map(getRootKey).sort().join("|"),
		v.startingStyle ? "1" : "0"
	].join("###");
}
/**
* Check if variant A is a superset of variant B (A is more restrictive)
*
* If A has all of B's conditions plus more, then A is redundant
* because B already covers the same cases (and more).
*
* Example:
*   A: :not([size=large]):not([size=medium]):not([size=small])
*   B: :not([size=large])
*   A is a superset of B, so A is redundant when B exists.
*/
function isVariantSuperset(a, b) {
	if (a.startingStyle !== b.startingStyle) return false;
	if (!isRootConditionsSuperset(a.rootConditions, b.rootConditions)) return false;
	if (!isMediaConditionsSuperset(a.mediaConditions, b.mediaConditions)) return false;
	if (!isContainerConditionsSuperset(a.containerConditions, b.containerConditions)) return false;
	if (!isSupportsConditionsSuperset(a.supportsConditions, b.supportsConditions)) return false;
	if (!isModifierConditionsSuperset(a.modifierConditions, b.modifierConditions)) return false;
	if (!isPseudoConditionsSuperset(a.pseudoConditions, b.pseudoConditions)) return false;
	if (!isOwnConditionsSuperset(a.ownConditions, b.ownConditions)) return false;
	return a.mediaConditions.length + a.containerConditions.length + a.supportsConditions.length + a.modifierConditions.length + a.pseudoConditions.length + a.rootConditions.length + a.ownConditions.length > b.mediaConditions.length + b.containerConditions.length + b.supportsConditions.length + b.modifierConditions.length + b.pseudoConditions.length + b.rootConditions.length + b.ownConditions.length;
}
/**
* Check if media conditions A is a superset of B
*/
function isMediaConditionsSuperset(a, b) {
	const aKeys = new Set(a.map((c) => `${c.subtype}|${c.condition}|${c.negated}`));
	for (const c of b) {
		const key = `${c.subtype}|${c.condition}|${c.negated}`;
		if (!aKeys.has(key)) return false;
	}
	return true;
}
/**
* Check if container conditions A is a superset of B
*/
function isContainerConditionsSuperset(a, b) {
	const aKeys = new Set(a.map((c) => `${c.name ?? ""}|${c.condition}|${c.negated}`));
	for (const c of b) {
		const key = `${c.name ?? ""}|${c.condition}|${c.negated}`;
		if (!aKeys.has(key)) return false;
	}
	return true;
}
/**
* Check if supports conditions A is a superset of B
*/
function isSupportsConditionsSuperset(a, b) {
	const aKeys = new Set(a.map((c) => `${c.subtype}|${c.condition}|${c.negated}`));
	for (const c of b) {
		const key = `${c.subtype}|${c.condition}|${c.negated}`;
		if (!aKeys.has(key)) return false;
	}
	return true;
}
/**
* Check if modifier conditions A is a superset of B
*/
function isModifierConditionsSuperset(a, b) {
	const aKeys = new Set(a.map(getModifierKey));
	for (const c of b) if (!aKeys.has(getModifierKey(c))) return false;
	return true;
}
/**
* Check if pseudo conditions A is a superset of B
*/
function isPseudoConditionsSuperset(a, b) {
	const aKeys = new Set(a.map(getPseudoKey));
	for (const c of b) if (!aKeys.has(getPseudoKey(c))) return false;
	return true;
}
/**
* Check if root conditions A is a superset of B
*/
function isRootConditionsSuperset(a, b) {
	const aKeys = new Set(a.map(getRootKey));
	for (const c of b) if (!aKeys.has(getRootKey(c))) return false;
	return true;
}
/**
* Check if own conditions A is a superset of B
*/
function isOwnConditionsSuperset(a, b) {
	const aKeys = new Set(a.map((c) => "attribute" in c ? `mod:${getModifierKey(c)}` : `pseudo:${getPseudoKey(c)}`));
	for (const c of b) {
		const key = "attribute" in c ? `mod:${getModifierKey(c)}` : `pseudo:${getPseudoKey(c)}`;
		if (!aKeys.has(key)) return false;
	}
	return true;
}
/**
* Deduplicate variants
*
* Removes:
* 1. Exact duplicates (same key)
* 2. Superset variants (more restrictive selectors that are redundant)
*/
function dedupeVariants(variants) {
	const seen = /* @__PURE__ */ new Set();
	let result = [];
	for (const v of variants) {
		const key = getVariantKey(v);
		if (!seen.has(key)) {
			seen.add(key);
			result.push(v);
		}
	}
	result.sort((a, b) => {
		return a.modifierConditions.length + a.pseudoConditions.length + a.ownConditions.length + a.mediaConditions.length + a.containerConditions.length + a.supportsConditions.length + a.rootConditions.length - (b.modifierConditions.length + b.pseudoConditions.length + b.ownConditions.length + b.mediaConditions.length + b.containerConditions.length + b.supportsConditions.length + b.rootConditions.length);
	});
	const filtered = [];
	for (const candidate of result) {
		let isRedundant = false;
		for (const kept of filtered) if (isVariantSuperset(candidate, kept)) {
			isRedundant = true;
			break;
		}
		if (!isRedundant) filtered.push(candidate);
	}
	return filtered;
}
/**
* Combine AND conditions into CSS
*
* AND of conditions means cartesian product of variants:
* (A1 | A2) & (B1 | B2) = A1&B1 | A1&B2 | A2&B1 | A2&B2
*
* Variants that result in contradictions (e.g., conflicting media rules)
* are filtered out.
*/
function andToCSS(children) {
	let currentVariants = [emptyVariant()];
	for (const child of children) {
		const childCSS = conditionToCSSInner(child);
		if (childCSS.isImpossible || childCSS.variants.length === 0) return {
			variants: [],
			isImpossible: true
		};
		const newVariants = [];
		for (const current of currentVariants) for (const childVariant of childCSS.variants) {
			const merged = mergeVariants(current, childVariant);
			if (merged !== null) newVariants.push(merged);
		}
		if (newVariants.length === 0) return {
			variants: [],
			isImpossible: true
		};
		currentVariants = dedupeVariants(newVariants);
	}
	return {
		variants: currentVariants,
		isImpossible: false
	};
}
/**
* Combine OR conditions into CSS
*
* OR in CSS means multiple selector variants (DNF).
* Each variant becomes a separate selector in the comma-separated list,
* or multiple CSS rules if they have different at-rules.
*
* Note: OR exclusivity is handled at the pipeline level (expandOrConditions),
* so here we just collect all variants. Any remaining ORs in the condition
* tree (e.g., from De Morgan expansion) are handled as simple alternatives.
*/
function orToCSS(children) {
	const allVariants = [];
	for (const child of children) {
		const childCSS = conditionToCSSInner(child);
		if (childCSS.isImpossible) continue;
		allVariants.push(...childCSS.variants);
	}
	if (allVariants.length === 0) return {
		variants: [],
		isImpossible: true
	};
	return {
		variants: dedupeVariants(allVariants),
		isImpossible: false
	};
}
/**
* Get a cache key for a condition
*/
function getConditionKey(node) {
	if (node.kind === "true") return "TRUE";
	if (node.kind === "false") return "FALSE";
	if (node.kind === "state") return node.uniqueId;
	if (node.kind === "compound") {
		const childKeys = node.children.map(getConditionKey).sort();
		return `${node.operator}(${childKeys.join(",")})`;
	}
	return "UNKNOWN";
}
/**
* Build at-rules array from a variant
*/
function buildAtRulesFromVariant(variant) {
	const atRules = [];
	if (variant.mediaConditions.length > 0) {
		const conditionParts = variant.mediaConditions.map((c) => {
			if (c.subtype === "type") return c.negated ? `not ${c.condition}` : c.condition;
			else return c.negated ? `(not ${c.condition})` : c.condition;
		});
		atRules.push(`@media ${conditionParts.join(" and ")}`);
	}
	if (variant.containerConditions.length > 0) {
		const byName = /* @__PURE__ */ new Map();
		for (const cond of variant.containerConditions) {
			const group = byName.get(cond.name) || [];
			group.push(cond);
			byName.set(cond.name, group);
		}
		for (const [name, conditions] of byName) {
			const conditionParts = conditions.map((c) => c.negated ? `(not ${c.condition})` : c.condition);
			const namePrefix = name ? `${name} ` : "";
			atRules.push(`@container ${namePrefix}${conditionParts.join(" and ")}`);
		}
	}
	if (variant.supportsConditions.length > 0) {
		const conditionParts = variant.supportsConditions.map((c) => {
			if (c.subtype === "selector") {
				const selectorCond = `selector(${c.condition})`;
				return c.negated ? `(not ${selectorCond})` : selectorCond;
			} else {
				const featureCond = `(${c.condition})`;
				return c.negated ? `(not ${featureCond})` : featureCond;
			}
		});
		atRules.push(`@supports ${conditionParts.join(" and ")}`);
	}
	if (variant.startingStyle) atRules.push("@starting-style");
	return atRules;
}

//#endregion
export { buildAtRulesFromVariant, conditionToCSS, modifierToCSS, pseudoToCSS, rootConditionsToCSS };
//# sourceMappingURL=materialize.js.map