<?php
/**
 * Uniform hero article grid.
 *
 * @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 HeroGrid
 */
final class HeroGrid implements HeroComponentInterface {

	/**
	 * {@inheritdoc}
	 */
	public static function render( HeroContext $ctx, array $args = array() ): void {
		$posts = $args['posts'] ?? $ctx->grid_posts();
		if ( ! is_array( $posts ) || empty( $posts ) ) {
			LoadingState::render( $ctx );
			return;
		}

		$class = isset( $args['class'] ) ? (string) $args['class'] : 'kutby-hero-grid';
		?>
		<div class="<?php echo esc_attr( $class ); ?>">
			<?php
			foreach ( $posts as $index => $post ) {
				ArticleCard::render(
					$ctx,
					array(
						'post'  => $post,
						'class' => 'kutby-hero-card' . ( 0 === (int) $index ? ' kutby-hero-card--lead' : '' ),
					)
				);
			}
			?>
		</div>
		<?php
	}
}
