<?php
/**
 * Latest 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 LatestPosts
 */
final class LatestPosts implements HeroComponentInterface {

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

		if ( empty( $posts ) ) {
			return;
		}
		?>
		<div class="kutby-hero-list kutby-hero-list--latest">
			<h2 class="kutby-hero-list__heading"><?php echo esc_html( $title ); ?></h2>
			<ul class="kutby-hero-list__items">
				<?php foreach ( $posts as $post ) : ?>
					<li class="kutby-hero-list__item">
						<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 ) ); ?>
					</li>
				<?php endforeach; ?>
			</ul>
		</div>
		<?php
	}
}
