<?php
/**
 * Footer widget areas component.
 *
 * @package Kutby
 * @since   2.7.0
 */

declare(strict_types=1);

namespace Kutby\Footer\Components;

use Kutby\Footer\Contracts\FooterComponentInterface;
use Kutby\Footer\Support\FooterContext;

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

/**
 * Class WidgetArea
 */
final class WidgetArea implements FooterComponentInterface {

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

		$columns = $ctx->columns();
		$class   = isset( $args['class'] ) ? (string) $args['class'] : 'kutby-footer__grid';
		$class  .= ' kutby-footer__grid--' . $columns;

		$has_any = false;
		for ( $i = 1; $i <= $columns; $i++ ) {
			if ( is_active_sidebar( 'footer-' . $i ) ) {
				$has_any = true;
				break;
			}
		}

		if ( ! $has_any ) {
			return;
		}
		?>
		<div class="<?php echo esc_attr( $class ); ?>">
			<?php
			for ( $i = 1; $i <= $columns; $i++ ) {
				if ( is_active_sidebar( 'footer-' . $i ) ) {
					echo '<div class="kutby-footer__column kutby-footer__column--' . esc_attr( (string) $i ) . '">';
					dynamic_sidebar( 'footer-' . $i );
					echo '</div>';
				}
			}
			?>
		</div>
		<?php
	}
}
