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

//#region src/tasty/styles/createStyle.ts
const CACHE = {};
/**
* Convert color fallback chain to RGB fallback chain.
* Example: var(--primary-color, var(--secondary-color)) → var(--primary-color-rgb, var(--secondary-color-rgb))
*/
function convertColorChainToRgbChain(colorValue) {
	const rgbVarMatch = colorValue.match(/^rgba?\(\s*(var\(--[a-z0-9-]+-color-rgb\))\s*\//);
	if (rgbVarMatch) return rgbVarMatch[1];
	const match = colorValue.match(/var\(--([a-z0-9-]+)-color\s*(?:,\s*(.+))?\)/);
	if (!match) {
		if (colorValue.startsWith("rgb(") || colorValue.startsWith("rgba(")) return colorValue;
		const rgba = strToRgb(colorValue);
		if (rgba) return getRgbValuesFromRgbaString(rgba).join(" ");
		return colorValue;
	}
	const [, name, fallback] = match;
	if (!fallback) return `var(--${name}-color-rgb)`;
	return `var(--${name}-color-rgb, ${convertColorChainToRgbChain(fallback.trim())})`;
}
function createStyle(styleName, cssStyle, converter) {
	const key = `${styleName}.${cssStyle ?? ""}`;
	if (!CACHE[key]) {
		const styleHandler = (styleMap) => {
			let styleValue = styleMap[styleName];
			if (styleValue == null || styleValue === false) return;
			let finalCssStyle;
			const isColorToken = !cssStyle && typeof styleName === "string" && styleName.startsWith("#");
			if (isColorToken) finalCssStyle = `--${toSnakeCase(styleName.slice(1)).replace(/^-+/, "")}-color`;
			else finalCssStyle = cssStyle || toSnakeCase(styleName).replace(/^\$/, "--");
			if (isColorToken) {
				const normalized = normalizeColorTokenValue(styleValue);
				if (normalized === null) return;
				styleValue = normalized;
			}
			if (converter && typeof styleValue !== "string") {
				styleValue = converter(styleValue);
				if (!styleValue) return;
			}
			if (typeof styleValue === "string" && finalCssStyle.startsWith("--") && finalCssStyle.endsWith("-color")) {
				styleValue = styleValue.trim();
				const rgba = strToRgb(styleValue);
				const { color, name } = parseColor(styleValue);
				if (name && rgba) return {
					[finalCssStyle]: `var(--${name}-color, ${rgba})`,
					[`${finalCssStyle}-rgb`]: `var(--${name}-color-rgb, ${getRgbValuesFromRgbaString(rgba).join(" ")})`
				};
				else if (name) {
					if (color) return {
						[finalCssStyle]: color,
						[`${finalCssStyle}-rgb`]: convertColorChainToRgbChain(color)
					};
					return {
						[finalCssStyle]: `var(--${name}-color)`,
						[`${finalCssStyle}-rgb`]: `var(--${name}-color-rgb)`
					};
				} else if (rgba) return {
					[finalCssStyle]: rgba,
					[`${finalCssStyle}-rgb`]: getRgbValuesFromRgbaString(rgba).join(" ")
				};
				return { [finalCssStyle]: color };
			}
			const processed = parseStyle(styleValue);
			return { [finalCssStyle]: processed.output };
		};
		styleHandler.__lookupStyles = [styleName];
		CACHE[key] = styleHandler;
	}
	return CACHE[key];
}

//#endregion
export { convertColorChainToRgbChain, createStyle };
//# sourceMappingURL=createStyle.js.map