<?php
/**
 * EAC Météo Block (fonctions) — Librairie de fonctions pour le bloc Météo
 * Notamment pour l'API REST et l'enregistrement des routes Météo
 *
 * @since 2.5.3
 */

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

/**
 * eac_register_route_language
 *
 * @return void
 */
function eac_register_route_language(): void {
	register_rest_route(
		'eac-blocks/v1',
		'/language',
		array(
			'methods'             => WP_REST_Server::READABLE, // GET
			'callback'            => 'eac_rest_get_language',
			'permission_callback' => function () {
				return current_user_can( 'edit_posts' );
			},
		)
	);
}

// Enregistrement de la route REST pour le bloc Météo
add_action( 'rest_api_init', 'eac_register_route_language', 30 );

/**
 * eac_rest_get_language
 *
 * @param WP_REST_Request $request
 *
 * @return WP_REST_Response
 */
function eac_rest_get_language( WP_REST_Request $request ): WP_REST_Response { // phpcs:ignore
	$code_lang = 'en';
	$supported_languages = array( 'en', 'fr', 'es', 'it', 'hi' );
	$languages = array();
	$langue    = $code_lang;

	$locale = determine_locale(); // ex: 'fr_FR' ou 'fr'
	$langue = ( strpos( $locale, '_' ) !== false ) ? substr( $locale, 0, strpos( $locale, '_' ) ) : $locale;

	if ( in_array( $langue, $supported_languages, true ) ) {
		$code_lang = $langue;
	}

	$languages = eac_get_langs( $code_lang );
	uasort( $languages, function ( $a, $b ) {
		$na = mb_strtolower( eac_remove_accents( $a ) );
		$nb = mb_strtolower( eac_remove_accents( $b ) );
		return $na <=> $nb;
	} );

	return rest_ensure_response( $languages );
}

/**
 * eac_get_langs
 *
 * @param string $client_lang
 *
 * @return array
 */
function eac_get_langs( string $client_lang ): array {
	$languages = include __DIR__ . '/assets/utils/countries.php';
	$langs     = array();

	foreach ( $languages as $language => $properties ) {
		$val = $properties[ $client_lang ];
		$langs[ esc_attr( strtolower( $language ) ) ] = esc_html( $val );
	}
	return $langs;
}

/**
 * eac_remove_accents
 *
 * @param string $str
 *
 * @return string
 */
function eac_remove_accents( string $str ): string {
	if ( function_exists( 'transliterator_transliterate' ) ) {
		return transliterator_transliterate( 'Any-Latin; Latin-ASCII; [:Nonspacing Mark:] Remove', $str );
	}

	// phpcs:disable WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine
	$chars = array(
		'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E',
		'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O',
		'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Þ' => 'Th', 'ß' => 'ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a',
		'å' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd',
		'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ý' => 'y',
		'þ' => 'th', 'ÿ' => 'y',
	);
	// phpcs:enable WordPress.Arrays.ArrayDeclarationSpacing.ArrayItemNoNewLine

	return strtr( $str, $chars );
}

/**
 * eac_get_uv_color
 *
 * @param float $uv_index
 *
 * @return string
 */
function eac_get_uv_color( float $uv_index ): string {
	if ( $uv_index <= 2 ) {
		return 'rgb(31, 163, 0)'; // Vert pour UV faible
	} elseif ( $uv_index <= 5 ) {
		return 'rgb(255, 187, 0)'; // Jaune pour UV modéré
	} elseif ( $uv_index <= 7 ) {
		return 'rgb(255, 119, 33)'; // Orange pour UV élevé
	} elseif ( $uv_index <= 10 ) {
		return 'rgb(255, 0, 0)'; // Rouge pour UV très élevé
	} else {
		return 'rgb(210, 39, 255)'; // Violet pour UV extrême
	}
}
