/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { BUTTON_CONTROL_HEIGHT_VAR } from "../../../css-tokens/dist/index.js";
import { mapTextVariantFixtureToValue } from "../mapTextVariantFixtureToValue.js";
import { configurableProperties } from "../properties.js";
import { isIconButtonMatchButtonHeightEnabled } from "./iconButtonMatchButtonHeight.js";
//#region ../automated-config/dist/utils/getButtonControlHeight.js
/*! © 2026 Yahoo, Inc. UDS Default Config v0.0.0-development */
/** Sizes where IconButton control height should match Button. */
const SHARED_BUTTON_ICONBUTTON_SIZES = new Set([
	"xs",
	"sm",
	"md",
	"lg"
]);
function getButtonIconSizeVar({ schema, iconLayerKey, state, theme }) {
	const iconSizeValue = schema.variables?.[iconLayerKey]?.size?.[state]?.value;
	if (typeof iconSizeValue === "string" && iconSizeValue) return theme(configurableProperties.iconSize.twThemePath("iconSizes", iconSizeValue));
}
/**
* Button content box: max(icon size, text line-height).
* Matches Figma auto-layout and keeps IconButton height aligned with Button.
*/
function getButtonControlContentSize({ schema, rootLayerKey, iconLayerKey, state, theme }) {
	const iconSizeVar = getButtonIconSizeVar({
		schema,
		iconLayerKey,
		state,
		theme
	});
	const textVariantValue = schema.variables?.[rootLayerKey]?.textVariant?.[state]?.value;
	if (iconSizeVar && typeof textVariantValue === "string" && textVariantValue) return `max(${iconSizeVar}, ${theme(mapTextVariantFixtureToValue("lineHeight")("textVariants", textVariantValue))})`;
	if (iconSizeVar) return iconSizeVar;
}
/** `round(up, …, 1px)` matches Math.ceil in getButtonControlHeightPx and Figma hug height. */
function formatControlHeightCalc(calcExpression) {
	return `round(up, ${calcExpression}, 1px)`;
}
/**
* Button control height: vertical padding on both sides plus max(icon size, text line-height).
*/
function getButtonControlHeight({ paddingVertical, schema, rootLayerKey, iconLayerKey, state, theme }) {
	const contentSize = getButtonControlContentSize({
		schema,
		rootLayerKey,
		iconLayerKey,
		state,
		theme
	});
	if (!contentSize) return;
	return formatControlHeightCalc(`calc(2 * ${paddingVertical} + ${contentSize})`);
}
/**
* IconButton control height: uniform padding plus icon size only (no typography).
*/
function getIconButtonControlHeight({ paddingVertical, schema, iconLayerKey, state, theme }) {
	const iconSizeVar = getButtonIconSizeVar({
		schema,
		iconLayerKey,
		state,
		theme
	});
	if (!iconSizeVar) return;
	return formatControlHeightCalc(`calc(2 * ${paddingVertical} + ${iconSizeVar})`);
}
/**
* Layout line-height for button root: max(icon size, text line-height).
*/
function getButtonContentLineHeight({ schema, rootLayerKey, iconLayerKey, state, theme }) {
	return getButtonControlContentSize({
		schema,
		rootLayerKey,
		iconLayerKey,
		state,
		theme
	}) ?? "1em";
}
function applyButtonControlHeightDeclarations({ cssDeclarations, paddingVertical, schema, rootLayerKey, iconLayerKey, state, theme }) {
	const controlHeight = rootLayerKey ? getButtonControlHeight({
		paddingVertical,
		schema,
		rootLayerKey,
		iconLayerKey,
		state,
		theme
	}) : getIconButtonControlHeight({
		paddingVertical,
		schema,
		iconLayerKey,
		state,
		theme
	});
	if (controlHeight) {
		cssDeclarations[BUTTON_CONTROL_HEIGHT_VAR] = controlHeight;
		cssDeclarations.height = `var(${BUTTON_CONTROL_HEIGHT_VAR})`;
	}
}
function applyIconButtonSquareDimensions(cssDeclarations) {
	if (cssDeclarations[BUTTON_CONTROL_HEIGHT_VAR]) cssDeclarations.width = `var(${BUTTON_CONTROL_HEIGHT_VAR})`;
}
/**
* IconButton height for xs–lg mirrors Button (padding + max(icon, line-height)).
* xl uses icon-only height from IconButton tokens.
*/
function applyIconButtonControlHeightDeclarations({ cssDeclarations, paddingVertical, iconButtonSchema, buttonHeightReferenceSchema, variantValue, iconLayerKey: _iconLayerKey, state, theme }) {
	if (buttonHeightReferenceSchema && SHARED_BUTTON_ICONBUTTON_SIZES.has(variantValue) && isIconButtonMatchButtonHeightEnabled(iconButtonSchema, variantValue)) {
		const buttonRootLayerKey = `size/${variantValue}/root`;
		const buttonIconLayerKey = `size/${variantValue}/icon`;
		const buttonSpacingValue = buttonHeightReferenceSchema.variables?.[buttonRootLayerKey]?.spacingVertical?.[state]?.value;
		applyButtonControlHeightDeclarations({
			cssDeclarations,
			paddingVertical: typeof buttonSpacingValue === "string" && buttonSpacingValue ? theme(configurableProperties.spacingVertical.twThemePath("spacingAliases", buttonSpacingValue)) : paddingVertical,
			schema: buttonHeightReferenceSchema,
			rootLayerKey: buttonRootLayerKey,
			iconLayerKey: buttonIconLayerKey,
			state,
			theme
		});
		applyIconButtonSquareDimensions(cssDeclarations);
	}
}
//#endregion
export { SHARED_BUTTON_ICONBUTTON_SIZES, applyButtonControlHeightDeclarations, applyIconButtonControlHeightDeclarations, getButtonContentLineHeight, getButtonControlContentSize, getButtonControlHeight, getButtonIconSizeVar, getIconButtonControlHeight };
