<?php
/**
 * Shared helpers for effect style serialization.
 *
 * @package AxcelersBlocks
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'axcelersblocks_sanitize_transform_style_value' ) ) {
	/**
	 * Normalize a transform control value to a trimmed scalar string.
	 *
	 * @param mixed $value Raw transform value.
	 * @return string
	 */
	function axcelersblocks_sanitize_transform_style_value( $value ) {
		if ( null === $value || is_array( $value ) || is_object( $value ) ) {
			return '';
		}

		return trim( (string) $value );
	}
}

if ( ! function_exists( 'axcelersblocks_spacing_shorthand' ) ) {
	/**
	 * Build an optimal CSS shorthand value from four box-model sides (T R B L).
	 *
	 * Returns the most compact representation:
	 *   1 value  — all sides equal
	 *   2 values — top/bottom equal AND right/left equal
	 *   3 values — right/left equal
	 *   4 values — all different
	 *
	 * @param string $t Top value.
	 * @param string $r Right value.
	 * @param string $b Bottom value.
	 * @param string $l Left value.
	 * @return string
	 */
	function axcelersblocks_spacing_shorthand( $t, $r, $b, $l ) {
		if ( $t === $r && $r === $b && $b === $l ) {
			return $t;
		}
		if ( $t === $b && $r === $l ) {
			return "$t $r";
		}
		if ( $r === $l ) {
			return "$t $r $b";
		}
		return "$t $r $b $l";
	}
}

if ( ! function_exists( 'axcelersblocks_border_radius_shorthand' ) ) {
	/**
	 * Build an optimal CSS border-radius shorthand from four corner values (TL TR BR BL).
	 *
	 * The two-value rule for border-radius: first = TL + BR, second = TR + BL.
	 *
	 * @param string $tl Top-left value.
	 * @param string $tr Top-right value.
	 * @param string $br Bottom-right value.
	 * @param string $bl Bottom-left value.
	 * @return string
	 */
	function axcelersblocks_border_radius_shorthand( $tl, $tr, $br, $bl ) {
		if ( $tl === $tr && $tr === $br && $br === $bl ) {
			return $tl;
		}
		if ( $tl === $br && $tr === $bl ) {
			return "$tl $tr";
		}
		return "$tl $tr $br $bl";
	}
}

if ( ! function_exists( 'axcelersblocks_build_transform_style' ) ) {
	/**
	 * Build a safe CSS transform string from effect settings.
	 *
	 * CSS supports translateZ() and rotateZ(), but not skewZ(), so skew stays X/Y only.
	 *
	 * @param mixed $transform Transform settings array.
	 * @return string|null
	 */
	function axcelersblocks_build_transform_style( $transform ) {
		if ( ! is_array( $transform ) ) {
			return null;
		}

		$translate_x = axcelersblocks_sanitize_transform_style_value( $transform['translateX'] ?? '' );
		$translate_y = axcelersblocks_sanitize_transform_style_value( $transform['translateY'] ?? '' );
		$translate_z = axcelersblocks_sanitize_transform_style_value( $transform['translateZ'] ?? '' );
		$scale_x     = axcelersblocks_sanitize_transform_style_value( $transform['scaleX'] ?? '' );
		$scale_y     = axcelersblocks_sanitize_transform_style_value( $transform['scaleY'] ?? '' );
		$rotate_x    = axcelersblocks_sanitize_transform_style_value( $transform['rotateX'] ?? '' );
		$rotate_y    = axcelersblocks_sanitize_transform_style_value( $transform['rotateY'] ?? '' );
		$rotate_z    = axcelersblocks_sanitize_transform_style_value( $transform['rotateZ'] ?? ( $transform['rotate'] ?? '' ) );
		$skew_x      = axcelersblocks_sanitize_transform_style_value( $transform['skewX'] ?? '' );
		$skew_y      = axcelersblocks_sanitize_transform_style_value( $transform['skewY'] ?? '' );

		$parts = array();

		if ( '' !== $translate_z ) {
			$parts[] = 'translate3d(' . ( '' !== $translate_x ? $translate_x : '0px' ) . ', ' . ( '' !== $translate_y ? $translate_y : '0px' ) . ', ' . $translate_z . ')';
		} else {
			if ( '' !== $translate_x ) {
				$parts[] = 'translateX(' . $translate_x . ')';
			}

			if ( '' !== $translate_y ) {
				$parts[] = 'translateY(' . $translate_y . ')';
			}
		}

		if ( '' !== $scale_x ) {
			$parts[] = 'scaleX(' . $scale_x . ')';
		}

		if ( '' !== $scale_y ) {
			$parts[] = 'scaleY(' . $scale_y . ')';
		}

		if ( '' !== $rotate_x ) {
			$parts[] = 'rotateX(' . $rotate_x . ')';
		}

		if ( '' !== $rotate_y ) {
			$parts[] = 'rotateY(' . $rotate_y . ')';
		}

		if ( '' !== $rotate_z ) {
			$parts[] = 'rotateZ(' . $rotate_z . ')';
		}

		if ( '' !== $skew_x ) {
			$parts[] = 'skewX(' . $skew_x . ')';
		}

		if ( '' !== $skew_y ) {
			$parts[] = 'skewY(' . $skew_y . ')';
		}

		return ! empty( $parts ) ? implode( ' ', $parts ) : null;
	}
}
