<?php
namespace EVPB\Tools;

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

if ( !class_exists( 'ToCSS' ) ) {
	class ToCSS{
		static function isEmpty( $val ) {
			return (
				$val === null ||
				$val === false ||
				$val === '' ||
				( is_array( $val ) && empty( $val ) ) ||
				( is_string( $val ) && trim( $val ) === '' )
			);
		}

		static function hasCSS( $property, $value ) {
			return self::isEmpty( $value ) ? '' : "$property: $value;";
		}

		static function autoOrVal( $value ) {
			return self::isEmpty( $value ) ? '' : ( ( intval( $value ) < 0 ) ? 'auto' : $value );
		}

		static function cleanBGDetail( $bg ) {
			return $bg ? preg_replace( '/(background)-(color|image)(?=:)/', 'background', $bg ) : '';
		}

		static function borderCSS( $value = ['0px solid #0000'] ) {
			if ( count( $value ) === 0 ) {
				return '';
			}
		
			$top = $value[0];
		
			if ( count( $value ) >= 4 ) {
				$right = $value[1];
				$bottom = $value[2];
				$left = $value[3];
				return "border-top: $top; border-right: $right; border-bottom: $bottom; border-left: $left;";
			}
		
			return "border: $top;";
		}


		static function genTypo( $device ) {
			if ( empty( $device ) ) {
				return '';
			}

			$css = '';
			foreach ( $device as $property => $value ) {
				$css .= self::hasCSS( $property, $value ) . ' ';
			}
			return trim( $css );
		}

		static function typoCSS( $typo = [], $selector = '' ) {
			$desktop = $typo['desktop'] ?? [];
			$tablet = $typo['tablet'] ?? [];
			$mobile = $typo['mobile'] ?? [];

			$desCSS = self::genTypo( $desktop );
			$tabCSS = self::genTypo( $tablet );
			$mobCSS = self::genTypo( $mobile );

			$output = '';

			if ( !empty( $desCSS ) ) {
				$output .= "
					$selector {
						$desCSS
					}
				";
			}

			if ( !empty( $tabCSS ) ) {
				$output .= "
					". Helper::TAB_BREAKPOINT ." {
						$selector {
							$tabCSS
						}
					}
				";
			}
			
			if ( !empty( $mobCSS ) ) {
				$output .= "
					". Helper::MOB_BREAKPOINT ." {
						$selector {
							$mobCSS
						}
					}
				";
			}
			
			return preg_replace( '/\s+/', ' ', $output );
		}
	}
}