<?php
/**
 * The Template for displaying flag.
 *
 * This template can be overridden by copying it to yourtheme/sports-leagues/general/flag.php.
 *
 * @var object $data - Object with widget data.
 *
 * @author        Andrei Strekozov <anwp.pro>
 * @package       Sports-Leagues/Templates
 * @since         0.13.6
 *
 * @version       0.13.6
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

$data = wp_parse_args(
	$data,
	[
		'country_code'  => '',
		'wrapper_class' => '',
		'size'          => 16,
		'width'         => 0,
	]
);

if ( empty( $data['country_code'] ) ) {
	return;
}

/*
|--------------------------------------------------------------------
| Load Legacy flags
|--------------------------------------------------------------------
*/
if ( 'legacy' === sports_leagues()->customizer->get_value( 'general', 'flags' ) ) {
	sports_leagues()->load_partial( $data, 'general/flag--legacy' );

	return;
}

static $flags_url = null;

if ( null === $flags_url ) {
	$flags_url = apply_filters( 'sports-leagues/media/modify_url_svg_sprite', Sports_Leagues::url( 'public/img/flags-v2.svg' ) );
}

/*
|--------------------------------------------------------------------
| Size
|--------------------------------------------------------------------
*/
if ( absint( $data['width'] ) > 10 ) {
	$flag_size = absint( $data['width'] );
} else {
	$flag_size = 32 === absint( $data['size'] ) ? 25 : 18;
}

/*
|--------------------------------------------------------------------
| Parsed country code
|--------------------------------------------------------------------
*/
$code_parsed = mb_strtolower( str_replace( '_', '-', $data['country_code'] ) );

if ( in_array( $data['country_code'], [ '__Africa', '__Asia', '__NC_America', '__Oceania', '__South_America' ], true ) ) {
	$code_parsed = '--world';
} elseif ( ! in_array( $code_parsed, sports_leagues()->data->get_available_circle_flags(), true ) ) {
	$code_parsed = 'xx';
}

/*
|--------------------------------------------------------------------
| Render Flag
|--------------------------------------------------------------------
*/
?>
<svg class="sl-flag--rounded <?php echo esc_attr( $data['wrapper_class'] ); ?>"
	data-toggle="anwp-sl-tooltip" data-tippy-content="<?php echo esc_attr( sports_leagues()->data->get_country_by_code( $data['country_code'] ) ); ?>"
	width="<?php echo absint( $flag_size ); ?>" height="<?php echo absint( $flag_size ); ?>">
	<use xlink:href="<?php echo esc_url( $flags_url ); ?>#sl-flag--<?php echo esc_attr( $code_parsed ); ?>"></use>
</svg>

