/**
 * Button icon size / color helpers shared by Fields + Styles panels.
 */

export const DEFAULT_BUTTON_ICON_SIZE = '20px';

/**
 * Normalize button icon size; empty / invalid → 20px.
 *
 * @param {string} value
 * @return {string}
 */
export function resolveButtonIconSize( value ) {
	const raw = String( value || '' ).trim();
	if ( ! raw ) {
		return DEFAULT_BUTTON_ICON_SIZE;
	}
	if ( /^-?[\d.]+$/.test( raw ) ) {
		return `${ raw }px`;
	}
	if ( /^-?[\d.]+(px|rem|em|%)$/i.test( raw ) ) {
		return raw;
	}
	return DEFAULT_BUTTON_ICON_SIZE;
}

/**
 * Sanitize a CSS color for inline icon styles (empty → inherit).
 *
 * @param {string} value
 * @return {string}
 */
export function resolveButtonIconColor( value ) {
	const raw = String( value || '' ).trim();
	if ( ! raw ) {
		return '';
	}
	// Allow common CSS color forms only.
	if (
		/^(#([0-9a-f]{3,8})|rgba?\([^)]+\)|hsla?\([^)]+\)|currentcolor|inherit|transparent|[a-z]+)$/i.test(
			raw
		)
	) {
		return raw;
	}
	return '';
}
