<?php

/**
 * @file
 * Primary module hooks for image_field_caption module.
 */

declare(strict_types=1);

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Hook\Attribute\LegacyHook;
use Drupal\Core\Hook\Attribute\LegacyModuleImplementsAlter;
use Drupal\image_field_caption\Hook\ImageFieldCaptionHooks;
use Drupal\image_field_caption\Hook\ImageFieldCaptionWidgetHooks;

/**
 * Implements hook_module_implements_alter().
 *
 * @see ImageFieldCaptionHooks::theme()
 */
#[LegacyModuleImplementsAlter]
function image_field_caption_module_implements_alter(array &$implementations, string $hook): void {
  // Ensures our hook_theme() runs after image & responsive_image modules
  // since we inherit their templates.
  if ($hook === 'theme') {
    $modules = array_keys($implementations);
    $insert_position = -1;
    foreach (ImageFieldCaptionHooks::THEME_DEPENDENCIES as $dependency) {
      $dependency_index = array_search($dependency, $modules, TRUE);
      if ($dependency_index !== FALSE) {
        $insert_position = max($dependency_index, $insert_position);
      }
    }
    $target_implementation = 'image_field_caption';
    if ($insert_position > array_search($target_implementation, $modules, TRUE)) {
      $insert_position++;
      $group = $implementations[$target_implementation];
      unset($implementations[$target_implementation]);
      $implementations = array_slice($implementations, 0, $insert_position, TRUE)
        + [$target_implementation => $group]
        + array_slice($implementations, $insert_position, NULL, TRUE);
    }
  }
}

/**
 * Implements hook_field_info_alter().
 */
#[LegacyHook]
function image_field_caption_field_info_alter(array &$info): void {
  \Drupal::classResolver(ImageFieldCaptionHooks::class)->fieldInfoAlter($info);
}

/**
 * Implements hook_field_widget_single_element_form_alter().
 */
#[LegacyHook]
function image_field_caption_field_widget_single_element_form_alter(array &$element, FormStateInterface $form_state, array $context): void {
  \Drupal::classResolver(ImageFieldCaptionWidgetHooks::class)->imageWidgetAlter($element, $form_state, $context);
}

/**
 * Implements hook_theme().
 */
#[LegacyHook]
function image_field_caption_theme(array $existing): array {
  return \Drupal::classResolver(ImageFieldCaptionHooks::class)->theme($existing);
}

/**
 * Implements hook_config_schema_info_alter().
 */
#[LegacyHook]
function image_field_caption_config_schema_info_alter(array &$definitions): void {
  \Drupal::classResolver(ImageFieldCaptionHooks::class)->configSchemaInfoAlter($definitions);
}

/**
 * Prepares variables for image caption formatter templates.
 *
 * Default template: image-caption-formatter.html.twig.
 *
 * @param array $variables
 *   Inherits all variables from the base template, with an additional:
 *   - caption: An optional caption text.
 *
 * @see \template_preprocess_image_formatter()
 */
function template_preprocess_image_caption_formatter(array &$variables): void {
  /** @var \Drupal\image_field_caption\Plugin\Field\FieldType\ImageCaptionItem $item */
  $item = $variables['item'];
  $values = $item->getValue();
  // Set the caption value. Skip only empty string, allow '0' and
  // other non-empty values.
  $caption = $values['caption'] ?? '';
  if ($caption !== '') {
    $variables['caption'] = [
      '#type' => 'processed_text',
      '#text' => $caption,
      '#format' => $values['caption_format'],
    ];
  }
}

/**
 * Prepares variables for responsive image caption formatter templates.
 *
 * Default template: responsive-image-caption-formatter.html.twig.
 *
 * @param array $variables
 *   Inherits all variables from the base template, with an additional:
 *   - caption: An optional caption text.
 *
 * @see \template_preprocess_responsive_image_formatter()
 */
function template_preprocess_responsive_image_caption_formatter(array &$variables): void {
  template_preprocess_image_caption_formatter($variables);
}
