<?php

/**
 * @file
 * Functions to support views.
 */

use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Markup;
use EasyRdf\Literal\HTML as LHTML;

/**
 * Preprocess hook preprocess_views_view__VIEW_ID.
 */
function toc_lnr_preprocess_views_view__news_latest_mini_coursel__block_1(&$vars) {
  $items = [];
  $vars['ac_vars'] = FALSE;
  if (!empty($vars["rows"])) {
    $view = $vars["rows"][0]['#view'];
    foreach ($view->result as $result) {
      $news = $result->_entity;
      $aiid = Html::getUniqueId("fid" . $news->id());
      $title = _tm_misc_get_field_content($news, 'title');
      $date = _tm_misc_format_date_overridden($news, 'toc_long', 'changed', ['field_mndpr_d_updated_at', 'field_mndpr_d_published_at']);
      $url = $news->toUrl()->toString();
      $items[] = [
        "link_alt" => $title,
        "link_url" => $url,
        "text" => $title,
        "meta" => $date,
      ];
    }
  }
  if (!empty($items)) {
    $vars['items'] = $items;
  }
}

/**
 * Preprocess hook preprocess_views_view__VIEW_ID.
 */
function toc_lnr_preprocess_views_view__service_alterations_date_range__block(&$vars) {
  $vars['btn_prev_class'] = 'cbelilanl a-pri-icon';
  $vars['btn_next_class'] = 'cbelianr a-pri-icon';
}

/**
 * Preprocess hook preprocess_views_view__VIEW_ID.
 */
function toc_lnr_preprocess_views_view__news_blocks__block_3(&$vars) {
  $items = [];
  $vars['link_scope'] = 'card';
  $vars['header_tag'] = 'h2';
  if (!empty($vars["rows"][0])) {
    $view = $vars["rows"][0]['#view'];
    foreach ($view->result as $result) {
      $entity = $result->_entity;
      $title = _tm_misc_get_field_content($entity, 'title');
      $cats = _tm_misc_get_field_content($entity, 'field_blog_category');
      $tags = _tm_misc_format_tags($cats);
      $link = $entity->toUrl()->toString();
      $subtitle = _tm_misc_format_date_overridden($entity);
      $media_id = _tm_misc_get_field_content($entity, 'field_media_image');
      $image = _tm_misc_get_styled_images_from_media($media_id, 'card_image', 'field_media_image', 'media', 'url_alt');
      $items[] = [
        'title' => $title,
        'card_link' => $link,
        'date' => $subtitle,
        'tags' => $tags,
        'img_url' => $image['url'] ?? '',
        'img_alt' => $image['alt'] ?? $title,
        'link_scope' => 'card',
      ];
    }
  }
  if (!empty($items)) {
    $vars['items'] = $items;
  }
  $vars['card_template'] = "@molecules/tm-cards/news-card/news-card.twig";
  $vars['batch'] = 2;
}

/**
 * Preprocess hook preprocess_views_view__VIEW_ID.
 */
function toc_lnr_preprocess_views_view__child_pages__block_1(&$vars) {
  $items = [];
  $vars['link_scope'] = 'block';
  $vars['header_tag'] = 'h2';
  $theme_setings = theme_get_setting('toc_master_theme_settings');

  if (!empty($vars["rows"][0])) {
    $view = $vars["rows"][0]['#view'];
    foreach ($view->result as $result) {
      $entity = $result->_entity;
      $title = _tm_misc_get_field_content($entity, 'title');
      $link = $entity->toUrl()->toString();
      $body_formatter = _tm_text_summary(200);
      $text = _tm_misc_get_field_content($entity, 'body', 'render', $body_formatter);
      $body = strip_tags(_tm_misc_get_field_content($entity, 'body', 'render', $body_formatter));
      $media_id = _tm_misc_get_field_content($entity, 'field_page_er_main_image');
      if (!$media_id) {
        if (!empty($theme_setings['default_child_page_image']) && $theme_setings['default_child_page_image']) {
          $media_id = $theme_setings['default_child_page_image'];
        }
      }
      $image = _tm_misc_get_styled_images_from_media($media_id, 'card_image', 'field_media_image', 'media', 'url_alt');
      $items[] = [
        'title' => $title,
        'card_link' => $link,
        'body' => $body,
        'img_url' => $image['url'] ?? '',
        'img_alt' => $image['alt'] ?? $title,
      ];
    }
  }
  if (!empty($items)) {
    $vars['items'] = $items;
  }
  $vars['card_template'] = "@molecules/tm-cards/news-card/news-card.twig";
  $vars['batch'] = 2;
}

/**
 * Preprocess hook preprocess_views_view__VIEW_ID.
 */
function toc_lnr_preprocess_views_view__offers_frontend_views__block_1(&$vars) {
  $vars['header'] = [];
  $vars['offers_header'] = [];

  $offers_settings = \Drupal::config('days_out_guide_content_type.settings');
  $overwrite_badge = $offers_settings->get('overwrite_badge');
  if ($overwrite_badge && !empty($vars['items'])) {
    $badge_top_replace = Markup::create($offers_settings->get('overwrite_badge_top'));
    foreach($vars['items'] as &$item) {
      $item['badge_text_top'] = $badge_top_replace;
    }
  }
}

/**
 * Preprocess hook preprocess_views_view__VIEW_ID.
 */
function toc_lnr_preprocess_views_view__news_blocks__two_col(&$vars) {
  toc_master_preprocess_views_view__news_blocks__travel_inspiration($vars);
  $vars['batch'] = 3;
}
