<?php

/**
 * @file
 * preprocess-paragraph.php
 *
 * Define all paragraph preprocess HOOKs. Each bundle should provide it's own
 * hook function. e.g. `naomi_2025_preprocess_paragraph__[bundle]`
 */

use Drupal\Component\Utility\Html;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Render\Markup;
use Drupal\menu_link_content\Entity\MenuLinkContent;

/**
 * Implements hook_preprocess_paragraph().
 */
function naomi_2025_preprocess_paragraph(&$variables) {
  /** @var \Drupal\paragraph\Entity\Paragraph $paragraph */
  $paragraph = $variables['paragraph'];
  $paragraph_bundle = $paragraph->bundle();

  $variables['attributes']['id'] = Html::getClass('paragraph-' . $paragraph_bundle . '-' . $paragraph->id());
}

/**
 * Implements hook_preprocess_paragraph() for document_list.
 */
function naomi_2025_preprocess_paragraph__document_list(&$variables) {
  $variables['documents'] = '';
}

/**
 * Implements hook_preprocess_paragraph() for curated_content.
 */
function naomi_2025_preprocess_paragraph__curated_content(&$variables) {
  $entity_type_manager = \Drupal::entityTypeManager();

  /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
  $paragraph = $variables['paragraph'];

  $content_view_mode = $paragraph?->field_content_view_mode?->getString();
  $node_id = $paragraph?->field_ern_content?->getString();

  if (!$content_view_mode || !$node_id) {
    return;
  }

  /** @var \Drupal\node\Entity\Node $node */
  $node = $entity_type_manager->getStorage('node')->load($node_id);

  // Make sure it's a valid node and we have the proper access to view it.
  if ($node && $node->access('view')) {
    $render_controller = $entity_type_manager->getViewBuilder('node');
    $variables['curated_node'] = $render_controller->view($node, $content_view_mode);
  }

  if ($color_scheme = $paragraph?->field_color_scheme?->getString()) {
    $variables['#attached']['library'][] = "naomi_2025/color-scheme.{$color_scheme}";
  }
}

/**
 * Implements hook_preprocess_paragraph() for embed_code.
 */
function naomi_2025_preprocess_paragraph__embed_code(&$variables) {
  /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
  $paragraph = $variables['paragraph'];

  $field_embedded_media = $paragraph->field_embedded_media?->first()?->getValue();

  if ($field_embedded_media) {
    $variables['embed_code'] = Markup::create($field_embedded_media['second']);
    $variables['embed_type'] = Html::getClass($field_embedded_media['first']);
  }
}

/**
 * Implements hook_preprocess_paragraph() for in_this_section.
 */
function naomi_2025_preprocess_paragraph__in_this_section(&$variables) {
  /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
  $paragraph = $variables['paragraph'];

  // If no target menu is set, default to "main".
  $field_target_menu = $paragraph?->get('field_target_menu')->getString() ?: 'main';

  if ($field_target_menu === 'main') {
    $menu_link_content_query = \Drupal::entityQuery('menu_link_content')
      ->condition('link.uri', 'entity:node/' . $paragraph->getParentEntity()->id())
      ->condition('menu_name', $field_target_menu, 'IN')
      ->sort('id', 'ASC')
      ->accessCheck(TRUE)
      ->range(0, 1);

    $menu_link_content_query_result = $menu_link_content_query->execute();
    $menu_link_id = !empty($menu_link_content_query_result)
      ? reset($menu_link_content_query_result)
      : FALSE;

    if ($menu_link_id) {
      $menu_link = MenuLinkContent::load($menu_link_id);

      $menu_for_current_node = [
        'entity_id' => $menu_link->id(),
        'id' => $menu_link->getPluginId(),
        'title' => $menu_link->getTitle(),
        'menu_name' => $menu_link->getMenuName(),
        'parent' => $menu_link->getParentId(),
      ];
    }
  }

  $parameters = new MenuTreeParameters();
  $parameters->setMinDepth(1);
  $parameters->setMaxDepth(1);
  $parameters->setRoot($menu_for_current_node['id']);

  $menu_link_tree = Drupal::service('menu.link_tree');
  $menu_links = $menu_link_tree->load($menu_for_current_node['menu_name'], $parameters);

  $parent_menu_title = $menu_for_current_node['title'];
  $parent_menu_link = '';

  // No menu links with current root, try the parent.
  if (!$menu_links) {
    // No parent from the current node, do nothing.
    if (empty($menu_for_current_node['parent'])) {
      return;
    }

    $parent_menu_uuid = explode(':', $menu_for_current_node['parent'])[1];

    /** @var Drupal\menu_link_content\Entity\MenuLinkContent $parent_menu_link_content */
    $parent_menu_link_content = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['uuid' => $parent_menu_uuid]);
    $parent_menu_link_content = reset($parent_menu_link_content);

    $parent_menu_title = $parent_menu_link_content->getTitle();
    $parent_menu_link = $parent_menu_link_content->getUrlObject()->toString();

    $parameters->excludeRoot();
    $parameters->setRoot($menu_for_current_node['parent']);

    $menu_links = $menu_link_tree->load($menu_for_current_node['menu_name'], $parameters);
  }

  $manipulators = [
    ['callable' => 'menu.default_tree_manipulators:checkAccess'],
    ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
  ];

  $menu_links = $menu_link_tree->transform($menu_links, $manipulators);
  $build = $menu_link_tree->build($menu_links);

  $build['#cache']['contexts'][] = 'url.path';
  $build['#menu_name'] = 'in-this-section-menu';
  $build["#theme"] = 'menu__in-this-section_menu';

  $variables['menu'] = $build;
  $variables['parent_label'] = $parent_menu_title;
  $variables['parent_label_link'] = $parent_menu_link;
}

