<?php

namespace Convertforce\Utils;

class StyleUtils
{
	public static function get_spacing_value($value)
	{
		if (!$value) {
			return null;
		}

		$matches = [];
		preg_match('/var:preset\|spacing\|(.+)/', $value, $matches);

		if (empty($matches)) {
			return $value;
		}
		return "var(--wp--preset--spacing--{$matches[1]})";
	}

	public static function get_spacing_values($object)
	{
		$css = [];

		foreach ($object as $key => $value) {
			$css[$key] = self::get_spacing_value($value);
		}

		return $css;
	}
	public static function get_spacing_style(array $object, string $prop)
	{
		$css = [];

		foreach ($object as $key => $value) {
			$css["{$prop}-{$key}"] = self::get_spacing_value($value);
		}

		return $css;
	}

	public static function get_box_shadow_string($settings)
	{
		if (!isset($settings)) {
			return null;
		}
		$x = $settings['x'] ?? 0;
		$y = $settings['y'] ?? 0;
		$blur = $settings['blur'] ?? 0;
		$spread = $settings['spread'] ?? 0;
		$color = $settings['color'] ?? '#000000';
		$type = $settings['type'] ?? 'outer';
		$type = $type === 'inner' ? 'inset ' : '';

		return "{$type}{$x}px {$y}px {$blur}px {$spread}px {$color}";

	}
	public static function get_border_css($settings)
	{
		if (!$settings) {
			return [];
		}
		if (is_string($settings)) {
			return [
				'border' => $settings,
			];
		}

		$isMultiSide = isset($settings['top']) || isset($settings['right']) || isset($settings['bottom']) || isset($settings['left']);
		if ($isMultiSide) {
			$css = [];
			$sides = ['top', 'right', 'bottom', 'left'];
			foreach ($sides as $side) {
				if (isset($settings[$side])) {
					$css["border-{$side}-width"] = $settings[$side]['width'] ?? null;
					$css["border-{$side}-color"] = $settings[$side]['color'] ?? null;
				}
			}
			return $css;
		}

		return [
			'border-width' => $settings['width'] ?? null,
			'border-color' => $settings['color'] ?? null,
		];
	}

	public static function get_border_radius_css($settings)
	{
		if (!$settings) {
			return [];
		}
		if (is_string($settings)) {
			return [
				'border-radius' => $settings,
			];
		}

		$css = [];
		foreach ($settings as $key => $value) {
			$kebabKey = self::camel_to_kebab($key);
			$css["border-{$kebabKey}-radius"] = $value;
		}
		return $css;
	}

	public static function generate_css_string($styles)
	{
		return esc_attr(self::build_css_string($styles));
	}

	/** Builds CSS declaration string for use in style blocks. Sanitizes to prevent breaking out of the style tag. */
	public static function build_css_string($styles)
	{
		$css_string = '';
		foreach ($styles as $key => $value) {
			if (!self::is_empty($value)) {
				$css_string .= $key . ': ' . $value . '; ';
			}
		}
		return self::sanitize_css_for_style_block($css_string);
	}

	/** Prevents style-block breakout and normalizes output (for use inside &lt;style&gt;). */
	public static function sanitize_css_for_style_block($css)
	{
		if ($css === '' || $css === null) {
			return '';
		}
		$css = (string) $css;
		$css = preg_replace('/<\/style\s*>/i', '', $css);
		$css = preg_replace('/\s+/', ' ', $css);
		return trim($css);
	}

	public static function is_empty($value)
	{
		return !$value || !trim($value);
	}

	public static function camel_to_kebab($input)
	{
		$withHyphens = preg_replace('/([a-z0-9])([A-Z])/', '$1-$2', $input);
		return strtolower($withHyphens);
	}

}