<?php
/**
 * CSS generator for the Video block's <video> element.
 *
 * The video block targets two separate elements:
 *   - <figure class="uniqueClass"> — handled by axcelersblocks_generate_block_styles()
 *   - <figure class="uniqueClass"> video — handled here
 *
 * @package AxcelersBlocks
 */

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

if ( ! function_exists( 'axcelersblocks_video_generate_video_css' ) ) {
	/**
	 * Generate responsive CSS rules for the <video> element inside a video block.
	 *
	 * Handles: object-fit, object-position, width, height, min/max dimensions, border-radius.
	 *
	 * @param string $unique_class Block-scoped unique class.
	 * @param array  $devices      Device map from axcelersblocks_get_responsive_devices().
	 * @param array  $attributes   Full block attributes.
	 * @return string[] CSS rule strings.
	 */
	function axcelersblocks_video_generate_video_css( string $unique_class, array $devices, array $attributes ): array {
		// Match the shared CSS generator's mobile-first compatibility behavior:
		// old/default data that only has a desktop value must still emit a base
		// rule so it applies below the first min-width breakpoint.
		$has_css_v = function ( $v ) use ( &$has_css_v ) {
			if ( is_bool( $v ) || is_null( $v ) ) { return false; }
			if ( is_string( $v ) ) { return '' !== $v; }
			if ( is_array( $v ) ) {
				foreach ( $v as $item ) {
					if ( $has_css_v( $item ) ) { return true; }
				}
			}
			return false;
		};

		foreach ( $attributes as &$attribute_value ) {
			if ( ! is_array( $attribute_value ) ) { continue; }
			$has_desktop = isset( $attribute_value['desktop'] ) && $has_css_v( $attribute_value['desktop'] );
			$has_tablet  = isset( $attribute_value['tablet'] ) && $has_css_v( $attribute_value['tablet'] );
			$has_mobile  = isset( $attribute_value['mobile'] ) && $has_css_v( $attribute_value['mobile'] );
			if ( $has_desktop && ! $has_mobile ) {
				$attribute_value['mobile'] = $attribute_value['desktop'];
			}
			if ( $has_tablet && ! $has_desktop ) {
				$attribute_value['desktop'] = $attribute_value['tablet'];
				if ( ! $has_mobile ) {
					$attribute_value['mobile'] = $attribute_value['tablet'];
				}
			}
		}
		unset( $attribute_value );

		$styles_css = [];
		$cascade    = [];
		// Target both the self-hosted <video> and the embed <iframe> so the Video
		// sizing controls (width, max-width, border-radius …) apply to either.
		$cls        = sanitize_html_class( $unique_class );
		$selector   = ".$cls video, .$cls iframe";
		$overlay_selector = ".$cls .axcelersblocks-video__play-overlay";

		// Simple responsive attribute → CSS property map.
		$props = [
			'object-fit'      => 'videoObjectFit',
			'object-position' => 'videoObjectPosition',
			'width'           => 'videoWidth',
			'height'          => 'videoHeight',
			'min-width'       => 'videoMinWidth',
			'min-height'      => 'videoMinHeight',
			'max-width'       => 'videoMaxWidth',
			'max-height'      => 'videoMaxHeight',
			'border-radius'   => 'videoBorderRadius',
		];

		foreach ( $devices as $device => $config ) {
			$rules         = [];
			$overlay_rules = [];

			foreach ( $props as $css_prop => $attr_key ) {
				$val = axcelersblocks_sanitize_css_value( $attributes[ $attr_key ][ $device ] ?? '' );
				if ( $val === '' ) {
					continue;
				}
				// Cascade deduplication: skip identical values on tablet/desktop.
				if ( $device !== 'mobile' && isset( $cascade[ $css_prop ] ) && $cascade[ $css_prop ] === $val ) {
					continue;
				}
				$cascade[ $css_prop ] = $val;
				$rules[]              = "$css_prop: $val;";
				if ( 'border-radius' === $css_prop ) {
					$overlay_rules[] = "$css_prop: $val;";
				}
			}

			if ( empty( $rules ) ) {
				continue;
			}

			$css          = implode( ' ', $rules );
			$styles_css[] = empty( $config['media'] )
				? "$selector { $css }"
				: "{$config['media']} { $selector { $css } }";

			if ( ! empty( $overlay_rules ) ) {
				$overlay_css  = implode( ' ', $overlay_rules );
				$styles_css[] = empty( $config['media'] )
					? "$overlay_selector { $overlay_css }"
					: "{$config['media']} { $overlay_selector { $overlay_css } }";
			}
		}

		return $styles_css;
	}
}
