<?php
/**
 * Logo grid template.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

if ( empty( $items ) ) {
	return;
}

$classes = array(
	'rsmspark-cw',
	'rsmspark-cw-logo-grid',
	'rsmspark-cw-logo-grid--cols-' . sanitize_html_class( (string) $columns ),
	'rsmspark-cw-logo-grid--' . sanitize_html_class( (string) $layout ),
);

if ( ! empty( $hover_overlay ) ) {
	$classes[] = 'rsmspark-cw-logo-grid--hover-overlay';
}
if ( ! empty( $grayscale ) ) {
	$classes[] = 'rsmspark-cw-logo-grid--grayscale';
}
if ( ! empty( $container_border ) ) {
	$classes[] = 'rsmspark-cw-logo-grid--has-container-border';
}

$render_item = static function ( $item ) use ( $hover_overlay ) {
	$tag = ! empty( $item['url'] ) ? 'a' : 'div';
	?>
	<<?php echo esc_html( $tag ); ?>
		class="rsmspark-cw-logo-grid__item"
		<?php if ( ! empty( $item['url'] ) ) : ?>
			href="<?php echo esc_url( $item['url'] ); ?>"
			rel="noopener noreferrer"
		<?php endif; ?>
		<?php if ( 'a' === $tag && ! empty( $item['name'] ) ) : ?>
			aria-label="<?php echo esc_attr( $item['name'] ); ?>"
		<?php endif; ?>
	>
		<?php if ( ! empty( $item['image_url'] ) ) : ?>
			<img class="rsmspark-cw-logo-grid__image" src="<?php echo esc_url( $item['image_url'] ); ?>" alt="<?php echo esc_attr( $item['alt'] ?? '' ); ?>" loading="lazy" decoding="async" />
		<?php elseif ( ! empty( $item['name'] ) ) : ?>
			<span class="rsmspark-cw-logo-grid__fallback"><?php echo esc_html( $item['name'] ); ?></span>
		<?php endif; ?>
		<?php if ( ! empty( $hover_overlay ) && ! empty( $item['name'] ) ) : ?>
			<span class="rsmspark-cw-logo-grid__name"><?php echo esc_html( $item['name'] ); ?></span>
		<?php endif; ?>
	</<?php echo esc_html( $tag ); ?>>
	<?php
};
?>
<section
	class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"
	style="<?php echo esc_attr( $style_vars ); ?>"
	data-rsmspark-logo-grid
>
	<?php if ( ! empty( $title ) ) : ?>
		<h2 class="rsmspark-cw-logo-grid__title"><?php echo esc_html( $title ); ?></h2>
	<?php endif; ?>
	<div class="rsmspark-cw-logo-grid__track">
		<?php foreach ( $items as $item ) : ?>
			<?php $render_item( $item ); ?>
		<?php endforeach; ?>
	</div>
</section>
