<?php
/**
 * Outer hero section wrapper.
 *
 * @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 HeroSection
 */
final class HeroSection implements HeroComponentInterface {

	/**
	 * Open section.
	 *
	 * @param HeroContext          $ctx  Context.
	 * @param array<string, mixed> $args Args.
	 * @return void
	 */
	public static function open( HeroContext $ctx, array $args = array() ): void {
		$layout = isset( $args['layout'] ) ? sanitize_html_class( (string) $args['layout'] ) : sanitize_html_class( $ctx->layout_id() );
		$width  = $ctx->container_width();
		$space  = $ctx->spacing();
		$label  = isset( $args['label'] ) ? (string) $args['label'] : 'القسم الرئيسي';

		$classes = array(
			'kutby-hero',
			'kutby-hero--' . $layout,
			'kutby-hero--space-' . $space,
			'kutby-hero--width-' . $width,
		);
		?>
		<section class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>" aria-label="<?php echo esc_attr( $label ); ?>">
			<div class="kutby-container kutby-hero__container">
		<?php
	}

	/**
	 * Close section.
	 *
	 * @return void
	 */
	public static function close(): void {
		echo '</div></section>';
	}

	/**
	 * {@inheritdoc}
	 */
	public static function render( HeroContext $ctx, array $args = array() ): void {
		self::open( $ctx, $args );
		if ( isset( $args['callback'] ) && is_callable( $args['callback'] ) ) {
			$args['callback']( $ctx );
		}
		self::close();
	}
}
