<?php

defined( 'ABSPATH' ) || exit;


// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals -- Block render: core-injected $attributes, $content, and $block in this scope.
/**
 * Render callback: team avatar image.
 *
 * WordPress loads this via `ob_start(); require; return ob_get_clean();` — output must be echoed, not returned.
 *
 * @package clanbite
 */

$team_id = clanbite_team_single_block_team_id( $block );

$avatar_preset = isset( $attributes['avatarPreset'] ) ? sanitize_key( (string) $attributes['avatarPreset'] ) : 'large';
if ( ! in_array( $avatar_preset, array( 'large', 'medium', 'small' ), true ) ) {
	$avatar_preset = 'large';
}

$width = isset( $attributes['width'] ) ? (int) $attributes['width'] : 120;
$width = min( 512, max( 32, $width ) );

$style = sprintf( 'width:%dpx;height:%dpx;', $width, $width );

if ( $team_id < 1 ) {
	$wrapper = get_block_wrapper_attributes(
		array(
			'class' => 'clanbite-team-avatar clanbite-team-avatar--placeholder',
			'style' => $style,
		),
		$block
	);
	echo wp_kses( '<div ' . $wrapper . '><span>' . esc_html__( 'Team avatar', 'clanbite' ) . '</span></div>', clanbite_block_fragment_allowed_html());
	return;
}

$url = function_exists( 'clanbite_teams_get_display_team_avatar' )
	? clanbite_teams_get_display_team_avatar( $team_id, false, '', 'team_avatar_block', $avatar_preset )
	: '';
if ( '' === $url ) {
	$url = clanbite_teams_get_bundled_default_avatar_url();
}
$url = trim( (string) $url );

$wrapper_attributes = get_block_wrapper_attributes(
	array(
		'class' => 'clanbite-team-avatar-block',
		'style' => $style,
	),
	$block
);

$alt = sprintf(
	/* translators: %s: team name */
	__( 'Avatar for %s', 'clanbite' ),
	get_the_title( $team_id )
);

ob_start();
if ( $url ) {
	printf(
		'<img class="clanbite-team-avatar__img" src="%1$s" alt="%2$s" width="%3$d" height="%3$d" loading="lazy" decoding="async" />',
		esc_url( $url ),
		esc_attr( $alt ),
		(int) $width
	);
} else {
	echo '<span class="clanbite-team-avatar__img clanbite-team-avatar__img--placeholder" role="img" aria-label="' . esc_attr( $alt ) . '">' . esc_html__( 'No avatar', 'clanbite' ) . '</span>';
}
$img_inner = ob_get_clean();

if ( ! empty( $attributes['isLink'] ) && function_exists( 'clanbite_block_entity_link_url' ) ) {
	$href = clanbite_block_entity_link_url(
		(string) get_permalink( $team_id ),
		'clanbite/team-avatar',
		$team_id,
		$block
	);
	if ( '' !== $href ) {
		$target = ( isset( $attributes['linkTarget'] ) && '_blank' === $attributes['linkTarget'] ) ? ' target="_blank"' : '';
		$rel    = function_exists( 'clanbite_block_entity_link_rel' ) ? clanbite_block_entity_link_rel( $attributes ) : '';
		$rel_at = '' !== $rel ? ' rel="' . esc_attr( $rel ) . '"' : '';
		$img_inner = '<a class="clanbite-team-avatar__link" href="' . esc_url( $href ) . '"' . $target . $rel_at . '>' . $img_inner . '</a>';
	}
}

$avatar_clip_open  = '<div class="clanbite-team-avatar__clip">';
$avatar_clip_close = '</div>';
$avatar_media      = $avatar_clip_open . $img_inner . $avatar_clip_close;

echo wp_kses( '<div ' . $wrapper_attributes . '><div class="clanbite-team-avatar">' . $avatar_media . '</div></div>', clanbite_block_fragment_allowed_html());
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals
