<?php
/**
 * WeCodeArt Framework
 *
 * WARNING: This file is part of the core WeCodeArt Framework. DO NOT edit this file under any circumstances.
 * Please do all modifications in the form of a child theme.
 *
 * @package		WeCodeArt Framework
 * @subpackage  Gutenberg CSS Frontend
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since		5.0.0
 * @version		6.6.6
 */

namespace WeCodeArt\Gutenberg\Styles\Blocks;

defined( 'ABSPATH' ) || exit;

use WeCodeArt\Singleton;
use WeCodeArt\Gutenberg\Styles\Processor;
use function WeCodeArt\Functions\{ get_prop, get_lightness_limit };

/**
 * Block CSS Processor
 */
class Button extends Processor {
	/**
	 * Parses an output and creates the styles array for it.
	 *
	 * @return 	void
	 */
	protected function process_style(): void {
		$is_outline = str_contains( get_prop( $this->attrs, [ 'className' ], '' ), 'is-style-outline' );
		$palette 	= wecodeart_json( [ 'settings', 'color', 'palette' ], [] );

		// Inline Style
		$declarations = [];

		// Autodetect color lightness if no text color but has background.
		if( get_prop( $this->attrs, 'textColor' ) === null && get_prop( $this->attrs, 'backgroundColor' ) ) {
			$selected	= wp_list_filter( $palette, [ 'slug' => get_prop( $this->attrs, 'backgroundColor' ) ] );

			$is_dark 	= false;
			$rgba 		= wecodeart( 'styles' )::hex_to_rgb( get_prop( current( $selected ), 'color' ), 1, true );

			if( is_array( $rgba ) ) {
				$luminance	= wecodeart( 'styles' )::rgb_luminance( $rgba );
				$is_dark 	= $luminance < get_lightness_limit();
			}

			$declarations['color'] = $is_dark ? 'var(--wp--preset--color--white)' : 'var(--wp--preset--color--black)';
		}

		// Autodetect color lightness if only text color for outline.
		if( get_prop( $this->attrs, 'textColor' ) && $is_outline ) {
			$selected	= wp_list_filter( $palette, [ 'slug' => get_prop( $this->attrs, 'textColor' ) ] );

			$is_dark 	= false;
			$rgba 		= wecodeart( 'styles' )::hex_to_rgb( get_prop( current( $selected ), 'color' ), 1, true );
			
			if( is_array( $rgba ) ) {
				$luminance	= wecodeart( 'styles' )::rgb_luminance( $rgba );
				$is_dark 	= $luminance < get_lightness_limit();
			}

			$declarations['--wp--color--hover'] = $is_dark ? 'var(--wp--preset--color--white)' : 'var(--wp--preset--color--black)';
		}

		// Autodetect color lightness for custom color.
		if( $css_style = get_prop( $this->attrs, 'style' ) ) {
			// Color
			if( $color = get_prop( $css_style, 'color' ) ) {
				// Text
				if( $text = get_prop( $color, 'text' ) ) {
 					$value	= wecodeart( 'styles' )::hex_to_rgb( $text, 1, true );

					if( $is_outline ) {
						$luminance	= wecodeart( 'styles' )::rgb_luminance( $value );
						$is_dark 	= $luminance < get_lightness_limit();

						$declarations['--wp--color--hover'] = $is_dark ? 'var(--wp--preset--color--white)' : 'var(--wp--preset--color--black)';
					}

					$declarations['--wp--color--rgb'] 	= join( ', ', wp_array_slice_assoc( $value, [ 'r', 'g', 'b' ] ) );
					$declarations['color'] 				= $text;
				}
				// Background
				if( $background = get_prop( $color, 'background' ) ) {
 					$value		= wecodeart( 'styles' )::hex_to_rgb( $background, 1, true );
					$luminance	= wecodeart( 'styles' )::rgb_luminance( $value );
					$is_dark 	= $luminance < get_lightness_limit();

					$declarations['color']	= $is_dark ? 'var(--wp--preset--color--white)' : 'var(--wp--preset--color--black)';
				}
			}
		}

		if( ! empty( $declarations ) ) {
			$this->add_declarations( $declarations );
		}

		parent::process_style();
	}

	/**
	 * Additional selectors to remove styles from.
	 *
	 * @return 	array
	 */
	public function remove_style(): array {
		return [ 
			[ 'tag_name' => 'a' ]
		];
	}
}