<?php
/**
 * Effect value builders and utility methods for AxcelersBlocksCss.
 *
 * @package AxcelersBlocks
 */

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

trait AxcelersBlocksCssEffects {

	// ─── Effect value builders ────────────────────────────────────────────────

	private function effectValue( array $values, string $key, string $fallback ): string {
		return ( isset( $values[ $key ] ) && '' !== $values[ $key ] ) ? (string) $values[ $key ] : $fallback;
	}

	private function effectBoxShadow( array $eff ): string {
		$bs = $eff['boxShadow'] ?? [];
		if ( empty( $bs ) ) {
			return '';
		}
		$x      = $this->effectValue( $bs, 'xOffset', '0px' );
		$y      = $this->effectValue( $bs, 'yOffset', '0px' );
		$blur   = $this->effectValue( $bs, 'blurRadius', '0px' );
		$spread = $this->effectValue( $bs, 'spreadRadius', '0px' );
		$color  = $this->effectValue( $bs, 'color', 'transparent' );
		$inset  = ! empty( $bs['inset'] ) ? 'inset ' : '';
		if ( $x === '0px' && $y === '0px' && $blur === '0px' && $spread === '0px' && $color === 'transparent' && ! $inset ) {
			return '';
		}
		return "{$inset}{$x} {$y} {$blur} {$spread} {$color}";
	}

	private function effectTransition( array $eff ): string {
		$t = $eff['transition'] ?? [];
		if ( empty( $t ) ) {
			return '';
		}
		$prop = $this->effectValue( $t, 'property', 'all' );
		$dur  = $this->effectValue( $t, 'duration', '0s' );
		$tf   = $this->effectValue( $t, 'timingFunction', 'ease' );
		$del  = $this->effectValue( $t, 'delay', '0s' );
		if ( $prop === 'all' && $dur === '0s' && $tf === 'ease' && $del === '0s' ) {
			return '';
		}
		return "$prop $dur $tf $del";
	}

	private function effectFilter( array $eff ): string {
		$f = $eff['filter'] ?? [];
		if ( empty( $f ) ) {
			return '';
		}
		$parts = [];
		if ( ! empty( $f['blur'] ) )                                        { $parts[] = "blur({$f['blur']})"; }
		if ( ! empty( $f['brightness'] ) && $f['brightness'] !== '100%' )   { $parts[] = "brightness({$f['brightness']})"; }
		if ( ! empty( $f['contrast'] )   && $f['contrast']   !== '100%' )   { $parts[] = "contrast({$f['contrast']})"; }
		if ( ! empty( $f['grayscale'] )  && $f['grayscale']  !== '0%' )     { $parts[] = "grayscale({$f['grayscale']})"; }
		if ( ! empty( $f['hueRotate'] )  && $f['hueRotate']  !== '0deg' )   { $parts[] = "hue-rotate({$f['hueRotate']})"; }
		if ( ! empty( $f['invert'] )     && $f['invert']     !== '0%' )     { $parts[] = "invert({$f['invert']})"; }
		if ( ! empty( $f['opacity'] )    && $f['opacity']    !== '100%' )   { $parts[] = "opacity({$f['opacity']})"; }
		if ( ! empty( $f['saturate'] )   && $f['saturate']   !== '100%' )   { $parts[] = "saturate({$f['saturate']})"; }
		if ( ! empty( $f['sepia'] )      && $f['sepia']      !== '0%' )     { $parts[] = "sepia({$f['sepia']})"; }
		return implode( ' ', $parts );
	}

	private function effectTextShadow( array $eff ): string {
		$ts = $eff['textShadow'] ?? [];
		if ( empty( $ts ) ) {
			return '';
		}
		$x     = $this->effectValue( $ts, 'xOffset', '0px' );
		$y     = $this->effectValue( $ts, 'yOffset', '0px' );
		$blur  = $this->effectValue( $ts, 'blurRadius', '0px' );
		$color = $this->effectValue( $ts, 'color', 'transparent' );
		if ( $x === '0px' && $y === '0px' && $blur === '0px' && $color === 'transparent' ) {
			return '';
		}
		return "$x $y $blur $color";
	}

	// ─── Shorthand utilities ──────────────────────────────────────────────────

	private function spacingShorthand( string $t, string $r, string $b, string $l ): string {
		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";
	}

	private function radiusShorthand( string $tl, string $tr, string $br, string $bl ): string {
		if ( $tl === $tr && $tr === $br && $br === $bl ) { return $tl; }
		if ( $tl === $br && $tr === $bl )                { return "$tl $tr"; }
		return "$tl $tr $br $bl";
	}

	private function hexToRgb( string $hex ): string {
		$hex = ltrim( $hex, '#' );
		if ( strlen( $hex ) < 6 ) {
			return '0,0,0';
		}
		return hexdec( substr( $hex, 0, 2 ) ) . ',' .
		       hexdec( substr( $hex, 2, 2 ) ) . ',' .
		       hexdec( substr( $hex, 4, 2 ) );
	}

	/** @return array{width:string,height:string} */
	private function normalizeIconSize( $raw ): array {
		if ( $raw === null || $raw === '' )  { return [ 'width' => '', 'height' => '' ]; }
		if ( is_string( $raw ) )             { return [ 'width' => $raw, 'height' => $raw ]; }
		return [
			'width'  => ( is_string( $raw['width']  ?? null ) ? $raw['width']  : '' ),
			'height' => ( is_string( $raw['height'] ?? null ) ? $raw['height'] : '' ),
		];
	}
}
