<?php

/**
 * @file
 * Slick carousel integration, the last carousel you'll ever need.
 */

use Drupal\blazy\Blazy;
use Drupal\slick\SlickDefault;

/**
 * Implements hook_theme().
 */
function slick_theme() {
  $themes = [];
  $all = ['slick', 'slide', 'grid', 'image', 'thumbnail', 'vanilla', 'wrapper'];
  foreach ($all as $item) {
    $key = $item == 'slick' ? $item : 'slick_' . $item;
    $themes[$key] = [
      'render element' => 'element',
      'file' => 'templates/slick.theme.inc',
    ];
  }
  return $themes;
}

/**
 * Implements hook_library_info_build().
 */
function slick_library_info_build() {
  return \Drupal::service('slick.manager')->libraryInfoBuild();
}

/**
 * Implements hook_config_schema_info_alter().
 */
function slick_config_schema_info_alter(array &$definitions) {
  Blazy::configSchemaInfoAlter($definitions, 'slick_base', SlickDefault::extendedSettings());
}

/**
 * Implements hook_field_formatter_info_alter().
 */
function slick_field_formatter_info_alter(array &$info) {
  // Supports Media Entity via VEM within VEF if available.
  if (\Drupal::moduleHandler()->moduleExists('video_embed_media')) {
    $info['slick_file'] = [
      'id'          => 'slick_file',
      'label'       => t('Slick Image with Media'),
      'description' => t('Display the images associated to VEM/ME as a simple mix of Slick image/video carousel.'),
      'class'       => 'Drupal\slick\Plugin\Field\FieldFormatter\SlickFileFormatter',
      'field_types' => ['entity_reference', 'image'],
      'quickedit'   => ['editor' => 'disabled'],
      'provider'    => 'slick',
    ];
  }
}

/**
 * Implements hook_hook_info().
 */
function slick_hook_info() {
  $hooks['slick_skins_info'] = ['group' => 'slick'];
  return $hooks;
}

/**
 * Implements hook_library_info_alter().
 */
function slick_library_info_alter(&$libraries, $extension) {
  if ($extension === 'slick' && function_exists('libraries_get_path')) {
    $library_path = libraries_get_path('slick') ?: libraries_get_path('slick-carousel');
    if ($library_path) {
      $libraries['slick']['js'] = ['/' . $library_path . '/slick/slick.min.js' => ['weight' => -3]];
      $libraries['slick']['css']['base'] = ['/' . $library_path . '/slick/slick.css' => []];
      $libraries['slick.css']['css']['theme'] = ['/' . $library_path . '/slick/slick-theme.css' => []];
    }

    $library_easing = libraries_get_path('easing') ?: libraries_get_path('jquery.easing');
    if ($library_easing) {
      $easing_path = $library_easing . '/jquery.easing.min.js';
      // Composer via bower-asset puts the library within `js` directory.
      if (!is_file($easing_path)) {
        $easing_path = $library_easing . '/js/jquery.easing.min.js';
      }
      $libraries['slick.easing']['js'] = ['/' . $easing_path => ['weight' => -4]];
    }

    $library_mousewheel = libraries_get_path('mousewheel') ?: libraries_get_path('jquery-mousewheel');
    if ($library_mousewheel) {
      $libraries['slick.mousewheel']['js'] = ['/' . $library_mousewheel . '/jquery.mousewheel.min.js' => ['weight' => -4]];
    }
  }
}
