<?php
/**
 * Template part for displaying content of index
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package errigal
 * @since 1.1.0
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Admin\Errigal_Admin as Admin;
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_Template_Part as Template_Part,
};
use Errigal\Shared\Errigal_Html_Builder as Html_Builder;

Html_Builder::create()->create_header()->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 => Accessibility_Class::SCREEN_READER_TEXT,
				),
			)
		)->render_element(
			esc_html__( 'The latest posts', 'errigal' )
		);
	}
);

if ( have_posts() ) {
	get_template_part( 'template-parts/component/content/post-list' );
	get_template_part( 'template-parts/component/content/posts-pagination' );

	return;
}

get_template_part(
	'template-parts/component/content/no-entry-section',
	null,
	array(
		/**
		 * Renders no entry section content.
		 *
		 * @since 1.1.0
		 *
		 * @return void
		 */
		Template_Part::RENDER_CALLBACK => function () {
			get_template_part(
				'template-parts/component/content/page-content',
				null,
				array(
					/**
					 * Renders page content.
					 *
					 * @since 1.1.0
					 *
					 * @return void
					 */
					Template_Part::RENDER_CALLBACK => function () {
						$html_builder = Html_Builder::create();
						$current_user_can_publish_posts = current_user_can( 'publish_posts' );

						$html_builder->create_h2()->render_element(
							esc_html(
								$current_user_can_publish_posts
									? __( 'No posts published yet.', 'errigal' )
									: __( 'Coming soon!', 'errigal' )
							)
						);
						$html_builder->create_p()->render_element(
							esc_html(
								$current_user_can_publish_posts
									? __( 'Are you ready to create your first post?', 'errigal' )
									: __( 'Come back later to check out new posts.', 'errigal' )
							)
						);

						if ( $current_user_can_publish_posts ) {
							$html_builder->create_a(
								array(
									Html_Option::ATTRS => array(
										Html_Attr::HREF => Admin::get_new_post_url(),
									),
								)
							)->render_element(
								function ( $html_builder ) {
									if ( ! Html_Builder::is_html_builder( $html_builder ) ) {
										return;
									}

									$html_builder->create_div(
										array(
											Html_Option::ATTRS => array(
												Html_Attr::CLASS_NAME => array(
													Component_Class::BUTTON,
													Component_Class::CONTAINED,
												),
											),
										)
									)->render_element(
										esc_html__( 'Add new post', 'errigal' )
									);
								}
							);
						}
					},
				)
			);
		},
	)
);
