/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
const require_entries = require("../packages/utils/dist/entries.cjs");
const require_opacity = require("./opacity.cjs");
const require_index = require("../packages/palette/dist/index.cjs");
const require_index$1 = require("../packages/css-tokens/dist/index.cjs");
const require_mapValues = require("../packages/utils/dist/mapValues.cjs");
//#region ../tailwind-internal/dist/utils/parseTokens.js
/*! © 2026 Yahoo, Inc. UDS Tailwind Internal v0.0.0-development */
/** 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 formatColorVarWithOpacity(name, opacity) {
	return shouldIncludeOpacity(opacity) ? `rgb(var(${name}) / ${opacity}%)` : `rgb(var(${name}))`;
}
function resolveFocusRingColor(color) {
	switch (color.type) {
		case "spectrum": return `rgb(var(--${require_index$1.SPECTRUM_COLOR_PREFIX}-${color.value.hue}-${color.value.step}))`;
		case "line": return `rgb(var(--${require_index$1.LINE_COLOR_PREFIX}-${color.value}))`;
		case "always": return require_index.alwaysPalette[color.value];
		default: return `rgb(var(--${require_index$1.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 require_entries.entries(spectrum)) for (const [hueStep, value] of require_entries.entries(hueSteps)) {
		const varKey = `--${require_index$1.SPECTRUM_COLOR_PREFIX}-${`${hue}-${hueStep}`}`;
		if (!tailwindConfig.spectrum[hue]) tailwindConfig.spectrum[hue] = {};
		tailwindConfig.spectrum[hue][hueStep] = `rgb(var(${varKey}))`;
		rootVars[varKey] = spectrumValueToRgbString(value);
	}
	require_entries.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-${paletteType}-color-${paletteKey}`;
			rootVars[name] = `var(--${require_index$1.SPECTRUM_COLOR_PREFIX}-${hue}-${step})`;
			const opacityValue = opacity !== void 0 ? require_opacity.parseOpacityStep(opacity) : void 0;
			tailwindConfig.palette[paletteType][paletteKey] = formatColorVarWithOpacity(name, opacityValue);
		}
	});
	require_entries.entries(elevationPresets).forEach(([level, elevationPreset]) => {
		if (elevationPreset) require_entries.entries(elevationPreset).forEach(([key]) => {
			if (key === "finalBackgroundValue") {
				const { r, g, b, a } = elevationPreset[key];
				const alpha = parseInt(a) / 100;
				const name = `--${require_index$1.BACKGROUND_COLOR_PREFIX}-elevation-${level}`;
				rootVars[name] = `rgba(${r}, ${g}, ${b}, ${alpha})`;
			}
			if (key === "borderColor") {
				const name = `--${require_index$1.LINE_COLOR_PREFIX}-elevation-${level}`;
				switch (elevationPreset[key].type) {
					case "always":
						rootVars[name] = require_index.alwaysPalette[elevationPreset[key].value];
						break;
					case "spectrum":
						rootVars[name] = `rgb(var(--${require_index$1.SPECTRUM_COLOR_PREFIX}-${elevationPreset[key].value.hue}-${elevationPreset[key].value.step}))`;
						break;
					case "line": {
						const lineColor = palette.line[elevationPreset[key].value];
						const opacityValue = lineColor.opacity !== void 0 ? require_opacity.parseOpacityStep(lineColor.opacity) : void 0;
						rootVars[name] = formatColorVarWithOpacity(`--${require_index$1.LINE_COLOR_PREFIX}-${elevationPreset[key].value}`, opacityValue);
						break;
					}
				}
			}
			if (key === "borderWidth") {
				const name = `--${require_index$1.BORDER_WIDTH_PREFIX}-elevation-${level}`;
				rootVars[name] = `var(--${require_index$1.BORDER_WIDTH_PREFIX}-${elevationPreset[key]})`;
			}
			if (key === "dropShadow") {
				const name = `--${require_index$1.DROP_SHADOW_PREFIX}-elevation-${level}`;
				const configValue = elevationPreset[key];
				rootVars[name] = Array.isArray(configValue) ? configValue.map((preset, index) => require_index$1.getShadowLayerValue({
					preset,
					prefix: index > 0 ? `${index + 1}` : void 0,
					shadowType: "drop"
				})).join(", ") : `var(--${require_index$1.DROP_SHADOW_PREFIX}-${elevationPreset[key]})`;
			}
			if (key === "insetShadow") {
				const name = `--${require_index$1.INSET_SHADOW_PREFIX}-elevation-${level}`;
				const configValue = elevationPreset[key];
				rootVars[name] = Array.isArray(configValue) ? configValue.map((preset, index) => require_index$1.getShadowLayerValue({
					preset,
					prefix: index > 0 ? `${index + 1}` : void 0,
					shadowType: "inset"
				})).join(", ") : `var(--${require_index$1.INSET_SHADOW_PREFIX}-${elevationPreset[key]})`;
			}
			if (key === "backgroundBlurRadius") {
				const name = `--${require_index$1.BACKGROUND_BLUR_RADIUS_PREFIX}-elevation-${level}`;
				rootVars[name] = `blur(${elevationPreset[key]}px)`;
			}
			if (key === "fallbackBlurredBackgroundValue") {
				const { r, g, b } = elevationPreset[key];
				const name = `--${require_index$1.BACKGROUND_BLUR_FALLBACK_COLOR_PREFIX}-elevation-${level}`;
				rootVars[name] = `rgb(${r}, ${g}, ${b})`;
			}
		});
	});
	rootVars[`--${require_index$1.OUTLINE_PREFIX}-offset`] = focusRingConfig ? `${focusRingConfig.offset}px` : "2px";
	rootVars[`--${require_index$1.OUTLINE_PREFIX}-width`] = focusRingConfig ? `${focusRingConfig.width}px` : "2px";
	rootVars[`--${require_index$1.OUTLINE_PREFIX}-style`] = "solid";
	rootVars[`--${require_index$1.OUTLINE_PREFIX}-color`] = focusRingConfig ? resolveFocusRingColor(focusRingConfig.color) : `rgb(var(--${require_index$1.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 require_entries.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 = {};
	require_entries.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: require_mapValues.mapValues(scaleMode, ({ borderRadius, borderWidth, avatarSizes, iconSizes }) => {
			return {
				borderRadius: transformNumericToPx(require_index$1.BORDER_RADIUS_PREFIX, borderRadius),
				borderWidth: transformNumericToPx(require_index$1.BORDER_WIDTH_PREFIX, borderWidth),
				avatarSizes: transformNumericToPx(require_index$1.AVATAR_SIZE_PREFIX, avatarSizes),
				iconSizes: transformNumericToPx(require_index$1.ICON_SIZE_PREFIX, iconSizes)
			};
		})
	};
}
//#endregion
exports.parseTokens = parseTokens;
