<?php
/**
 * View: Language switcher dropdown for translatable form fields.
 *
 * @package Ilabs\Firevert\Core
 *
 * @var string[] $languages List of language codes (e.g. ['pl', 'en']).
 * @var string   $current   Currently selected language code.
 */

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

use Ilabs\Firevert\Core\Services\Helper\Integration\LanguageHelper;

if ( count( $languages ) < 2 ) {
	return;
}
?>

<p style="margin:1em 0;">
	<label for="firevert_lang_select">
		<?php esc_html_e( 'Switch language:', 'firevert' ); ?>
	</label>
	<select id='firevert_lang_select' onchange='window.location.href=this.value'>
		<?php
		$self_url  = remove_query_arg( 'lang' );
		$languages = LanguageHelper::get_available_language_slugs();
		$locales   = LanguageHelper::get_locale_map();
		$current   = LanguageHelper::get_current_simple_code();

		foreach ( $languages as $code ) :
			$url   = add_query_arg( 'lang', $code, $self_url );
			$label = $locales[ $code ] ?? $code;
			?>
			<option value="<?php echo esc_url( $url ); ?>" <?php selected( $code, $current ); ?>>
				<?php echo esc_html( $label ); ?>
			</option>
		<?php endforeach; ?>
	</select>

</p>
