<?php
$tooltipWords = $attributes['tooltipWords'];

if (!function_exists('compareTooltipText')) :
	// Fonction de comparaison pour usort
	function compareTooltipText($a, $b)
	{
		return strcmp($a['tooltipText'], $b['tooltipText']);
	}
	// Utilisation de usort pour trier le tableau par ordre alphabétique
	usort($tooltipWords, 'compareTooltipText');
endif;

global $post;
$terms = get_the_terms($post->ID, 'thematiques');
$thematique = is_array($terms) ? $terms[0] : null; ?>
<?php

?>
<section id="vocabulaire-summary" class="homegrade-blocks-vocabulaire-summary">
	<h2 className="homegrade-blocks-vocabulaire-summary__title">
		<?php echo __("Vocabulaire", "homegrade-blocks") ?><?php echo  $thematique && $thematique->name ?  " — " . $thematique->name : ""; ?>
	</h2>
	<?php foreach ($tooltipWords as $word) : ?>
		<?php
		$wordUpToDateTitle  = get_the_title($word['tooltipID']);
		$wordUpToDateDefinition  = get_field('definition', $word['tooltipID']);
		?>
		<details class="question homegrade-dynamic-accordeon" data-definition-id="<?php echo $word['tooltipID'] ?>">
			<summary>
				<?php echo $wordUpToDateTitle; ?>
				<div class="open-close-icon">
					<img src="<?php echo plugins_url('/', dirname(__FILE__)) . 'src/img/chevron_down.svg' ?>" class="open-close-cta" alt="" />
				</div>
			</summary>
			<div class="homegrade-blocks-vocabulaire-summary__content-wrapper homegrade-dynamic-accordeon__content-wrapper">
				<div class=" homegrade-dynamic-accordeon__content">
					<p class="homegrade-blocks-vocabulaire-summary__content">
						<?php echo $wordUpToDateDefinition; ?>
					</p>
				</div>
			</div>
		</details>
	<?php endforeach; ?>
</section>