<?php
/**
 * Template part for displaying page header
 *
 * @package errigal
 * @since 1.0.0
 *
 * @param array $args {
 *     Associative array of arguments for page header.
 *
 *     @type string $Icon        Optional. Icon. Default empty string.
 *     @type string $heading     Heading. Default empty string.
 *     @type string $description Optional. Description. Default empty string.
 * }
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Accessibility_Class as Accessibility_Class,
	Errigal_Header_Class as Header_Class,
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
	Errigal_Template_Part as Template_Part,
};
use Errigal\Content\Errigal_Content as Content;
use Errigal\Header\Errigal_Header as Header;
use Errigal\Icon\Errigal_Icon as Icon;
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Validator as Validator,
};

$args = $args ?? array();
$args = wp_parse_args(
	$args,
	array(
		Template_Part::ICON        => '',
		Template_Part::HEADING     => '',
		Template_Part::DESCRIPTION => '',
	)
);

if ( ! Validator::is_non_empty_string( $args[ Template_Part::HEADING ] ) ) {
	return;
}

$html_builder = Html_Builder::create( $args );

if ( Header::is_screen_reader_only_page_header() ) {
	if ( ! Content::has_h1_heading() ) {
		$html_builder->create_header(
			array(
				Html_Option::ATTRS => Content::can_edit()
					? array(
						Html_Attr::CLASS_NAME => 'page-header__hidden',
					)
					: null,
			),
		)->render_element(
			function ( $html_builder ) {
				if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
					return;
				}

				$html_builder->create_h1(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::CLASS_NAME => array(
								'homepage-title',
								Accessibility_Class::SCREEN_READER_TEXT,
							),
						),
					)
				)->render_element(
					esc_html(
						$html_builder->get_arg( Template_Part::HEADING )
					)
				);
				get_template_part( 'template-parts/component/header/edit-post-link' );
			}
		);

		return;
	}

	if ( Content::can_edit() ) {
		$html_builder->create_header(
			array(
				Html_Option::ATTRS => Content::can_edit()
					? array(
						Html_Attr::CLASS_NAME => array(
							Header_Class::PAGE_HEADER,
							'page-header__no-heading',
						),
					)
					: null,
			)
		)->render_element(
			function () {
				get_template_part( 'template-parts/component/header/edit-post-link' );
			}
		);
	}

	return;
}

$html_builder->create_header(
	array(
		Html_Option::ATTRS => array(
			Html_Attr::CLASS_NAME => Header_Class::PAGE_HEADER,
		),
	)
)->render_element(
	function ( $html_builder ) {
		if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
			return;
		}

		if ( Icon::is_supported_icon( $html_builder->get_arg( Template_Part::ICON ) ) ) {
			$html_builder->create_div(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => 'page-icon',
					),
				)
			)->render_element(
				Icon::get_icon(
					$html_builder->get_arg( Template_Part::ICON ),
					Icon::SIZE_XL
				)
			);
		}

		$html_builder->create_h1(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => 'page-title',
				),
			)
		)->render_element(
			$html_builder->get_arg( Template_Part::HEADING )
		);

		if ( Validator::is_non_empty_string( $html_builder->get_arg( Template_Part::DESCRIPTION ) ) ) {
			$html_builder->create_div(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => 'page-description',
					),
				)
			)->render_element(
				wpautop( $html_builder->get_arg( Template_Part::DESCRIPTION ) )
			);
		}

		get_template_part( 'template-parts/component/header/edit-post-link' );
	}
);
