/*! © 2026 Yahoo, Inc. UDS Tailwind and Purger v0.0.0-development */
import { alwaysPalette } from "../palette/dist/index.js";
import { AVATAR_SIZE_PREFIX, BACKGROUND_BLUR_FALLBACK_COLOR_PREFIX, BACKGROUND_BLUR_RADIUS_PREFIX, BACKGROUND_COLOR_PREFIX, BORDER_RADIUS_PREFIX, BORDER_WIDTH_PREFIX, DROP_SHADOW_PREFIX, ICON_SIZE_PREFIX, LINE_COLOR_PREFIX, OUTLINE_PREFIX, SPECTRUM_COLOR_PREFIX, UDS_PREFIX, getShadowLayerValue } from "../css-tokens/dist/index.js";
import { entries } from "./entries.js";
import { mapValues } from "./mapValues.js";
import { parseOpacityStep } from "./opacity.js";

//#region src/utils/parseTokens.ts
/** Converts a SpectrumValueEntry to a space-separated RGB string for CSS variables. */
function spectrumValueToRgbString(value) {
	const { r, g, b } = value.rgb;
	return `${r} ${g} ${b}`;
}
function shouldIncludeOpacity(opacity) {
	if (!opacity) return false;
	const parsedOpacity = parseInt(opacity, 10);
	return !isNaN(parsedOpacity) && parsedOpacity !== 100;
}
function resolveFocusRingColor(color) {
	switch (color.type) {
		case "spectrum": return `rgb(var(--${SPECTRUM_COLOR_PREFIX}-${color.value.hue}-${color.value.step}))`;
		case "line": return `rgb(var(--${LINE_COLOR_PREFIX}-${color.value}))`;
		case "always": return alwaysPalette[color.value];
		default: return `rgb(var(--${SPECTRUM_COLOR_PREFIX}-gray-15))`;
	}
}
function transformColors(colors, elevationPresets, focusRingConfig) {
	const { palette, spectrum } = colors;
	const rootVars = {};
	const tailwindConfig = {
		spectrum: {},
		palette: {}
	};
	for (const [hue, hueSteps] of entries(spectrum)) for (const [hueStep, value] of entries(hueSteps)) {
		const varKey = `--${SPECTRUM_COLOR_PREFIX}-${`${hue}-${hueStep}`}`;
		if (!tailwindConfig.spectrum[hue]) tailwindConfig.spectrum[hue] = {};
		tailwindConfig.spectrum[hue][hueStep] = `rgb(var(${varKey}))`;
		rootVars[varKey] = spectrumValueToRgbString(value);
	}
	entries(palette).forEach(([paletteType, paletteConfig]) => {
		for (const [paletteKey, { hue, step, opacity }] of Object.entries(paletteConfig)) {
			if (!(paletteType in tailwindConfig.palette)) tailwindConfig.palette[paletteType] = {};
			const name = `--${UDS_PREFIX}-${paletteType}-color-${paletteKey}`;
			rootVars[name] = `var(--${SPECTRUM_COLOR_PREFIX}-${hue}-${step})`;
			const opacityValue = opacity !== void 0 ? parseOpacityStep(opacity) : void 0;
			tailwindConfig.palette[paletteType][paletteKey] = shouldIncludeOpacity(opacityValue) ? `rgb(var(${name}) / ${opacityValue}%)` : `rgb(var(${name}))`;
		}
	});
	entries(elevationPresets).forEach(([level, elevationPreset]) => {
		if (elevationPreset) entries(elevationPreset).forEach(([key]) => {
			if (key === "finalBackgroundValue") {
				const { r, g, b, a } = elevationPreset[key];
				const alpha = parseInt(a) / 100;
				const name = `--${BACKGROUND_COLOR_PREFIX}-elevation-${level}`;
				rootVars[name] = `rgba(${r}, ${g}, ${b}, ${alpha})`;
			}
			if (key === "borderColor") {
				const name = `--${LINE_COLOR_PREFIX}-elevation-${level}`;
				switch (elevationPreset[key].type) {
					case "always":
						rootVars[name] = alwaysPalette[elevationPreset[key].value];
						break;
					case "spectrum":
						rootVars[name] = `rgb(var(--${SPECTRUM_COLOR_PREFIX}-${elevationPreset[key].value.hue}-${elevationPreset[key].value.step}))`;
						break;
					case "line":
						rootVars[name] = `rgb(var(--${LINE_COLOR_PREFIX}-${elevationPreset[key].value}))`;
						break;
				}
			}
			if (key === "borderWidth") {
				const name = `--${BORDER_WIDTH_PREFIX}-elevation-${level}`;
				rootVars[name] = `var(--${BORDER_WIDTH_PREFIX}-${elevationPreset[key]})`;
			}
			if (key === "dropShadow") {
				const name = `--${DROP_SHADOW_PREFIX}-elevation-${level}`;
				const configValue = elevationPreset[key];
				rootVars[name] = Array.isArray(configValue) ? configValue.map((preset, index) => getShadowLayerValue({
					preset,
					prefix: index > 0 ? `${index + 1}` : void 0,
					shadowType: "drop"
				})).join(", ") : `var(--${DROP_SHADOW_PREFIX}-${elevationPreset[key]})`;
			}
			if (key === "backgroundBlurRadius") {
				const name = `--${BACKGROUND_BLUR_RADIUS_PREFIX}-elevation-${level}`;
				rootVars[name] = `blur(${elevationPreset[key]}px)`;
			}
			if (key === "fallbackBlurredBackgroundValue") {
				const { r, g, b } = elevationPreset[key];
				const name = `--${BACKGROUND_BLUR_FALLBACK_COLOR_PREFIX}-elevation-${level}`;
				rootVars[name] = `rgb(${r}, ${g}, ${b})`;
			}
		});
	});
	rootVars[`--${OUTLINE_PREFIX}-offset`] = focusRingConfig ? `${focusRingConfig.offset}px` : "2px";
	rootVars[`--${OUTLINE_PREFIX}-width`] = focusRingConfig ? `${focusRingConfig.width}px` : "2px";
	rootVars[`--${OUTLINE_PREFIX}-style`] = "solid";
	rootVars[`--${OUTLINE_PREFIX}-color`] = focusRingConfig ? resolveFocusRingColor(focusRingConfig.color) : `rgb(var(--${SPECTRUM_COLOR_PREFIX}-gray-15))`;
	return {
		_vars: rootVars,
		_raw: colors,
		tailwindConfig
	};
}
function transformNumericToPx(prefix, config) {
	if (!config) return {
		_vars: {},
		_raw: {},
		tailwindConfig: {}
	};
	const rootVars = {};
	const tailwindConfig = {};
	for (const [variant, value] of entries(config)) {
		const rootVarKey = `--${prefix}-${variant}`;
		rootVars[rootVarKey] = `${value}px`;
		tailwindConfig[variant] = `var(${rootVarKey})`;
	}
	return {
		_vars: rootVars,
		_raw: config,
		tailwindConfig
	};
}
function getElevationPresetsForColorMode(colorMode, config) {
	if (!config.elevation) return {};
	const result = {};
	entries(config.elevation).forEach(([level, levelPreset]) => {
		result[level] = levelPreset[colorMode];
	});
	return result;
}
function parseTokens(config) {
	const { colorMode, scaleMode } = config;
	return {
		...config,
		colorMode: {
			light: transformColors(colorMode.light, getElevationPresetsForColorMode("light", config), config.focusRing?.light),
			dark: transformColors(colorMode.dark, getElevationPresetsForColorMode("dark", config), config.focusRing?.dark)
		},
		scaleMode: mapValues(scaleMode, ({ borderRadius, borderWidth, avatarSizes, iconSizes }) => {
			return {
				borderRadius: transformNumericToPx(BORDER_RADIUS_PREFIX, borderRadius),
				borderWidth: transformNumericToPx(BORDER_WIDTH_PREFIX, borderWidth),
				avatarSizes: transformNumericToPx(AVATAR_SIZE_PREFIX, avatarSizes),
				iconSizes: transformNumericToPx(ICON_SIZE_PREFIX, iconSizes)
			};
		})
	};
}

//#endregion
export { parseTokens };
//# sourceMappingURL=parseTokens.js.map