<?php
// Définir les classes
$classes = ['hover-highlight-block'];

// Ajouter une classe si les images sont cachées sur mobile
if (isset($attributes['showImagesOnMobile']) && $attributes['showImagesOnMobile'] === false) {
  $classes[] = 'hide-images-on-mobile';
}

$wrapper_attributes = get_block_wrapper_attributes([
  'class' => implode(' ', $classes)
]);
?>
<div <?php echo $wrapper_attributes; ?>>
  <!-- Layout Desktop -->
  <div class="hover-highlight-container desktop">
    <div class="hover-highlight-text-list">
      <?php if ($block->inner_blocks): ?>
        <?php foreach ($block->inner_blocks as $innerBlock): ?>
          <div class="hover-highlight-text-item">
            <?php if (isset($innerBlock->attributes['url']) && !empty($innerBlock->attributes['url'])): ?>
              <?php $target = isset($innerBlock->attributes['opensInNewTab']) && $innerBlock->attributes['opensInNewTab'] ? '_blank' : ''; ?>
              <a href="<?php echo $innerBlock->attributes['url'] ?>" target="<?php echo $target ?>" class="text-item-link">
                <div class="title-wrapper">
                  <h3><?php
                      $title_string = $innerBlock->attributes['title'] ?? '';
                      $arrow_svg_html = '';

                      if (isset($innerBlock->attributes['url']) && !empty($innerBlock->attributes['url'])) {
                        $arrow_svg_html = '&nbsp;<span class="hover-highlight-arrow" aria-hidden="true">' . $attributes['customArrowSvg'] . '</span>';
                      }

                      if (!empty($title_string) && !empty($arrow_svg_html)) {
                        $title_words = explode(' ', $title_string);
                        if (count($title_words) > 1) {
                          $last_word = array_pop($title_words);
                          echo esc_html(implode(' ', $title_words)) . ' <span class="word-arrow-no-wrap">' . esc_html($last_word) . $arrow_svg_html . '</span>';
                        } else {
                          echo '<span class="word-arrow-no-wrap">' . esc_html($title_string) . $arrow_svg_html . '</span>';
                        }
                      } elseif (!empty($title_string)) {
                        echo esc_html($title_string);
                      } // Si titre vide et pas de flèche, ne rien afficher dans h3.
                      ?></h3>
                </div>
                <?php if (isset($innerBlock->attributes['text'])): ?>
                  <p><?php echo $innerBlock->attributes['text'] ?></p>
                <?php endif; ?>
              </a>
            <?php else: ?>
              <div class="text-item-link">
                <div class="title-wrapper">
                  <h3><?php
                      $title_string_no_link = $innerBlock->attributes['title'] ?? '';
                      $arrow_svg_html_no_link = '';

                      // Logique originale : la flèche s'affiche si URL existe, même si pas de <a> parent
                      if (isset($innerBlock->attributes['url']) && !empty($innerBlock->attributes['url'])) {
                        $arrow_svg_html_no_link = '&nbsp;<span class="hover-highlight-arrow" aria-hidden="true">' . $attributes['customArrowSvg'] . '</span>';
                      }

                      if (!empty($title_string_no_link) && !empty($arrow_svg_html_no_link)) {
                        $title_words_no_link = explode(' ', $title_string_no_link);
                        if (count($title_words_no_link) > 1) {
                          $last_word_no_link = array_pop($title_words_no_link);
                          echo esc_html(implode(' ', $title_words_no_link)) . ' <span class="word-arrow-no-wrap">' . esc_html($last_word_no_link) . $arrow_svg_html_no_link . '</span>';
                        } else {
                          echo '<span class="word-arrow-no-wrap">' . esc_html($title_string_no_link) . $arrow_svg_html_no_link . '</span>';
                        }
                      } elseif (!empty($title_string_no_link)) {
                        echo esc_html($title_string_no_link);
                      }
                      ?></h3>
                </div>
                <?php if (isset($innerBlock->attributes['text'])): ?>
                  <p><?php echo $innerBlock->attributes['text'] ?></p>
                <?php endif; ?>
              </div>
            <?php endif; ?>
          </div>
        <?php endforeach; ?>
      <?php endif; ?>
    </div>

    <div class="hover-highlight-image-container">
      <?php echo $content ?>
    </div>
  </div>

  <!-- Layout Mobile -->
  <div class="hover-highlight-container mobile">
    <?php if (isset($attributes['showImagesOnMobile']) && $attributes['showImagesOnMobile'] === true): ?>
      <div class="hover-highlight-mobile-wrapper js-mobile-wrapper">
        <div class="hover-highlight-mobile-items js-mobile-items">
          <?php if ($block->inner_blocks): ?>
            <?php foreach ($block->inner_blocks as $innerBlock): ?>
              <div class="hover-highlight-mobile-item">
                <?php if (isset($innerBlock->attributes['url']) && !empty($innerBlock->attributes['url'])): ?>
                  <?php $target = isset($innerBlock->attributes['opensInNewTab']) && $innerBlock->attributes['opensInNewTab'] ? '_blank' : ''; ?>
                  <a href="<?php echo $innerBlock->attributes['url'] ?>" class="mobile-item-link" target="<?php echo $target ?>">
                    <div class="title-wrapper">
                      <h3><?php echo $innerBlock->attributes['title'] ?></h3>
                      <span class="hover-highlight-arrow" aria-hidden="true">
                        <?php echo $attributes['customArrowSvg']; ?>
                      </span>
                    </div>
                    <?php if (isset($innerBlock->attributes['text'])): ?>
                      <p><?php echo $innerBlock->attributes['text'] ?></p>
                    <?php endif; ?>
                  </a>
                <?php else: ?>
                  <div class="mobile-item-link">
                    <div class="title-wrapper">
                      <h3><?php echo $innerBlock->attributes['title'] ?></h3>
                      <span class="hover-highlight-arrow" aria-hidden="true">
                        <?php echo $attributes['customArrowSvg']; ?>
                      </span>
                    </div>
                    <?php if (isset($innerBlock->attributes['text'])): ?>
                      <p><?php echo $innerBlock->attributes['text'] ?></p>
                    <?php endif; ?>
                  </div>
                <?php endif; ?>
              </div>
            <?php endforeach; ?>
          <?php endif; ?>
        </div>

        <div class="hover-highlight-mobile-content-holder js-mobile-content-holder">
          <?php echo $content; ?>
        </div>
      </div>
    <?php else: ?>
      <div class="hover-highlight-mobile-items">
        <?php if ($block->inner_blocks): ?>
          <?php foreach ($block->inner_blocks as $innerBlock): ?>
            <div class="hover-highlight-mobile-item">
              <?php if (isset($innerBlock->attributes['url']) && !empty($innerBlock->attributes['url'])): ?>
                <?php $target = isset($innerBlock->attributes['opensInNewTab']) && $innerBlock->attributes['opensInNewTab'] ? '_blank' : ''; ?>
                <a href="<?php echo $innerBlock->attributes['url'] ?>" class="mobile-item-link" target="<?php echo $target ?>">
                  <div class="title-wrapper">
                    <h3><?php echo $innerBlock->attributes['title'] ?></h3>
                    <span class="hover-highlight-arrow" aria-hidden="true">
                      <?php echo $attributes['customArrowSvg']; ?>
                    </span>
                  </div>
                  <?php if (isset($innerBlock->attributes['text'])): ?>
                    <p><?php echo $innerBlock->attributes['text'] ?></p>
                  <?php endif; ?>
                </a>
              <?php else: ?>
                <div class="mobile-item-link">
                  <div class="title-wrapper">
                    <h3><?php echo $innerBlock->attributes['title'] ?></h3>
                    <span class="hover-highlight-arrow" aria-hidden="true">
                      <?php echo $attributes['customArrowSvg']; ?>
                    </span>
                  </div>
                  <?php if (isset($innerBlock->attributes['text'])): ?>
                    <p><?php echo $innerBlock->attributes['text'] ?></p>
                  <?php endif; ?>
                </div>
              <?php endif; ?>
            </div>
          <?php endforeach; ?>
        <?php endif; ?>
      </div>
    <?php endif; ?>
  </div>
</div>