/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { getFeatureFlags } from "../flags.js";
import { falsyToString } from "../utils/falsyToString.js";
import { variants } from "./variants.js";
import { backgroundColors, borderRadii, borderWidthsWithElevation, foregroundColors, lineColors, spectrumColors, textVariants } from "@yahoo/uds/fixtures";
import { clsx } from "clsx";
import { extendTailwindMerge } from "tailwind-merge";
//#region src/styles/styler.ts
const createObjectWithoutPrototype = () => Object.create(null);
const { useGetStylesCache, skipTailwindMerge } = getFeatureFlags();
/**
* Prepare and object for memoization
* Code uses old-school JS to hyper optimize component rendering hot paths.
**/
function normalizeObject(props) {
	const sorted = createObjectWithoutPrototype();
	const sortedKeys = Object.keys(props).sort();
	for (let i = 0, len = sortedKeys.length; i < len; i++) {
		const key = sortedKeys[i];
		if (props[key] !== void 0) sorted[key] = props[key];
	}
	return sorted;
}
const twMerge = extendTailwindMerge({
	extend: { theme: {
		borderColor: lineColors,
		borderWidth: borderWidthsWithElevation,
		borderRadius: borderRadii
	} },
	override: {
		classGroups: {
			"text-color": [{ text: [...foregroundColors, ...spectrumColors] }],
			"bg-color": [{ bg: backgroundColors }],
			"font-family": [{ font: ["icons", ...textVariants] }],
			leading: [{ leading: textVariants }]
		},
		conflictingClassGroups: {}
	}
});
const cx = (...inputs) => {
	const finalClassName = clsx(inputs);
	return skipTailwindMerge ? finalClassName : twMerge(finalClassName);
};
const cva = (config) => {
	const { base, variants, defaultVariants, compoundVariants } = config;
	/**
	* !WARNING
	* This is a VERY performance-sensitive function. Do not change it
	* without benchmarking first.
	*/
	return (maybeProps) => {
		if (!maybeProps) return cx(base);
		const { className, ...props } = maybeProps;
		if (!variants) return cx(base, className);
		const variantClassNames = [];
		for (const prop in defaultVariants) {
			if (prop === void 0) continue;
			if (props[prop] !== void 0) continue;
			const defaultPropValue = defaultVariants[prop];
			const variantKey = falsyToString(defaultPropValue);
			variantClassNames.push(variants[prop][variantKey]);
		}
		for (const prop in props) {
			if (prop === void 0) continue;
			const propValue = props[prop];
			const variantKey = falsyToString(propValue);
			variantClassNames.push(variants[prop][variantKey]);
		}
		if (!compoundVariants) return cx(base, variantClassNames, className);
		let compoundVariantClassNames = "";
		for (const compoundConfig of compoundVariants) {
			const { className: compoundClassname, ...compoundVariantConfig } = compoundConfig;
			let allMatch = true;
			for (const cvKey in compoundVariantConfig) {
				const cvValue = compoundVariantConfig[cvKey];
				if (!((props[cvKey] ?? defaultVariants?.[cvKey]) === cvValue)) {
					allMatch = false;
					break;
				}
			}
			if (allMatch) compoundVariantClassNames = cx(compoundVariantClassNames, compoundClassname);
		}
		return cx(base, variantClassNames, compoundVariantClassNames, className);
	};
};
const getStylesInternal = cva({ variants });
const styleCache = /* @__PURE__ */ new Map();
const getStylesCacheKeySymbol = Symbol("getStylesCacheKey");
const generateCacheKey = (props) => {
	if (props[getStylesCacheKeySymbol]) return props[getStylesCacheKeySymbol];
	const sorted = normalizeObject(props);
	let cacheKey = "";
	for (const key in sorted) cacheKey += `${key}:${sorted[key]}|`;
	props[getStylesCacheKeySymbol] = cacheKey;
	return cacheKey;
};
const cachedGetStyles = (props, onHit) => {
	if (!useGetStylesCache) return getStylesInternal(props);
	const cacheKey = generateCacheKey(props);
	if (styleCache.has(cacheKey)) {
		onHit?.();
		return styleCache.get(cacheKey);
	}
	const styles = getStylesInternal(props);
	styleCache.set(cacheKey, styles);
	return styles;
};
const getStyles = cachedGetStyles;
//#endregion
export { cva, cx, getStyles, getStylesCacheKeySymbol, normalizeObject };
