<?php

/**
 * @file
 * preprocess-menu.php
 *
 * Define all view preprocess HOOKs.
 */

use Drupal\Component\Utility\Html;
use Drupal\Core\Url;

/**
 * Implements hook_preprocess_menu().
 */
function naomi_2025_preprocess_menu(&$variables) {
  $menu_name = $variables['menu_name'];

  $variables['menu_name'] = Html::getClass($menu_name);

  if (!in_array($menu_name, ['admin', 'devel'])) {
    // Clear any Drupal classes.
    $variables['attributes']['class'] = [];
  }
}

/**
 * Implements hook_preprocess_menu_local_tasks().
 */
function naomi_2025_preprocess_menu_local_tasks(&$variables) {
  /** @var Symfony\Component\Routing\Route $route */
  $route = \Drupal::routeMatch()->getRouteObject();
  $path = $route->getPath();

  if (str_starts_with($path, '/node')) {
    /** @var \Drupal\node\Entity\Node $node */
    $node = \Drupal::routeMatch()->getParameter('node');

    $variables['primary']['node.id'] = [
      '#theme' => 'menu_local_task',
      '#link' => [
        'title' => 'Node ID: ' . $node->id(),
        'url' => Url::fromRoute('<nolink>'),
      ],
      '#weight' => 1000,
    ];
  }
}

/**
 * Implements hook_preprocess_menu_local_task().
 */
function naomi_2025_preprocess_menu_local_task(&$variables) {
  $variables['attributes']['class'] = [];
  $variables['attributes']['class'][] = 'menu__item';

  if ($variables['link']['#url']->getRouteName() === '<nolink>') {
    $variables['attributes']['class'][] = 'menu__item--nid';
  }
}

/**
 * Implements hook_preprocess_menu() for social.
 */
function naomi_2025_preprocess_menu__social(&$variables) {
  $site_name = \Drupal::config('system.site')->get('name');

  foreach ($variables['items'] as &$item) {
    $item_title = &$item['title'];

    $item['url']->setOptions([
      'attributes' => [
        'aria-label' => "Go to $site_name's $item_title page",
        'title' => $item_title,
      ],
    ]);

    $item_title = [
      '#theme' => 'social_icon',
      '#social_name' => Html::getClass($item_title),
    ];
  }
}
