<?php
/**
 * Responsive CSS Helper
 *
 * @package Brandy\DynamicCss
 */

namespace Brandy\DynamicCss;

/**
 * Helper class for responsive CSS operations
 */
class ResponsiveHelper {

	/**
	 * Get empty device-keyed result array
	 *
	 * @return array Empty array with desktop/tablet/mobile keys
	 */
	public function get_empty_device_result(): array {
		return [
			'desktop' => [],
			'tablet'  => [],
			'mobile'  => [],
		];
	}

	/**
	 * Wrap CSS in tablet media query
	 *
	 * @param string $css CSS content to wrap.
	 * @return string CSS wrapped in tablet media query
	 */
	public function wrap_tablet_responsive( string $css ): string {
		return '@media screen and (min-width: ' . ( BRANDY_MOBILE_MAX_WIDTH + 1 ) . 'px) and (max-width: ' . BRANDY_TABLET_MAX_WIDTH . 'px) {' . $css . '}';
	}

	/**
	 * Wrap CSS in mobile media query
	 *
	 * @param string $css CSS content to wrap.
	 * @return string CSS wrapped in mobile media query
	 */
	public function wrap_mobile_responsive( string $css ): string {
		return '@media screen and (max-width: ' . BRANDY_MOBILE_MAX_WIDTH . 'px) {' . $css . '}';
	}

	/**
	 * Get available devices for a given device context
	 *
	 * @param string $device Base device type.
	 * @return array List of available devices
	 */
	public function get_available_devices( string $device ): array {
		$devices = [ $device ];
		if ( 'mobile' === $device ) {
			$devices[] = 'tablet';
		}
		return $devices;
	}

	/**
	 * Add CSS variable to result array for given device and selector
	 *
	 * @param array  $result   Result array to modify (passed by reference).
	 * @param string $device   Device key (desktop/tablet/mobile).
	 * @param string $selector CSS selector (empty string for root).
	 * @param string $css_var  CSS variable declaration.
	 * @return void
	 */
	public function add_to_result( array &$result, string $device, string $selector, string $css_var ): void {
		if ( empty( $css_var ) ) {
			return;
		}
		if ( ! empty( $selector ) ) {
			if ( ! isset( $result[ $device ][ $selector ] ) ) {
				$result[ $device ][ $selector ] = [];
			}
			$result[ $device ][ $selector ][] = $css_var;
		} else {
			$result[ $device ][] = $css_var;
		}
	}
}
