<?php
/**
 * Template part for displaying page links for paginated posts
 *
 * @package errigal
 * @since 1.0.0
 *
 * @global int $page     Page number of single post.
 * @global int $numpages Number of pages in post.
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Accessibility_Class as Accessibility_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;

global $page, $numpages;

if (
	! isset( $page ) ||
	! isset( $numpages ) ||
	! is_int( $page ) ||
	! is_int( $numpages )
) {
	return;
}

$html_builder         = Html_Builder::create();
$page_links           = $html_builder->create_nav(
	array(
		Html_Option::ATTRS => array(
			Html_Attr::CLASS_NAME => 'page-links',
			Html_Attr::ARIA_LABEL => esc_attr__( 'Page', 'errigal' ),
		),
	)
);
$page_links_container = $html_builder->create_div(
	array(
		Html_Option::ATTRS => array(
			Html_Attr::CLASS_NAME => 'page-links__container',
		),
	)
);
$page_link_container  = $html_builder->create_span(
	array(
		Html_Option::ATTRS => array(
			Html_Attr::CLASS_NAME => 'page-numbers',
		),
	)
);

wp_link_pages(
	array(
		'before'           => $page_links->get_open_tag() .
			$html_builder->create_div(
				array(
					Html_Option::ATTRS => array(
						Html_Attr::CLASS_NAME => 'page-links__label',
					),
				)
			)->get_element(
				esc_html(
					sprintf(
						/* translators: 1: Current page number. 2: The total number of pages in the post. */
						__( 'Page %1$u of %2$u', 'errigal' ),
						number_format_i18n( $page ),
						number_format_i18n( $numpages )
					)
				)
			) .
			$page_links_container->get_open_tag(),
		'after'            => $page_links_container->get_close_tag() .
			$page_links->get_close_tag(),
		'link_before'      => $page_link_container->get_open_tag(),
		'link_after'       => $page_link_container->get_close_tag(),
		'next_or_number'   => 'next_and_number',
		'nextpagelink'     => 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__( 'Next page', 'errigal' )
			),
		'previouspagelink' => 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__( 'Previous page', 'errigal' )
			),
	)
);
