<?php

/**
 * @file
 * Install, update and uninstall functions for the AN Enroll Export module.
 */

/**
 * Implements hook_install().
 */
function social_event_an_enroll_enrolments_export_install() {
  // Set the enroll export pluginsExportUser access based on their counterpart
  // access.
  _enrolments_export_plugin_access();

  // Add changes to event_manage_enrollments view.
  _social_event_an_enroll_enrolments_export_alter_event_manage_enrollments_view();
}

/**
 * Implements hook_uninstall().
 */
function social_event_an_enroll_enrolments_export_uninstall() {
  // Set the export plugins access based on their enroll counterpart access.
  _enrolments_export_plugin_access(TRUE);
}

/**
 * Implements hook_update_dependencies().
 */
function social_event_an_enroll_enrolments_export_update_dependencies() {
  // Run the export plugins update hook only after the default export plugin
  // hook is executed.
  $dependencies['social_event_an_enroll_enrolments_export'][8001] = [
    'social_user_export' => 8001,
  ];

  // New config changes should run after the VBO module changes
  // the structure of views.
  $dependencies['social_event_an_enroll_enrolments_export'][10201] = [
    'views_bulk_operations' => 8034,
    'social_event_enrolments_export' => 10201,
  ];

  return $dependencies;
}

/**
 * Set the enroll export plugins access based on their counterpart access.
 */
function social_event_an_enroll_enrolments_export_update_8001() {
  _enrolments_export_plugin_access();
}

/**
 * Sets the access for the export plugins.
 *
 * @param bool $reverse
 *   Flip the keys and value of the mapping plugins.
 */
function _enrolments_export_plugin_access($reverse = FALSE) {
  // Get the plugin mapping.
  $mapping = _enrolments_export_plugin_mapping($reverse);

  $config = \Drupal::getContainer()->get('config.factory')->getEditable('social_user_export.settings');
  $plugins = $config->get('plugins');
  // We now need to go through the plugins and add the plugin that should be
  // active based on the currently active plugins (either existing or enroll
  // plugins).
  foreach ($plugins as $key => $value) {
    foreach ($mapping as $existing_plugin => $new_plugin) {
      if ($key === $existing_plugin && !empty($value)) {
        $plugins[$new_plugin] = $new_plugin;
      }
    }
  }

  // Set the default export plugins normal users can use.
  $config->set('plugins', $plugins)->save();
}

/**
 * Retrieve a mapped list of normal plugins and enrollment plugins.
 *
 * @param bool $reverse
 *   Flip the keys and value of the return array.
 *
 * @return array
 *   A mapped list of normal plugins and their counterpart enrollment plugins.
 */
function _enrolments_export_plugin_mapping($reverse = FALSE) :array {
  $mapping = [
    'display_name' => 'enrolment_display_name',
    'user_email' => 'enrolment_user_email',
    'user_first_name' => 'enrolment_user_first_name',
    'user_last_name' => 'enrolment_user_last_name',
    'user_registration' => 'enrolment_user_registration',
  ];

  return $reverse ? array_flip($mapping) : $mapping;
}

/**
 * Add a custom action to 'views.view.event_manage_enrollments'.
 *
 * Action ID: 'social_event_an_enroll_enrolments_export_member_action'.
 */
function _social_event_an_enroll_enrolments_export_alter_event_manage_enrollments_view(): void {
  $config = \Drupal::configFactory()->getEditable('views.view.event_manage_enrollments');
  $selected_actions = $config->get('display.default.display_options.fields.social_views_bulk_operations_bulk_form_enrollments_1.selected_actions');

  if (!empty($selected_actions) && \Drupal::moduleHandler()->moduleExists('social_event_enrolments_export')) {
    // Get the action_key of social_event_enrolments_export_enrollments_action.
    $action_id = array_column($selected_actions, 'action_id');
    $action_key = array_search('social_event_enrolments_export_enrollments_action', $action_id);

    // Array search will return the index of the found item and because
    // we're dealing with a multidimensional indexed array, see screenshots in
    // #3316206, it means we need to find the correct key responding with this
    // indexed position so we can unset that.
    $index = array_keys($selected_actions);
    $position = $index[$action_key];

    // Remove 'social_event_enrolments_export_enrollments_action' action.
    // This is added via
    // _social_event_an_enroll_enrolments_export_alter_event_manage_enrollments_view().
    unset($selected_actions[$position]);
  }

  $selected_actions[] = [
    'action_id' => 'social_event_an_enroll_enrolments_export_action',
    'preconfiguration' => [
      'label_override' => t('Export'),
    ],
  ];

  $config->set('display.default.display_options.fields.social_views_bulk_operations_bulk_form_enrollments_1.selected_actions', $selected_actions);
  $config->save();
}

/**
 * Add a custom action to 'views.view.event_manage_enrollments'.
 *
 * Action ID: 'social_event_an_enroll_enrolments_export_member_action'.
 */
function social_event_an_enroll_enrolments_export_update_11001(array &$sandbox): void {
  $config = \Drupal::configFactory()->getEditable('views.view.event_manage_enrollments');
  $selected_actions = $config->get('display.default.display_options.fields.social_event_enrolments_export_enrollments_action.selected_actions');

  if (!empty($selected_actions) && \Drupal::moduleHandler()->moduleExists('social_event_enrolments_export')) {
    // Get the action_key of social_event_enrolments_export_enrollments_action.
    $action_key = array_search('social_event_enrolments_export_enrollments_action', array_column($selected_actions, 'action_id'));
    // Remove 'social_event_enrolments_export_enrollments_action' action.
    // This is added via
    // _social_event_an_enroll_enrolments_export_alter_event_manage_enrollments_view().
    unset($selected_actions[$action_key]);
  }

  $selected_actions[] = [
    'action_id' => 'social_event_an_enroll_enrolments_export_action',
    'preconfiguration' => [
      'label_override' => t('Export'),
    ],
  ];

  $config->set('display.default.display_options.fields.social_views_bulk_operations_bulk_form_enrollments_1.selected_actions', $selected_actions);
  $config->save();
}
