<?php
/**
 * Template for displaying comments
 *
 * This is the template that displays area of page that contains both current comments
 * and comment form.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @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,
};
use Errigal\Icon\Errigal_Icon as Icon;
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Utility as Utility,
};

/*
 * Returns early without loading comments, if current post is protected by password
 * and visitor has not yet entered password.
 */
if ( post_password_required() ) {
	return;
}

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

		if ( have_comments() ) {
			$html_builder->create_h2(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => 'comments-title',
					),
				)
			)->render_element(
				function () {
					$errigal_comment_count = get_comments_number();

					if ( '1' === $errigal_comment_count ) {
						esc_html_e( '1 comment', 'errigal' );

						return;
					}

					printf(
						/* translators: 1: Comment count number. */
						esc_html( _nx( '%1$s comment', '%1$s comments', $errigal_comment_count, 'Comments title', 'errigal' ) ),
						esc_html( number_format_i18n( $errigal_comment_count ) )
					);
				}
			);
			$html_builder->create_ol(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => array(
							'comment-list',
							get_option( 'show_avatars' )
								? 'has-avatar'
								: null,
						),
					),
				)
			)->render_element(
				wp_list_comments(
					array(
						'style'      => 'ol',
						'short_ping' => true,
						'echo'       => false,
					)
				)
			);

			the_comments_pagination(
				array(
					'mid_size'  => 0,
					'prev_text' => Icon::get_icon( Icon::PREVIOUS ) .
						$html_builder->create_span(
							array(
								Html_Option::ATTRS => array(
									Html_Attr::CLASS_NAME => Accessibility_Class::SCREEN_READER_TEXT,
								),
							)
						)->get_element(
							esc_html__( 'Older comments', 'errigal' )
						),
					'next_text' => Icon::get_icon( Icon::NEXT ) .
						$html_builder->create_span(
							array(
								Html_Option::ATTRS => array(
									Html_Attr::CLASS_NAME => Accessibility_Class::SCREEN_READER_TEXT,
								),
							)
						)->get_element(
							esc_html__( 'Newer comments', 'errigal' )
						),
				)
			);

			if ( ! comments_open() ) {
				$html_builder->create_p(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::CLASS_NAME => 'no-comments',
						),
					)
				)->render_element(
					esc_html__( 'Comments are closed.', 'errigal' )
				);
			}
		}

		comment_form(
			array(
				'logged_in_as' => null,
				'class_submit' => Utility::get_class(
					Component_Class::BUTTON,
					Component_Class::CONTAINED
				),
				'title_reply'  => esc_html__( 'Leave a comment', 'errigal' ),
			)
		);
	}
);
