<?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')) {
      $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_form_alter() for the exposed search content form.
 */
function toc_wmr_form_views_exposed_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  // Only target the specific exposed form for 'search_content' > 'page_results'.
  if (
    isset($form['#id']) &&
    $form['#id'] === 'views-exposed-form-search-content-page-results'
  ) {
    // Set the content flag. Adjust the value as needed.
    $form['content_flag'] = [
      '#type' => 'value',
      '#value' => TRUE,
    ];
  }
}

/**
 * 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';
}
