<?php

/**
 * @file
 * Primary module hooks for Intercept Dashboard module.
 */

use Drupal\Core\Asset\AttachedAssetsInterface;

/**
 * Implements hook_theme().
 */
function intercept_dashboard_theme() {
  return [
    'intercept_current_filters' => [
      'variables' => [
        'filters' => NULL,
      ],
    ],
    'intercept_dashboard' => [
      'variables' => [
        'filters' => NULL,
        'filters_summary' => NULL,
        'summary' => NULL,
        'event_table' => NULL,
        'charts' => NULL,
      ],
    ],
    'intercept_dashboard_metric' => [
      'variables' => [
        'label' => NULL,
        'value' => NULL,
        'link' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_js_alter().
 */
function intercept_dashboard_js_alter(&$javascript, AttachedAssetsInterface $assets) {
  /**
   * Charts module fires a custom JS event when the charts are initialized.
   * We need to ensure our overrides load first so we have the opportunity
   * to attach an event listener before the initial listener is dispatched.
   */
  $dashboard_file = 'modules/contrib/intercept/modules/intercept_dashboard/js/intercept-dashboard-bar-chart.js';
  $charts_file = 'modules/contrib/charts/modules/charts_chartjs/js/charts_chartjs.js';
  if (isset($javascript[$charts_file]) && isset($javascript[$dashboard_file])) {
    $javascript[$dashboard_file]['weight'] = $javascript[$charts_file]['weight'] - 0.001;
  }
}

/**
 * Prepares variables for intercept_dashboard template.
 */
function template_preprocess_intercept_dashboard(array &$variables) {
  $variables['#attached']['library'][] = 'intercept_dashboard/intercept_dashboard';
}

/**
 * Prepares variables for intercept_dashboard_metric template.
 */
function template_preprocess_intercept_dashboard_metric(array &$variables) {
  $variables['#attached']['library'][] = 'intercept_dashboard/intercept_dashboard_metric';
}
