<?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 	Header Class
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since		3.5
 * @version		6.6.1
 */

namespace WeCodeArt\Core;

defined( 'ABSPATH' ) || exit;

use WeCodeArt\Config\Traits\Singleton;
use function WeCodeArt\Functions\get_prop;
use function WeCodeArt\Functions\get_json_color;
use function WeCodeArt\Functions\get_lightness_limit;

/**
 * Framework Header
 */
class Header {

	use Singleton;

	/**
	 * Send to Constructor
	 */
	public function init() {
		add_action( 'wecodeart/header',	[ __CLASS__, 'markup' 	] );
		add_filter( 'body_class',		[ $this, 'body_classes'	] );
		add_action( 'wp_head', 			[ $this, 'link_rel' 	] );
	}
	
	/**
	 * Output HEADER markup function Plugin PHP fallback
	 *
	 * @since 	unknown
	 * @version	6.6.1
	 *
	 * @return 	string 
	 */
	public static function markup( $args = [] ) {
		$args 	= wp_json_encode( wp_parse_args( $args, [
			'slug' 		=> 'header',
			'tagName' 	=> 'header',
			'className'	=> 'wp-site-header',
			'style'		=> [
				'position' => [
					'type' => 'sticky',
					'top'  => '0px'
				]
			]
		] ) );

		echo do_blocks( sprintf( '<!-- wp:template-part %s /-->', $args ) );
	}

	/**
	 * Adds wecodeart link rel in head.
	 *
	 * @since	5.7.0
	 * @version 6.3.7
	 *
	 * @return 	void
	 */
	public function link_rel() {
		if( strpos( home_url(), 'wecodeart.com' ) ) return;

		printf( '<link rel="external" href="%s" />' . PHP_EOL, 'https://www.wecodeart.com' ); 
	}

	/**
	 * Adds custom classes to the array of body classes.
	 *
	 * @since	4.0.6
	 * @version 6.3.1
	 *
	 * @param 	array 	$classes Classes for the body element.
	 *
	 * @return 	array
	 */
	public function body_classes( $classes ) {
		// Add a class of hfeed to non-singular pages.
		if ( ! is_singular() && ! is_404() ) {
			$classes[] = 'hfeed';
		}
		
		// Adds a class of group-blog to blogs with more than 1 published author.
		if ( is_multi_author() ) {
			$classes[] = 'group-blog';
		}
		
		// Theme specific classes
		$classes[] = 'theme-' . wecodeart( 'name' );
		$classes[] = is_child_theme() ? 'theme-is-skin' : 'theme-is-base';

		$background	= get_json_color( [ 'styles', 'color', 'background' ] );
		if( $background ) {
			$background = wecodeart( 'styles' )::hex_to_rgb( $background, 1, true );
			$luminance 	= wecodeart( 'styles' )::rgb_luminance( $background );

			$classes[] = ( $luminance < get_lightness_limit() ) ? 'theme-is-dark' : 'theme-is-light';
		} 

		return $classes;
	}
}