<?php
/**
 * Template part for displaying breadcrumb
 *
 * @package errigal
 * @since 1.2.0
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
	Errigal_Navigation_Class as Navigation_Class,
	Errigal_Shared_Class as Shared_Class,
};
use Errigal\Customiser\Errigal_Custom_Breadcrumb as Breadcrumb;
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Validator as Validator,
};

if ( ! Breadcrumb::can_show() ) {
	if ( Breadcrumb::should_add_in_customise_preview() ) {
		Html_Builder::create()->create_nav(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => array(
						Navigation_Class::BREADCRUMB,
						Shared_Class::HIDE,
					),
				),
			)
		)->render_element();
	}

	return;
}

$args = Breadcrumb::get_args();

if ( ! Validator::is_associative_array( $args ) ) {
	return;
}

Html_Builder::create( $args )->create_nav(
	array(
		Html_Option::ATTRS => array(
			Html_Attr::CLASS_NAME => Navigation_Class::BREADCRUMB,
			Html_Attr::ARIA_LABEL => Breadcrumb::get_name(),
		),
	)
)->render_element(
	function ( $html_builder ) {
		if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
			return;
		}

		$html_builder->create_ol(
			array(
				Html_Option::ATTRS => array(
					Html_Attr::CLASS_NAME => array(
						'errigal-breadcrumb__list',
						Breadcrumb::get_has_ancestor_class(
							$html_builder->get_arg( Breadcrumb::ANCESTOR_COUNT )
						),
					),
				),
			)
		)->render_element(
			function ( $html_builder ) {
				if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
					return;
				}

				$html_builder->create_li(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::CLASS_NAME => array(
								Navigation_Class::BREADCRUMB_LIST_ITEM,
								'errigal-breadcrumb__home',
							),
						),
					)
				)->render_element(
					function ( $html_builder ) {
						if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
							return;
						}

						$html_builder->create_a(
							array(
								Html_Option::ATTRS => array(
									Html_Attr::CLASS_NAME => Navigation_Class::BREADCRUMB_LINK,
									Html_Attr::HREF       => $html_builder->get_arg(
										Breadcrumb::HOME_ITEM,
										Breadcrumb::ITEM_URL
									),
								),
							)
						)->render_element(
							esc_html(
								$html_builder->get_arg(
									Breadcrumb::HOME_ITEM,
									Breadcrumb::ITEM_TEXT
								)
							)
						);
						Breadcrumb::render_separator();
					}
				);

				foreach ( $html_builder->get_arg( Breadcrumb::ANCESTOR_ITEMS ) as $index => $item ) {
					if ( ! Validator::is_associative_array( $item ) ) {
						continue;
					}

					if ( 0 === $index ) {
						$html_builder->create_li(
							array(
								Html_Option::ATTRS => array(
									Html_Attr::CLASS_NAME => array(
										Navigation_Class::BREADCRUMB_LIST_ITEM,
										'errigal-breadcrumb__reveal',
									),
								),
							)
						)->render_element(
							function ( $html_builder ) {
								if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
									return;
								}

								$ancestor_count = $html_builder->get_arg( Breadcrumb::ANCESTOR_COUNT );

								$html_builder->create_button(
									array(
										Html_Option::ATTRS => array(
											Html_Attr::CLASS_NAME => 'errigal-breadcrumb__reveal-button',
											Html_Attr::TYPE => Html_Attr::VALUE_BUTTON,
											Html_Attr::ARIA_LABEL => Breadcrumb::get_reveal_button_aria_label( $ancestor_count ),
											Html_Attr::ARIA_CONTROLS => Breadcrumb::get_reveal_button_aria_controls( $ancestor_count ),
											Html_Attr::ARIA_EXPANDED => false,
										),
									)
								)->render_element( '&hellip;' );
								Breadcrumb::render_separator();
							}
						);
					}

					$html_builder->create_li(
						array(
							Html_Option::ATTRS => array(
								Html_Attr::ID         => Breadcrumb::get_ancestor_id( $index ),
								Html_Attr::CLASS_NAME => array(
									Navigation_Class::BREADCRUMB_LIST_ITEM,
									'errigal-breadcrumb__ancestor',
								),
							),
						)
					)->render_element(
						function ( $html_builder ) use ( $item ) {
							if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
								return;
							}

							$html_builder->create_a(
								array(
									Html_Option::ATTRS => array(
										Html_Attr::CLASS_NAME => Navigation_Class::BREADCRUMB_LINK,
										Html_Attr::HREF => $item[ Breadcrumb::ITEM_URL ],
									),
								)
							)->render_element(
								esc_html(
									$item[ Breadcrumb::ITEM_TEXT ]
								)
							);
							Breadcrumb::render_separator();
						}
					);
				}

				$html_builder->create_li(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::CLASS_NAME => array(
								Navigation_Class::BREADCRUMB_LIST_ITEM,
								'errigal-breadcrumb__current',
							),
						),
					)
				)->render_element(
					function ( $html_builder ) {
						if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
							return;
						}

						$html_builder->create_span(
							array(
								Html_Option::ATTRS => array(
									Html_Attr::ARIA_CURRENT => 'page',
								),
							)
						)->render_element(
							esc_html(
								$html_builder->get_arg(
									Breadcrumb::CURRENT_ITEM,
									Breadcrumb::ITEM_TEXT
								)
							)
						);
					}
				);
			}
		);
	}
);
