<?php
/**
 * CssOutput.
 *
 * @package MadeByAura\Prime\Customizer\Hooks
 * @author  MadeByAura.com
 */

namespace MadeByAura\Prime\Customizer\Hooks;

use MadeByAura\Prime\Base;
use MadeByAura\Prime\Helpers\SingletonTrait;
use MadeByAura\Prime\Modules\Button;

defined( 'ABSPATH' ) || die();

/**
 * CssOutput.
 */
class CssOutput {
	use SingletonTrait;

	/**
	 * Constructor.
	 */
	protected function __construct() {
		add_action( 'kirki_aura_theme_config_styles', [ __CLASS__, 'add_typography_variant_css' ] );
	}

	/**
	 * Add typography css.
	 *
	 * @param array $css  CSS.
	 * @return array $css
	 */
	public static function add_typography_variant_css( $css ) {
		// Body.
		$variant  = Base::get_mod( 'typography', 'body' )['variant'];
		$styles[] = [
			'selector'        => ':root',
			'property_prefix' => '--aura-g-body-',
			'value'           => self::parse_font_variant( $variant ),
		];

		// Headings.
		$variant = Base::get_mod( 'typography', 'headings' )['variant'];
		$variant = self::parse_font_variant( $variant );
		foreach ( [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ] as $tag ) {
			$styles[] = [
				'selector'        => ':root',
				'property_prefix' => "--aura-g-{$tag}-",
				'value'           => $variant,
			];
		}

		// Buttons.
		foreach ( Button::get_types() as $id => $label ) {
			$variant  = Base::get_mod( "button_{$id}", 'typography' )['variant'];
			$styles[] = [
				'selector'        => ':root',
				'property_prefix' => "--aura-g-button-{$id}-",
				'value'           => self::parse_font_variant( $variant ),
			];
		}

		// Forms > Label.
		$variant  = Base::get_mod( 'label', 'typography' )['variant'];
		$styles[] = [
			'selector'        => ':root',
			'property_prefix' => '--aura-g-label-',
			'value'           => self::parse_font_variant( $variant ),
		];

		// Forms > Field.
		$variant  = Base::get_mod( 'field', 'typography' )['variant'];
		$styles[] = [
			'selector'        => ':root',
			'property_prefix' => '--aura-g-field-',
			'value'           => self::parse_font_variant( $variant ),
		];

		// Forms > Field.
		$variant  = Base::get_mod( 'submit', 'typography' )['variant'];
		$styles[] = [
			'selector'        => ':root',
			'property_prefix' => '--aura-g-submit-',
			'value'           => self::parse_font_variant( $variant ),
		];

		// Content > Blockquote.
		$variant  = Base::get_mod( 'content_blockquote', 'typography' )['variant'];
		$styles[] = [
			'selector' => '.aura-formatted-content :where(blockquote)',
			'value'    => self::parse_font_variant( $variant ),
		];

		// Add CSS.
		foreach ( $styles as $style ) {
			$selector        = is_array( $style['selector'] ) ? implode( ',', $style['selector'] ) : $style['selector'];
			$property_prefix = $style['property_prefix'] ?? '';

			$css['global'][ $selector ][ $property_prefix . 'font-weight' ] = $style['value']['font-weight'];
			$css['global'][ $selector ][ $property_prefix . 'font-style' ]  = $style['value']['font-style'];
		}

		// Content > Blockquote > Force italic.
		if ( Base::get_mod( 'content_blockquote', 'force_italic' ) ) {
			$css['global']['.aura-formatted-content :where(blockquote)']['font-style'] = 'italic';
		}

		return $css;
	}

	/**
	 * Get font weight and font style from font variant.
	 *
	 * @param string $variant  Font variant.
	 * @return array $property
	 */
	public static function parse_font_variant( $variant ) {
		// Font weight.
		$property['font-weight'] = str_replace( 'italic', '', $variant );
		$property['font-weight'] = ( in_array( $property['font-weight'], [ '', 'regular' ], true ) ) ? '400' : $property['font-weight'];

		// Font style.
		$property['font-style'] = ( false === strpos( $variant, 'italic' ) ) ? 'normal' : 'italic';

		return $property;
	}
}
