/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-1fc5ad1 | Cube Dev Team */
//#region src/tasty/pipeline/conditions.ts
/**
* Create a TRUE condition (matches everything)
*/
function trueCondition() {
	return { kind: "true" };
}
/**
* Create a FALSE condition (matches nothing)
*/
function falseCondition() {
	return { kind: "false" };
}
/**
* Create an AND compound condition
*/
function and(...children) {
	const filtered = [];
	for (const child of children) {
		if (child.kind === "false") return falseCondition();
		if (child.kind === "true") continue;
		if (child.kind === "compound" && child.operator === "AND") filtered.push(...child.children);
		else filtered.push(child);
	}
	if (filtered.length === 0) return trueCondition();
	if (filtered.length === 1) return filtered[0];
	return {
		kind: "compound",
		operator: "AND",
		children: filtered
	};
}
/**
* Create an OR compound condition
*/
function or(...children) {
	const filtered = [];
	for (const child of children) {
		if (child.kind === "true") return trueCondition();
		if (child.kind === "false") continue;
		if (child.kind === "compound" && child.operator === "OR") filtered.push(...child.children);
		else filtered.push(child);
	}
	if (filtered.length === 0) return falseCondition();
	if (filtered.length === 1) return filtered[0];
	return {
		kind: "compound",
		operator: "OR",
		children: filtered
	};
}
/**
* Negate a condition
*/
function not(node) {
	if (node.kind === "true") return falseCondition();
	if (node.kind === "false") return trueCondition();
	if (node.kind === "state") return {
		...node,
		negated: !node.negated,
		uniqueId: node.negated ? node.uniqueId.replace(/^!/, "") : `!${node.uniqueId}`
	};
	if (node.kind === "compound" && node.operator === "AND") return or(...node.children.map((c) => not(c)));
	if (node.kind === "compound" && node.operator === "OR") return and(...node.children.map((c) => not(c)));
	return node;
}
/**
* Check if a condition is a compound condition
*/
function isCompoundCondition(node) {
	return node.kind === "compound";
}
/**
* Generate a normalized unique ID for a modifier condition
*/
function modifierUniqueId(attribute, value, operator = "=", negated = false) {
	const base = value ? `mod:${attribute}${operator}${value}` : `mod:${attribute}`;
	return negated ? `!${base}` : base;
}
/**
* Generate a normalized unique ID for a pseudo condition
*/
function pseudoUniqueId(pseudo, negated = false) {
	const base = `pseudo:${pseudo}`;
	return negated ? `!${base}` : base;
}
/**
* Generate a normalized unique ID for a media condition
*/
function mediaUniqueId(subtype, props, negated = false) {
	let base;
	if (subtype === "dimension") {
		const parts = [
			"media",
			"dim",
			props.dimension
		];
		if (props.lowerBound) {
			parts.push(props.lowerBound.inclusive ? ">=" : ">");
			parts.push(props.lowerBound.value);
		}
		if (props.upperBound) {
			parts.push(props.upperBound.inclusive ? "<=" : "<");
			parts.push(props.upperBound.value);
		}
		base = parts.join(":");
	} else if (subtype === "feature") base = props.featureValue ? `media:feat:${props.feature}:${props.featureValue}` : `media:feat:${props.feature}`;
	else base = `media:type:${props.mediaType}`;
	return negated ? `!${base}` : base;
}
/**
* Generate a normalized unique ID for a container condition
*/
function containerUniqueId(subtype, props, negated = false) {
	let base;
	const name = props.containerName || "_";
	if (subtype === "dimension") {
		const parts = [
			"container",
			"dim",
			name,
			props.dimension
		];
		if (props.lowerBound) {
			parts.push(props.lowerBound.inclusive ? ">=" : ">");
			parts.push(props.lowerBound.value);
		}
		if (props.upperBound) {
			parts.push(props.upperBound.inclusive ? "<=" : "<");
			parts.push(props.upperBound.value);
		}
		base = parts.join(":");
	} else if (subtype === "raw") base = `container:raw:${name}:${props.rawCondition}`;
	else base = props.propertyValue ? `container:style:${name}:--${props.property}:${props.propertyValue}` : `container:style:${name}:--${props.property}`;
	return negated ? `!${base}` : base;
}
/**
* Generate a normalized unique ID for a root condition
*/
function rootUniqueId(selector, negated = false) {
	const base = `root:${selector}`;
	return negated ? `!${base}` : base;
}
/**
* Generate a normalized unique ID for an own condition
*/
function ownUniqueId(innerUniqueId, negated = false) {
	const base = `own:${innerUniqueId}`;
	return negated ? `!${base}` : base;
}
/**
* Generate a normalized unique ID for a starting condition
*/
function startingUniqueId(negated = false) {
	return negated ? "!starting" : "starting";
}
/**
* Generate a normalized unique ID for a supports condition
*/
function supportsUniqueId(subtype, condition, negated = false) {
	const base = `supports:${subtype}:${condition}`;
	return negated ? `!${base}` : base;
}
/**
* Create a modifier condition
*/
function createModifierCondition(attribute, value, operator = "=", negated = false, raw) {
	return {
		kind: "state",
		type: "modifier",
		negated,
		raw: raw || (value ? `${attribute}${operator}${value}` : attribute),
		uniqueId: modifierUniqueId(attribute, value, operator, negated),
		attribute,
		value,
		operator: value ? operator : void 0
	};
}
/**
* Create a pseudo condition
*/
function createPseudoCondition(pseudo, negated = false, raw) {
	return {
		kind: "state",
		type: "pseudo",
		negated,
		raw: raw || pseudo,
		uniqueId: pseudoUniqueId(pseudo, negated),
		pseudo
	};
}
/**
* Create a media dimension condition
*/
function createMediaDimensionCondition(dimension, lowerBound, upperBound, negated = false, raw) {
	return {
		kind: "state",
		type: "media",
		subtype: "dimension",
		negated,
		raw: raw || `@media(${dimension})`,
		uniqueId: mediaUniqueId("dimension", {
			dimension,
			lowerBound,
			upperBound
		}, negated),
		dimension,
		lowerBound,
		upperBound
	};
}
/**
* Create a media feature condition
*/
function createMediaFeatureCondition(feature, featureValue, negated = false, raw) {
	return {
		kind: "state",
		type: "media",
		subtype: "feature",
		negated,
		raw: raw || `@media(${feature}${featureValue ? `: ${featureValue}` : ""})`,
		uniqueId: mediaUniqueId("feature", {
			feature,
			featureValue
		}, negated),
		feature,
		featureValue
	};
}
/**
* Create a media type condition
*/
function createMediaTypeCondition(mediaType, negated = false, raw) {
	return {
		kind: "state",
		type: "media",
		subtype: "type",
		negated,
		raw: raw || `@media:${mediaType}`,
		uniqueId: mediaUniqueId("type", { mediaType }, negated),
		mediaType
	};
}
/**
* Create a container dimension condition
*/
function createContainerDimensionCondition(dimension, lowerBound, upperBound, containerName, negated = false, raw) {
	return {
		kind: "state",
		type: "container",
		subtype: "dimension",
		negated,
		raw: raw || `@(${containerName ? containerName + ", " : ""}${dimension})`,
		uniqueId: containerUniqueId("dimension", {
			containerName,
			dimension,
			lowerBound,
			upperBound
		}, negated),
		containerName,
		dimension,
		lowerBound,
		upperBound
	};
}
/**
* Create a container style condition
*/
function createContainerStyleCondition(property, propertyValue, containerName, negated = false, raw) {
	return {
		kind: "state",
		type: "container",
		subtype: "style",
		negated,
		raw: raw || `@(${containerName ? containerName + ", " : ""}$${property}${propertyValue ? "=" + propertyValue : ""})`,
		uniqueId: containerUniqueId("style", {
			containerName,
			property,
			propertyValue
		}, negated),
		containerName,
		property,
		propertyValue
	};
}
/**
* Create a container raw function condition (e.g., scroll-state(), style(), etc.)
* The condition string is passed through to CSS verbatim.
*/
function createContainerRawCondition(rawCondition, containerName, negated = false, raw) {
	return {
		kind: "state",
		type: "container",
		subtype: "raw",
		negated,
		raw: raw || `@(${containerName ? containerName + ", " : ""}${rawCondition})`,
		uniqueId: containerUniqueId("raw", {
			containerName,
			rawCondition
		}, negated),
		containerName,
		rawCondition
	};
}
/**
* Create a root condition
*/
function createRootCondition(selector, negated = false, raw) {
	return {
		kind: "state",
		type: "root",
		negated,
		raw: raw || `@root(${selector})`,
		uniqueId: rootUniqueId(selector, negated),
		selector
	};
}
/**
* Create an own condition
*/
function createOwnCondition(innerCondition, negated = false, raw) {
	const innerUniqueId = getConditionUniqueId(innerCondition);
	return {
		kind: "state",
		type: "own",
		negated,
		raw: raw || `@own(...)`,
		uniqueId: ownUniqueId(innerUniqueId, negated),
		innerCondition
	};
}
/**
* Create a starting condition
*/
function createStartingCondition(negated = false, raw) {
	return {
		kind: "state",
		type: "starting",
		negated,
		raw: raw || "@starting",
		uniqueId: startingUniqueId(negated)
	};
}
/**
* Create a supports condition
*
* @param subtype 'feature' for @supports(display: grid), 'selector' for @supports($, :has(*))
* @param condition The condition string (e.g., "display: grid" or ":has(*)")
*/
function createSupportsCondition(subtype, condition, negated = false, raw) {
	return {
		kind: "state",
		type: "supports",
		subtype,
		negated,
		raw: raw || `@supports(${condition})`,
		uniqueId: supportsUniqueId(subtype, condition, negated),
		condition
	};
}
/**
* Get the unique ID for any condition node
*/
function getConditionUniqueId(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 childIds = node.children.map(getConditionUniqueId).sort();
		return `${node.operator}(${childIds.join(",")})`;
	}
	return "UNKNOWN";
}

//#endregion
export { and, createContainerDimensionCondition, createContainerRawCondition, createContainerStyleCondition, createMediaDimensionCondition, createMediaFeatureCondition, createMediaTypeCondition, createModifierCondition, createOwnCondition, createPseudoCondition, createRootCondition, createStartingCondition, createSupportsCondition, falseCondition, getConditionUniqueId, isCompoundCondition, not, or, trueCondition };
//# sourceMappingURL=conditions.js.map