/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { spacingMap } from "../../../fixtures/dist/index.js";
import { isIconButtonMatchButtonHeightEnabled } from "./iconButtonMatchButtonHeight.js";
import { get } from "lodash-es";
import { ICON_SIZE_MAP } from "@yahoo/uds-icons/tokens";
//#region ../automated-config/dist/utils/getButtonControlHeightPx.js
/*! © 2026 Yahoo, Inc. UDS Default Config v0.0.0-development */
const SHARED_BUTTON_ICONBUTTON_SIZES = new Set([
	"xs",
	"sm",
	"md",
	"lg"
]);
function resolveSpacingPx(config, alias) {
	if (!alias) return 0;
	const scaleValue = get(config, [
		"scaleMode",
		"large",
		"spacing",
		alias.replace(".", "_")
	]);
	if (typeof scaleValue === "number") return scaleValue;
	return spacingMap[alias] ?? 0;
}
function resolveTypographyPx(config, textVariant, property) {
	if (!textVariant) return 0;
	const typographyConfig = config.typography[textVariant];
	if (!typographyConfig?.[property]) return 0;
	const defaultBreakpoint = config.globalDefaults.breakpoint;
	const value = defaultBreakpoint === "base" ? typographyConfig[property].base : typographyConfig[property].breakpoints[defaultBreakpoint];
	if (typeof value === "number") return value;
	if (value && typeof value === "object" && "value" in value) return value.value;
	return 0;
}
/** Ceil fractional totals so Figma-bound controlHeight matches auto-layout hug (e.g. xs + ui3 15.4lh → 28). */
function snapControlHeightPx(value) {
	return Math.ceil(value);
}
/** Pixel control height for Button: padding + max(icon size, text line-height). */
function getButtonControlHeightPx(config, size) {
	const button = config.button;
	if (!button?.variables) return 0;
	const layerPrefix = `size/${size}`;
	const paddingAlias = button.variables[`${layerPrefix}/root`]?.spacingVertical?.rest?.value;
	const iconSizeAlias = button.variables[`${layerPrefix}/icon`]?.size?.rest?.value;
	const textVariant = button.variables[`${layerPrefix}/root`]?.textVariant?.rest?.value;
	const padding = resolveSpacingPx(config, paddingAlias);
	const iconSize = ICON_SIZE_MAP[iconSizeAlias ?? "sm"] ?? 16;
	const lineHeight = resolveTypographyPx(config, textVariant, "lineHeight");
	return snapControlHeightPx(padding * 2 + Math.max(iconSize, lineHeight || iconSize));
}
/** Pixel control height for IconButton: uniform padding plus icon size only. */
function getIconButtonOnlyControlHeightPx(config, size) {
	const iconButton = config.iconButton;
	if (!iconButton?.variables) return 0;
	const layerPrefix = `size/${size}`;
	const paddingAlias = iconButton.variables[`${layerPrefix}/root`]?.spacing?.rest?.value;
	const iconSizeAlias = iconButton.variables[`${layerPrefix}/icon`]?.size?.rest?.value;
	const padding = resolveSpacingPx(config, paddingAlias);
	const iconSize = ICON_SIZE_MAP[iconSizeAlias ?? "sm"] ?? 16;
	return snapControlHeightPx(padding * 2 + iconSize);
}
/** Pixel control height for IconButton when match-button-height is enabled for that size. */
function getIconButtonControlHeightPx(config, size) {
	if (SHARED_BUTTON_ICONBUTTON_SIZES.has(size)) {
		if (!isIconButtonMatchButtonHeightEnabled(config, size)) return 0;
		return getButtonControlHeightPx(config, size);
	}
	return 0;
}
function setControlHeightVar(config, componentKey, size, heightPx) {
	const rootKey = `size/${size}/root`;
	const componentConfig = config[componentKey];
	const existingRoot = componentConfig?.variables?.[rootKey] ?? {};
	return {
		...config,
		[componentKey]: {
			...componentConfig,
			variables: {
				...componentConfig?.variables,
				[rootKey]: {
					...existingRoot,
					controlHeight: { rest: {
						type: "positiveIntegers",
						value: snapControlHeightPx(heightPx),
						valueType: "alias"
					} }
				}
			}
		}
	};
}
function syncButtonControlHeightVarForSize(config, size) {
	if (!SHARED_BUTTON_ICONBUTTON_SIZES.has(size) || !isIconButtonMatchButtonHeightEnabled(config, size)) return config;
	return setControlHeightVar(config, "iconButton", size, getButtonControlHeightPx(config, size));
}
function syncIconButtonControlHeightVarForSize(config, size) {
	const heightPx = getIconButtonControlHeightPx(config, size);
	if (heightPx <= 0) return config;
	return setControlHeightVar(config, "iconButton", size, heightPx);
}
function syncIconButtonOnlyControlHeightVarForSize(config, size) {
	const heightPx = getIconButtonOnlyControlHeightPx(config, size);
	if (heightPx <= 0) return config;
	return setControlHeightVar(config, "iconButton", size, heightPx);
}
//#endregion
export { getButtonControlHeightPx, getIconButtonControlHeightPx, getIconButtonOnlyControlHeightPx, syncButtonControlHeightVarForSize, syncIconButtonControlHeightVarForSize, syncIconButtonOnlyControlHeightVarForSize };
