<?php
/**
 * Estimated reading time.
 *
 * @package Kutby
 * @since   2.9.0
 */

declare(strict_types=1);

namespace Kutby\Cards\Parts;

use Kutby\Cards\Contracts\CardPartInterface;
use Kutby\Cards\Support\CardContext;

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

/**
 * Class ReadingTime
 */
final class ReadingTime implements CardPartInterface {

	/**
	 * {@inheritdoc}
	 */
	public static function render( CardContext $ctx, array $args = array() ): void {
		if ( ! $ctx->bool( 'show_reading_time' ) ) {
			return;
		}

		$content = (string) $ctx->post()->post_content;
		$words   = str_word_count( wp_strip_all_tags( $content ) );
		$minutes = max( 1, (int) ceil( $words / 200 ) );
		?>
		<span class="kutby-card__reading-time">
			<?php echo esc_html( sprintf( '%d دقيقة قراءة', $minutes ) ); ?>
		</span>
		<?php
	}
}