/**
 * Implements hook_preprocess_paragraph() for views_reference.
 */
function naomi_2025_preprocess_paragraph__views_reference($variables) {
  // Add the url (complete) as a cache context to field_views_reference.
  $variables["content"]["field_views_reference"]["#cache"]["contexts"][] = 'url';
}

/**
 * Implements hook_preprocess_paragraph() for layout.
 */
function naomi_2025_preprocess_paragraph__layout(&$variables) {
  /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
  $paragraph = $variables['paragraph'];

  $layout_config = $paragraph->getBehaviorSetting('layout_paragraphs', 'config');

  $color_scheme = $layout_config['layout']['layout_color_scheme_options']['data_attribute'] ?? NULL;

  if ($color_scheme) {
    $variables['#attached']['library'][] = "naomi_2025/color-scheme.{$color_scheme}";
  }
}

/**
 * Implements hook_preprocess_paragraph() for image.
 */
function naomi_2025_preprocess_paragraph__image(&$variables) {
  /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
  $paragraph = $variables['paragraph'];

  naomi_2025_process_image_caption($paragraph);
}

/**
 * Implements hook_preprocess_paragraph() for photo_gallery.
 */
function naomi_2025_preprocess_paragraph__photo_gallery(&$variables) {
  /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
  $paragraph = $variables['paragraph'];

  $field_erp_images = $paragraph?->field_erp_images?->referencedEntities();

  if (!$field_erp_images) {
    return;
  }

  /** @var \Drupal\Core\Render\Renderer $renderer */
  $renderer = \Drupal::service('renderer');

  $image_map = [
    0 => 'vertical',
    1 => 'horizontal',
    2 => 'horizontal',
    3 => 'vertical',
  ];

  $reverse_image_map = [
    0 => 'horizontal',
    1 => 'vertical',
    2 => 'vertical',
    3 => 'horizontal',
  ];

  $reverse_layout = $paragraph?->field_reverse_layout?->getString() || FALSE;

  $variables['reverse_layout'] = $reverse_layout;
  $variables['#attached']['library'][] = "naomi_2025/paragraph.photo-gallery." . ($reverse_layout ? 'reverse' : 'normal');

  /** @var \Drupal\paragraphs\Entity\Paragraph $image_paragraph */
  foreach ($field_erp_images as $delta => $image_paragraph) {
    $field_erm_image = $image_paragraph?->field_erm_image?->referencedEntities();

    if (!$field_erm_image) {
      continue;
    }

    naomi_2025_process_image_caption($image_paragraph);

    /** @var \Drupal\media\Entity\Media $media */
    $media = reset($field_erm_image);
    $media_info = naomi_2025_get_media_image_info($media);

    /** @var \Drupal\file\Entity\File $file */
    $file = $media_info['file'];

    $direction = $reverse_layout
      ? $reverse_image_map[$delta % 4]
      : $image_map[$delta % 4];

    // Build the responsive image render array manually.
    $responsive_image = [
      '#theme' => 'responsive_image',
      '#responsive_image_style_id' => "image_photo_gallery_{$direction}",
      '#uri' => $file['uri'],
      '#height' => $file['height'],
      '#width' => $file['width'],
      '#attributes' => [
        'alt' => $file['alt'],
      ],
    ];
    $renderer->addCacheableDependency($responsive_image, $file['uri']);

    $media->image = $responsive_image;
  }
}
