<?php
/**
 * Trending posts list.
 *
 * @package Kutby
 * @since   2.8.0
 */

declare(strict_types=1);

namespace Kutby\Hero\Components;

use Kutby\Hero\Contracts\HeroComponentInterface;
use Kutby\Hero\Support\HeroContext;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class TrendingList
 */
final class TrendingList implements HeroComponentInterface {

	/**
	 * {@inheritdoc}
	 */
	public static function render( HeroContext $ctx, array $args = array() ): void {
		$limit = isset( $args['limit'] ) ? (int) $args['limit'] : 5;
		$posts = $ctx->trending( $limit );
		$title = isset( $args['title'] ) ? (string) $args['title'] : 'الأكثر تداولاً';

		if ( empty( $posts ) ) {
			return;
		}
		?>
		<div class="kutby-hero-list kutby-hero-list--trending">
			<h2 class="kutby-hero-list__heading"><?php echo esc_html( $title ); ?></h2>
			<ol class="kutby-hero-list__items">
				<?php foreach ( $posts as $i => $post ) : ?>
					<li class="kutby-hero-list__item">
						<span class="kutby-hero-list__index" aria-hidden="true"><?php echo esc_html( (string) ( $i + 1 ) ); ?></span>
						<div class="kutby-hero-list__content">
							<?php CategoryBadge::render( $ctx, array( 'post' => $post ) ); ?>
							<a class="kutby-hero-list__link" href="<?php echo esc_url( get_permalink( $post ) ); ?>">
								<?php echo esc_html( get_the_title( $post ) ); ?>
							</a>
							<?php ArticleMeta::render( $ctx, array( 'post' => $post ) ); ?>
						</div>
					</li>
				<?php endforeach; ?>
			</ol>
		</div>
		<?php
	}
}
