<?php
/**
 * Featured article (large).
 *
 * @package Kutby
 * @since   2.8.0
 */

declare(strict_types=1);

namespace Kutby\Hero\Components;

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

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

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

	/**
	 * {@inheritdoc}
	 */
	public static function render( HeroContext $ctx, array $args = array() ): void {
		$post = $args['post'] ?? $ctx->featured();
		if ( ! $post instanceof WP_Post ) {
			LoadingState::render( $ctx );
			return;
		}

		$class = isset( $args['class'] ) ? (string) $args['class'] : 'kutby-hero-featured';
		?>
		<article <?php post_class( $class, $post ); ?>>
			<a class="kutby-hero-featured__media-link" href="<?php echo esc_url( get_permalink( $post ) ); ?>">
				<?php
				BackgroundImage::render(
					$ctx,
					array(
						'post'  => $post,
						'lazy'  => false,
						'size'  => 'large',
						'class' => 'kutby-hero-media kutby-hero-media--featured',
					)
				);
				?>
			</a>
			<div class="kutby-hero-featured__body">
				<?php CategoryBadge::render( $ctx, array( 'post' => $post ) ); ?>
				<h2 class="kutby-hero-featured__title">
					<a href="<?php echo esc_url( get_permalink( $post ) ); ?>"><?php echo esc_html( get_the_title( $post ) ); ?></a>
				</h2>
				<?php ArticleMeta::render( $ctx, array( 'post' => $post ) ); ?>
				<?php if ( $ctx->bool( 'show_excerpt' ) ) : ?>
					<p class="kutby-hero-featured__excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt( $post ), 28 ) ); ?></p>
				<?php endif; ?>
				<?php ReadMoreButton::render( $ctx, array( 'post' => $post ) ); ?>
			</div>
		</article>
		<?php
	}
}
