<?php

/**
 * @file
 * Theming for individual forms by ID.
 */

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook__preprocess_views_exposed_form().
 * Search modal form.
 */
function toc_lnr_preprocess_views_exposed_form__search_content__page_results(&$vars): void
{

  // If the form is being used in a menu - set different classes and attributes.
  if (!empty($vars['form']['#attributes']['data-menu-attribute'])
    && $vars['form']['#attributes']['data-menu-attribute'] === 'mg-menu-search') {

    $vars['form']['search_api_fulltext']['#title'] = 'Search this site';
    $vars['form']['search_api_fulltext']['#title_display'] = 'before';
    $vars['form']['search_api_fulltext']['#element_classes'] = 'col-md-11 d-flex fle-row mb-2';
    $vars['form']['search_api_fulltext']["#label_attributes"]['class'][] = 'no-addon-sep col-md-3 h2 fs-5 text-black ffm d-flex justify-content-start align-items-center mb-0 mt-2';
    $vars['form']['search_api_fulltext']['#attributes']['class'][] = 'py-1 px-3 fs-5 min-h-0';
    $vars['form']['search_api_fulltext']['#wrapper_attributes']['class'][] = 'no-addon-sep';

    $vars['form']['actions']['#attributes']['class'][] = 'col-md-4 mt-3 d-none';
    $vars['form']['actions']['submit']['#attributes']['class'][] = 'w-100 align-self-end mb-2 ffm';
    // $vars['form']['actions']['submit']['#size'] = 'lg';
    $vars['form']['actions']['#attributes']['id'] = 'edit-actions-search';

    // The default function adds the 'addon' for all search boxes, unset that.
    if (!empty($vars['form']['search_api_fulltext']['#input_suffix'])) {
      $vars['form']['search_api_fulltext']['#input_suffix'] =
        '<button data-drupal-selector="edit-submit-search-content" class="button js-form-submit form-submit btn btn-default btn-lg mla no-addon-sep" type="submit" id="edit-submit-search-content--2" value="Search" data-persistent-id="edit-search-views-exposed-form-search-content" data-dashlane-label="true" data-dashlane-rid="15ec5ef945af53c8">
    <span class="sr-only">Search</span><i class="fas fa-chevron-right"></i>
  </button>';
    }
  } else {
    // Default was hidden, set to before.
    $vars['form']['search_api_fulltext']['#title_display'] = 'none';
    $vars['form']['search_api_fulltext']['#element_classes'] = 'col-md-8';
    $vars['form']['search_api_fulltext']["#label_attributes"]['class'][] = 'hs ffm';

    $vars['form']['actions']['#attributes']['class'][] = 'col-md-4 d-inline';
    $vars['form']['actions']['submit']['#attributes']['class'][] = 'w-100 align-self-end ffm';
    $vars['form']['actions']['submit']['#size'] = 'lg';
    $vars['form']['actions']['#attributes']['id'] = 'edit-actions-search';

    if(!empty($vars['form']['search_api_fulltext']['#input_suffix'])) {
      unset($vars['form']['search_api_fulltext']['#input_suffix']);
    }
  }
}

/**
 * Implements hook__preprocess_views_exposed_form().
 *
 * @throws \Exception
 */
function toc_lnr_preprocess_views_exposed_form__news_blocks__block_3(&$vars) {
  $vars['form']['actions']['reset']['#text'] = '<i class="fas fa-times-circle me-2"></i>' . $vars['form']['actions']['reset']['#value'];
  $vars['form']['actions']['reset']['#size'] = 'sm';
  $vars['form']['actions']['reset']['#attributes']['class'][] = 'mb-0';
  // Remove margin-top added by base theme.
  if (!empty($vars['form']['actions']['reset']['#attributes']['class'])) {
    $classes =& $vars['form']['actions']['reset']['#attributes']['class'];
    if (($idx = array_search('mt-3', $classes)) !== FALSE) {
      unset($classes[$idx]);
    }
  }
  $vars['form']['actions']['reset']['#status'] = 'info';
  // Finally, we are using some custom ajax submit triggers, so we need
  // a submit who has an attribute that will not change on ajax reload,
  // so that we can trigger it.
  $vars['form']['actions']['submit']['#status'] = 'info';
  $vars['form']['actions']['submit']['#attributes']['class'][] = 'd-none';

  if (!empty($vars['form']['#view'])) {
    /** @var Drupal\views\ViewExecutable $view */
    $view = $vars['form']['#view'];
    $vars['form_prefix'] = $view->getTitle() ?? FALSE;
  }
}

/**
 * Implements hook_preprocess_views_exposed_form().
 *
 * This view is also post-rendering it's form and limiting results using
 * _tm_views_limit_filter_options();
 *
 * @see _toc_master_views_post_render__offers_frontend_views__block_1()
 */
function toc_lnr_preprocess_views_exposed_form__offers_frontend_views__block_1(&$vars): void
{
  // Revert all the changes from TOC Master, and use our local template.
  $vars['form'] = $vars['form_original'];
  // Move Value to Text which is rendered with text|raw.
  $vars['form']['actions']['reset']['#text'] = '<i class="fas fa-times-circle me-2"></i>' . $vars['form']['actions']['reset']['#value'];
  $vars['form']['actions']['reset']['#size'] = 'sm';
  $vars['form']['actions']['reset']['#attributes']['class'][] = 'mb-0';
  $vars['form']['actions']['reset']['#status'] = 'info';
  // Finally, we are using some custom ajax submit triggers, so we need
  // a submit who has an attribute that will not change on ajax reload,
  // so that we can trigger it.
  $vars['form']['actions']['submit']['#status'] = 'info';
  $vars['form']['actions']['submit']['#attributes']['class'][] = 'd-none';
}

