<?php
/**
 * @file
 * Functions to support theming menus.
 */

use Drupal\views\Views;

/**
 * Implements hook_preprocess_menu().
 */
function toc_wmr_preprocess_menu__main_offcanvas(&$vars) {
  $vars['tpl_mid'] = 'menu-offcanvas';
  $vars['tpl_links'] = _tm_menu_tpl_process_links($vars['items'], TRUE, FALSE);
}

function toc_wmr_preprocess_menu__main_footer_menu_block(&$vars) {
  $vars['no_list_icon_left'] = TRUE;
  // Override using the suggestion template for the id as it is the same for all
  // of the footer menus. Use the unique menu name instead.
  // Another way is to use the twig filter clean_unique_id in the template.
  $vars['tpl_mid'] = preg_replace("/[^a-zA-Z0-9]+/", "", $vars['menu_name']);
}

function toc_wmr_preprocess_menu__assist_menu(&$vars) {
  // There has to be a better way to know what menu block it's coming from.
  if (isset($vars['menu_block_configuration'])) {
    if ($vars['menu_block_configuration']['label'] == 'Assist menu mobile') {
      $vars['alignment'] = 'left';
    }
  }
}


/**
 * Implements hook_preprocess_menu().
 */
function toc_wmr_preprocess_menu__main_header(&$vars) {
  // Embedded Assist menu support.
  if ($assist_menu = _tm_menu_load_menu_build_array('assist-menu')) {
    if (!empty($assist_menu['#items'])) {
      $upper_links = _tm_menu_tpl_process_links($assist_menu['#items'], TRUE);
      if (!empty($upper_links)) {
        $vars['upper_links'] = $upper_links;
        foreach ($vars['upper_links'] as $mid => $link) {
          $vars['upper_links'][$mid] = _tm_menu_get_service_updates_link($link);
        }
      }
    }
  }
  if ($mobile_header_menu = _tm_menu_load_menu_build_array('mobile-header-menu')) {
    if (!empty($mobile_header_menu['#items'])) {
      $mobile_header_links = _tm_menu_tpl_process_links($mobile_header_menu['#items'], TRUE);
      if (!empty($mobile_header_links)) {
        $vars['mobile_header_links'] = $mobile_header_links;
      }
    }
  }

  foreach ($vars['tpl_links'] as $mid => $link) {
    if (!empty($link['class']) && str_contains($link['class'], 'search-pane')) {
      if (\Drupal::hasService('toc_master_companion.service')) {
        /** @var \Drupal\toc_master_companion\Service\TocMasterCompanionService $toc_master_companion_service */
        $toc_master_companion_service = \Drupal::service('toc_master_companion.service');
        $is_search_page = $toc_master_companion_service->isSearchPage();
      } else {
        // No TOC master companion service so default to not being on the search
        // page so old behaviour is maintained.
        $is_search_page = FALSE;
      }
      if (!$is_search_page) {
        $view = Views::getView('search_content');
        if ($view && $view->access('page_results')) {
          $view->setDisplay('page_results');
          $view->initHandlers();
          $display_handler = $view->display_handler;
          if ($display_handler->usesExposed()) {
            $exposed_form_plugin = $display_handler->getPlugin('exposed_form');
            $exposed_form = $exposed_form_plugin->renderExposedForm($view);
            $exposed_form['#attributes']['data-menu-attribute'] = 'mg-menu-search';
            $vars['tpl_links'][$mid]['sub_content'] = $exposed_form;
          }
        }
      }
    }
  }
}

/**
 * Implements hook_preprocess_menu().
 */
function toc_wmr_preprocess_menu__secondary_mobile_menu(&$vars) {
  $vars['attributes']['class'][] = 'pt-3 pb-5';
}

/**
 * Implements hook_preprocess_menu().
 */
function toc_wmr_preprocess_menu__tertiary_mobile_menu(&$vars) {
  $vars['attributes']['class'][] = 'pt-3';
}
