<?php
/**
 * Template part for displaying post list
 *
 * @package errigal
 * @since 1.0.0
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Accessibility_Class as Accessibility_Class,
	Errigal_Component_Class as Component_Class,
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
	Errigal_Shared_Class as Shared_Class,
	Errigal_Template_Part as Template_Part,
};
use Errigal\Content\Errigal_Content as Content;
use Errigal\Icon\Errigal_Icon as Icon;
use Errigal\Shared\Errigal_Html_Builder as Html_Builder;

Html_Builder::create()->create_div(
	array(
		Html_Option::ATTRS => array(
			Html_Attr::CLASS_NAME => 'post-list',
		),
	)
)->render_element(
	function ( $html_builder ) {
		if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
			return;
		}

		while ( have_posts() ) {
			the_post();
			get_template_part(
				'template-parts/component/content/article',
				null,
				array(
					/**
					 * Renders article card content.
					 *
					 * @since 1.1.0
					 *
					 * @return void
					 */
					Template_Part::RENDER_CALLBACK => function () use ( $html_builder ) {
						Content::render_post_thumbnail( Component_Class::CARD_IMAGE );
						$html_builder->create_div(
							array(
								Html_Option::ATTRS => array(
									Html_Attr::CLASS_NAME => Component_Class::CARD_CONTENT,
								),
							)
						)->render_element(
							function ( $html_builder ) {
								if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
									return;
								}

								if ( Content::can_show_pinned_sign() ) {
									$html_builder->create_div(
										array(
											Html_Option::ATTRS => array(
												Html_Attr::CLASS_NAME => 'pinned',
											),
										)
									)->render_element(
										function ( $html_builder ) {
											if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
												return;
											}

											Icon::render_icon( Icon::PINNED );
											$html_builder->create_span(
												array(
													Html_Option::ATTRS => array(
														Html_Attr::CLASS_NAME => 'pinned-text',
													),
												)
											)->render_element(
												esc_html_x( 'Pinned', 'Article card', 'errigal' )
											);
										}
									);
								}

								$html_builder->create_h2(
									array(
										Html_Option::ATTRS => array(
											Html_Attr::CLASS_NAME => Component_Class::CARD_TITLE,
										),
									)
								)->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::HREF => get_the_permalink(),
												),
											)
										)->render_element(
											get_the_title()
										);
									}
								);
								$html_builder->create_div(
									array(
										Html_Option::ATTRS => array(
											Html_Attr::CLASS_NAME => Component_Class::CARD_METADATA,
										),
									)
								)->render_element(
									function () {
										get_template_part( 'template-parts/component/shared/posted-on' );
									}
								);
								the_excerpt();
								$html_builder->create_a(
									array(
										Html_Option::ATTRS => array(
											Html_Attr::CLASS_NAME => array(
												'continue-reading',
												Component_Class::BUTTON,
												Component_Class::OUTLINED,
											),
											Html_Attr::HREF => get_the_permalink(),
										),
									)
								)->render_element(
									function ( $html_builder ) {
										if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
											return;
										}

										printf(
											/* translators: %s: Name of current post. */
											esc_html__( 'Continue reading %s', 'errigal' ),
											wp_kses_post(
												$html_builder->create_span(
													array(
														Html_Option::ATTRS => array(
															Html_Attr::CLASS_NAME => Accessibility_Class::SCREEN_READER_TEXT,
														),
													)
												)->get_element(
													esc_html(
														wp_strip_all_tags( get_the_title() )
													)
												)
											)
										);
									}
								);
								get_template_part( 'template-parts/component/shared/categories' );
							}
						);
					},
					Template_Part::CSS_CLASS       => array(
						Component_Class::CARD,
						Shared_Class::LARGE,
					),
				)
			);
		}
	}
);
