<?php
/**
 * How much of a table you are looking at, and how to see the rest.
 *
 * One partial for the message log, the opt-out list, the consent list, the audit
 * trail and campaign history — five hand-written Newer/Older blocks would drift
 * the way the four empty states and the five tables did.
 *
 * @package TextTango
 *
 * @var array<string, mixed> $args {
 *     @type \TextTango\Support\Paging $paging The page being rendered.
 *     @type int                       $total  Rows in total.
 *     @type int                       $shown  Rows on this page.
 *     @type string                    $url    Screen address, without a page argument.
 *     @type array<string, string>     $keep   Query arguments to carry across pages.
 *     @type string                    $arg    Query argument naming this table's page.
 * }
 *
 * `$arg` is per-table and not optional in practice: the Logs screen renders the
 * message log and the audit trail, and the Consent screen the opt-out and
 * consent lists. Sharing one page argument between two tables on a screen would
 * page both at once, and `keep` is what stops paging one from resetting the
 * other.
 */

declare( strict_types=1 );

defined( 'ABSPATH' ) || exit;

use TextTango\Support\Paging;

$texttango_paging = $args['paging'] ?? null;

if ( ! $texttango_paging instanceof Paging ) {
	return;
}

$texttango_total = (int) ( $args['total'] ?? 0 );
$texttango_shown = (int) ( $args['shown'] ?? 0 );
$texttango_pages = $texttango_paging->pages( $texttango_total );
$texttango_page  = $texttango_paging->page();
$texttango_base  = (string) ( $args['url'] ?? '' );
$texttango_keep  = (array) ( $args['keep'] ?? array() );
$texttango_arg   = (string) ( $args['arg'] ?? Paging::ARG );

if ( $texttango_shown < 1 ) {
	return;
}

/**
 * The address of one page, carrying whatever the screen wants kept.
 *
 * @param int $page Page to point at.
 */
$texttango_page_url = static function ( int $page ) use ( $texttango_base, $texttango_keep, $texttango_paging, $texttango_arg ): string {
	return add_query_arg(
		array_merge( $texttango_keep, $texttango_paging->query_args( $page, $texttango_arg ) ),
		$texttango_base
	);
};
?>
<div class="tt-mt-4 tt-flex tt-flex-wrap tt-items-center tt-justify-between tt-gap-3">
	<p class="tt-m-0 tt-text-sm tt-text-slate-500">
		<?php
		printf(
			/* translators: 1: first row shown, 2: last row shown, 3: total rows. Each is already formatted. */
			esc_html__( 'Showing %1$s to %2$s of %3$s results', 'texttango' ),
			'<strong class="tt-font-medium tt-text-slate-700">' . esc_html( number_format_i18n( $texttango_paging->first() ) ) . '</strong>',
			'<strong class="tt-font-medium tt-text-slate-700">' . esc_html( number_format_i18n( $texttango_paging->last( $texttango_shown ) ) ) . '</strong>',
			'<strong class="tt-font-medium tt-text-slate-700">' . esc_html( number_format_i18n( $texttango_total ) ) . '</strong>'
		);
		?>
	</p>

	<?php if ( $texttango_pages > 1 ) : ?>
		<nav class="tt-pager" aria-label="<?php esc_attr_e( 'Pagination', 'texttango' ); ?>">
			<?php if ( $texttango_page > 1 ) : ?>
				<a class="tt-pager-step" href="<?php echo esc_url( $texttango_page_url( $texttango_page - 1 ) ); ?>" rel="prev">
					<span class="screen-reader-text"><?php esc_html_e( 'Previous page', 'texttango' ); ?></span>
					<span aria-hidden="true">&lsaquo;</span>
				</a>
			<?php else : ?>
				<span class="tt-pager-step tt-pager-off" aria-hidden="true">&lsaquo;</span>
			<?php endif; ?>

			<?php foreach ( $texttango_paging->window( $texttango_total ) as $texttango_slot ) : ?>
				<?php if ( null === $texttango_slot ) : ?>
					<span class="tt-pager-gap" aria-hidden="true">&hellip;</span>
				<?php elseif ( $texttango_slot === $texttango_page ) : ?>
					<span class="tt-pager-page tt-pager-current" aria-current="page">
						<?php echo esc_html( number_format_i18n( $texttango_slot ) ); ?>
					</span>
				<?php else : ?>
					<a class="tt-pager-page" href="<?php echo esc_url( $texttango_page_url( $texttango_slot ) ); ?>">
						<span class="screen-reader-text">
							<?php
							/* translators: %s: page number. */
							printf( esc_html__( 'Page %s', 'texttango' ), esc_html( number_format_i18n( $texttango_slot ) ) );
							?>
						</span>
						<span aria-hidden="true"><?php echo esc_html( number_format_i18n( $texttango_slot ) ); ?></span>
					</a>
				<?php endif; ?>
			<?php endforeach; ?>

			<?php if ( $texttango_page < $texttango_pages ) : ?>
				<a class="tt-pager-step" href="<?php echo esc_url( $texttango_page_url( $texttango_page + 1 ) ); ?>" rel="next">
					<span class="screen-reader-text"><?php esc_html_e( 'Next page', 'texttango' ); ?></span>
					<span aria-hidden="true">&rsaquo;</span>
				</a>
			<?php else : ?>
				<span class="tt-pager-step tt-pager-off" aria-hidden="true">&rsaquo;</span>
			<?php endif; ?>
		</nav>
	<?php endif; ?>
</div>
