<?php
/**
 * Template part for displaying navigation to next/previous post, when applicable
 *
 * @package errigal
 * @since 1.0.0
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Component_Class as Component_Class,
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
	Errigal_Navigation_Class as Navigation_Class,
	Errigal_Shared_Class as Shared_Class,
	Errigal_WP_Hook as WP_Hook,
};
use Errigal\Content\Errigal_Content as Content;
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Validator as Validator,
};

$next_post     = get_next_post();
$prev_post     = get_previous_post();
$has_next_post = Content::is_wp_post( $next_post );
$has_prev_post = Content::is_wp_post( $prev_post );
$html_builder  = Html_Builder::create();

the_post_navigation(
	array(
		'prev_text' => Validator::is_true( $has_prev_post )
			? $html_builder->create_div(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => array(
							Navigation_Class::NAV_POST_TEXT,
							Navigation_Class::PREV,
							Component_Class::OVERLINE,
						),
					),
				)
			)->get_element(
				esc_html__( 'Previous post', 'errigal' )
			) .
			$html_builder->create_div(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => array(
							Component_Class::CARD,
							Shared_Class::SMALL,
							Navigation_Class::PREV,
						),
					),
				)
			)->get_element(
				Content::get_post_thumbnail(
					Component_Class::CARD_IMAGE,
					array(
						Content::THUMBNAIL_POST => $prev_post,
						Content::THUMBNAIL_SHOULD_INCLUDE_LINK => false,
					)
				) .
				$html_builder->create_div(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::CLASS_NAME => Component_Class::CARD_CONTENT,
						),
					)
				)->get_element(
					$html_builder->create_div(
						array(
							Html_Option::ATTRS => array(
								Html_Attr::CLASS_NAME => Component_Class::CARD_TITLE,
							),
						)
					)->get_element(
						apply_filters(
							WP_Hook::THE_TITLE,
							$prev_post->post_title
						)
					)
				)
			)
			: '',
		'next_text' => Validator::is_true( $has_next_post )
			? $html_builder->create_div(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => array(
							Navigation_Class::NAV_POST_TEXT,
							Navigation_Class::NEXT,
							Component_Class::OVERLINE,
						),
					),
				)
			)->get_element(
				esc_html__( 'Next post', 'errigal' )
			) .
			$html_builder->create_div(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => array(
							Component_Class::CARD,
							Shared_Class::SMALL,
							Navigation_Class::NEXT,
						),
					),
				)
			)->get_element(
				$html_builder->create_div(
					array(
						Html_Option::ATTRS => array(
							Html_Attr::CLASS_NAME => Component_Class::CARD_CONTENT,
						),
					)
				)->get_element(
					$html_builder->create_div(
						array(
							Html_Option::ATTRS => array(
								Html_Attr::CLASS_NAME => Component_Class::CARD_TITLE,
							),
						)
					)->get_element(
						apply_filters(
							WP_Hook::THE_TITLE,
							$next_post->post_title
						)
					)
				) .
				Content::get_post_thumbnail(
					Component_Class::CARD_IMAGE,
					array(
						Content::THUMBNAIL_POST => $next_post,
						Content::THUMBNAIL_SHOULD_INCLUDE_LINK => false,
					)
				)
			)
			: '',
	),
);
