/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-b859c78 | Cube Dev Team */
import { getRgbValuesFromRgbaString, strToRgb } from "../utils/styles.js";

//#region src/tasty/properties/index.ts
const PROPERTIES_KEY = "@properties";
/**
* Valid CSS custom property name pattern (after the -- prefix).
* Must start with a letter or underscore, followed by letters, digits, hyphens, or underscores.
*/
const VALID_PROPERTY_NAME_PATTERN = /^[a-z_][a-z0-9-_]*$/i;
/**
* Validate a CSS custom property name (the part after --).
* Returns true if the name is valid for use as a CSS custom property.
*/
function isValidPropertyName(name) {
	return VALID_PROPERTY_NAME_PATTERN.test(name);
}
/**
* Check if styles object has local @properties definition.
* Fast path: single property lookup.
*/
function hasLocalProperties(styles) {
	return PROPERTIES_KEY in styles;
}
/**
* Extract local @properties from styles object.
* Returns null if no local properties (fast path).
*/
function extractLocalProperties(styles) {
	const properties = styles[PROPERTIES_KEY];
	if (!properties || typeof properties !== "object") return null;
	return properties;
}
/**
* Parse a property token name and return the CSS property name and whether it's a color.
* Supports tasty token syntax and validates the property name.
*
* Token formats:
* - `$name` → { cssName: '--name', isColor: false }
* - `#name` → { cssName: '--name-color', isColor: true }
* - `--name` → { cssName: '--name', isColor: false } (legacy, auto-detect color by suffix)
* - `name` → { cssName: '--name', isColor: false } (legacy)
*
* @param token - The property token to parse
* @returns Parsed result with cssName, isColor, isValid, and optional error
*/
function parsePropertyToken(token) {
	if (!token || typeof token !== "string") return {
		cssName: "",
		isColor: false,
		isValid: false,
		error: "Property token must be a non-empty string"
	};
	let name;
	let isColor;
	if (token.startsWith("$")) {
		name = token.slice(1);
		isColor = false;
	} else if (token.startsWith("#")) {
		name = token.slice(1);
		isColor = true;
	} else if (token.startsWith("--")) {
		name = token.slice(2);
		isColor = token.endsWith("-color");
	} else {
		name = token;
		isColor = token.endsWith("-color");
	}
	if (!name) return {
		cssName: "",
		isColor,
		isValid: false,
		error: "Property name cannot be empty"
	};
	if (!isValidPropertyName(name)) return {
		cssName: "",
		isColor,
		isValid: false,
		error: `Invalid property name "${name}". Must start with a letter or underscore, followed by letters, digits, hyphens, or underscores.`
	};
	let cssName;
	if (token.startsWith("#")) cssName = `--${name}-color`;
	else cssName = `--${name}`;
	return {
		cssName,
		isColor,
		isValid: true
	};
}
/**
* Normalize a property definition to a consistent string representation.
* Used for comparing definitions to detect changes/conflicts.
*
* Keys are sorted alphabetically to ensure consistent comparison:
* { inherits: true, syntax: '<color>' } === { syntax: '<color>', inherits: true }
*/
function normalizePropertyDefinition(def) {
	const normalized = {};
	if (def.inherits !== void 0) normalized.inherits = def.inherits;
	if (def.initialValue !== void 0) normalized.initialValue = String(def.initialValue);
	if (def.syntax !== void 0) normalized.syntax = def.syntax;
	return JSON.stringify(normalized);
}
/**
* Get the effective property definition for a token.
* For color tokens (#name), auto-sets syntax to '<color>' and defaults initialValue to 'transparent'.
*
* @param token - Property token ($name, #name, --name, or plain name)
* @param userDefinition - User-provided definition options
* @returns Effective definition with cssName, definition, isValid, and optional error
*/
function getEffectiveDefinition(token, userDefinition) {
	const parsed = parsePropertyToken(token);
	if (!parsed.isValid) return {
		cssName: "",
		definition: userDefinition,
		isColor: false,
		isValid: false,
		error: parsed.error
	};
	if (parsed.isColor) return {
		cssName: parsed.cssName,
		definition: {
			syntax: "<color>",
			inherits: userDefinition.inherits,
			initialValue: userDefinition.initialValue ?? "transparent"
		},
		isColor: true,
		isValid: true
	};
	return {
		cssName: parsed.cssName,
		definition: userDefinition,
		isColor: false,
		isValid: true
	};
}
/**
* Extract RGB triplet string from a color initial value.
* Used when auto-creating the companion `-rgb` property for color @property definitions.
*
* @param initialValue - The color property's initial value (e.g., 'rgb(255 255 255)', 'transparent', '#fff')
* @returns Space-separated RGB triplet (e.g., '255 255 255'), defaults to '0 0 0'
*/
function colorInitialValueToRgb(initialValue) {
	if (initialValue == null) return "0 0 0";
	const str = String(initialValue).trim();
	if (!str || str === "transparent") return "0 0 0";
	const rgba = strToRgb(str);
	if (rgba) {
		const values = getRgbValuesFromRgbaString(rgba);
		if (values.length >= 3) return values.slice(0, 3).join(" ");
	}
	return "0 0 0";
}

//#endregion
export { colorInitialValueToRgb, extractLocalProperties, getEffectiveDefinition, hasLocalProperties, normalizePropertyDefinition };
//# sourceMappingURL=index.js.map