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

use Drupal\views\Views;

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

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

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

function toc_lnr_preprocess_menu__main_footer_menu_block(&$vars) {
  $vars['no_list_icon_left'] = TRUE;
}

/**
 * Implements hook_preprocess_menu().
 */
function toc_lnr_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')) {
      $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;
        }
      }
    }
  }
}