/**
 * Implements hook__preprocess_views_exposed_form().
 *
 * @see toc_master_preprocess_views_exposed_form__attractions__listing().
 */
function toc_lnr_preprocess_views_exposed_form__attractions__listing(&$vars): void
{
  $vars['form'] = $vars['form_original'];
  $vars['form']['actions']['reset']['#text'] = '<i class="fas fa-times-circle me-2"></i>' . $vars['form']['actions']['reset']['#value'];
  $vars['form']['actions']['reset']['#size'] = 'sm';
  $vars['form']['actions']['reset']['#attributes']['class'][] = 'mb-0';
  $vars['form']['actions']['reset']['#status'] = 'info';
  // Finally, we are using some custom ajax submit triggers, so we need
  // a submit who has an attribute that will not change on ajax reload,
  // so that we can trigger it.
  $vars['form']['actions']['submit']['#status'] = 'info';
  $vars['form']['actions']['submit']['#attributes']['class'][] = 'd-none';

  if (!empty($vars['form']['#view'])) {
    /** @var Drupal\views\ViewExecutable $view */
    $view = $vars['form']['#view'];
    $vars['form_prefix'] = $view->getTitle() ?? FALSE;
  }
}

function toc_lnr_preprocess_views_exposed_form__news_blocks__travel_inspiration(&$vars): void {
  $vars['form'] = $vars['form_original'];
  // Move Value to Text which is rendered with text|raw.
  $vars['form']['actions']['reset']['#text'] = '<i class="fas fa-times-circle me-2"></i>' . $vars['form']['actions']['reset']['#value'];
  $vars['form']['actions']['reset']['#size'] = 'sm';
  $vars['form']['actions']['reset']['#attributes']['class'][] = 'mb-0';
  $vars['form']['actions']['reset']['#status'] = 'info';
  // Finally, we are using some custom ajax submit triggers, so we need
  // a submit who has an attribute that will not change on ajax reload,
  // so that we can trigger it.
  $vars['form']['actions']['submit']['#status'] = 'info';
  $vars['form']['actions']['submit']['#attributes']['class'][] = 'd-none';

  if (!empty($vars['form']['#view'])) {
    /** @var Drupal\views\ViewExecutable $view */
    $view = $vars['form']['#view'];
    $vars['form_prefix'] = $view->getTitle() ?? FALSE;
  }
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_lnr_preprocess_fieldset__field_blog_category_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_lnr_clear_fieldset_radioboxes($vars);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_lnr_preprocess_fieldset__field_travel_inspiration_cat_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_lnr_clear_fieldset_radioboxes($vars);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_lnr_preprocess_fieldset__field_of_region_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_lnr_clear_fieldset_radioboxes($vars);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_lnr_preprocess_fieldset__field_of_category_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_lnr_clear_fieldset_radioboxes($vars);
}

/**
 * Implements hook_preprocess_fieldset__FIELD_ID().
 */
function toc_lnr_preprocess_fieldset__field_atn_er_category_target_id(&$vars) {
  toc_master_preprocess_fieldset__checkboxes($vars);
  _toc_lnr_clear_fieldset_radioboxes($vars);
}

/**
 * Helper function. Hide the tags and margin/padding for fieldsets in filters.
 *
 * @param $vars
 *
 * @return void
 */
function _toc_lnr_clear_fieldset_radioboxes(&$vars) {
  $vars['attributes']['class'][] = 'no-shd';
  $vars['attributes']['class'][] = 'no-tags';
  $vars['attributes']['class'][] = 'mb-0';
}

function toc_lnr_form_nre_stations_ldb_live_boards_finder_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (isset($form['callingAt'])) {
    $form['callingAt']['#input_prefix'] = $form['callingAt']['#title'] ?? '';
    $form['callingAt']['#title_display'] = 'invisible';
    $form['callingAt']['#element_classes'] = 'mt-3 solid-background';
  }
  $form['station']['#element_classes'] .= ' solid-background';
  unset($form['station']['#input_suffix']);
  $form['#attributes']['class'][] = 'mb-0 stations-autocomplete';
  if (($key = array_search('visually-hidden', $form['actions']['submit']['#attributes']['class'])) !== FALSE) {
    unset($form['actions']['submit']['#attributes']['class'][$key]);
  }
  $form['actions']['submit']['#attributes']['class'][] = 'w-100 bk-frm-fs py-2 rounded-3';
}

/**
 * Implements hook_form_alter() for the exposed search content form.
 */
function toc_lnr_form_views_exposed_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id): void
{
  // 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,
    ];
  }

  if (!empty($form['#attributes']['class'])) {
    $classes = $form['#attributes']['class'];
    if (is_string($classes)) {
      $classes = preg_split('/\s+/', trim($classes)) ?: [];
    }
    $form['#attributes']['class'] = array_values(array_diff($classes, ['frm-shd']));
  }
}
