<?php

/**
 * @file
 * Contains media related functions.
 */

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;

/**
 * Implements hook_preprocess_media().
 */
function thunder_media_preprocess_media(array &$variables): void {
  // Remove contextual links from preview in node form.
  if ($variables['elements']['#view_mode'] === 'paragraph_preview' && isset($variables['title_suffix']['contextual_links'])) {
    unset($variables['title_suffix']['contextual_links']);
    unset($variables['elements']['#contextual_links']);
    $variables['attributes']['class'] = array_diff($variables['attributes']['class'], ['contextual-region']);
  }
}

/**
 * Implements hook_page_attachments().
 */
function thunder_media_page_attachments(array &$page): void {
  $page['#attached']['library'][] = 'thunder_media/gallery';
}

/**
 * Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
 *
 * Add process function to hide 'remove' button of image field widget on inline
 * entity forms.
 */
function thunder_media_field_widget_single_element_image_focal_point_form_alter(array &$element, FormStateInterface $form_state, array $context): void {
  $config = \Drupal::config('thunder_media.settings');

  if (!$config->get('enable_filefield_remove_button')) {
    $type = NULL;
    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface|null $form_display */
    $form_display = !empty($form_state->getStorage()['form_display']) ? $form_state->getStorage()['form_display'] : NULL;
    if ($form_display && $form_display->getTargetEntityTypeId() === 'media') {
      $type = $form_display->getTargetBundle();
    }
    elseif (isset($context['form']['#type']) && $context['form']['#type'] === 'inline_entity_form' && $context['form']['#entity_type'] === 'media') {
      $type = $context['form']['#bundle'];
    }
    if ($type) {
      /** @var \Drupal\media\MediaTypeInterface $type */
      $type = \Drupal::entityTypeManager()->getStorage('media_type')->load($type);
      if ($type->get('source_configuration')['source_field'] == $element['#field_name']) {
        $element['#process'][] = 'thunder_media_inline_entity_form_image_widget_process';
      }
    }
  }
}

/**
 * Implements hook_field_widget_complete_WIDGET_TYPE_form_alter().
 */
function thunder_media_field_widget_complete_media_library_media_modify_widget_form_alter(array &$field_widget_complete_form, FormStateInterface $form_state, array $context): void {
  // Add custom styling.
  $field_widget_complete_form['#attached']['library'][] = 'thunder_media/media_library.widget';
}

/**
 * Process function to hide 'Remove' button on image widget forms.
 */
function thunder_media_inline_entity_form_image_widget_process(array $element, FormStateInterface $form_state, array $form): array {
  if (isset($element['remove_button'])) {
    $element['remove_button']['#access'] = FALSE;
  }

  return $element;
}

/**
 * Implements hook_ENTITY_TYPE_view_alter() for media entities.
 */
function thunder_media_media_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display): void {
  if ($entity->bundle() === 'gallery' && $display->getMode() === 'paragraph_preview') {
    $build['#attached']['library'][] = 'thunder_media/gallery.paragraph_preview';
    foreach (Element::children($build['field_media_images']) as $key) {
      if ($key > 5) {
        $build['field_media_images'][$key]['#access'] = FALSE;
      }
    }
  }
}
