<?php

/**
 * @file
 * Install, update and uninstall functions for the thunder installation profile.
 */

use Drupal\Core\Entity\EntityStorageException;
use Drupal\update_helper\UpdateLogger;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

/**
 * Helper function for direct use of update helper checklist service.
 *
 * @param string $update_id
 *   Identifier of update in checklist.
 * @param bool $successful
 *   The success or failure of update.
 *   Used update logger service in update hook.
 */
function _thunder_mark_update_checklist(string $update_id, bool $successful, UpdateLogger $updateLogger): void {
  try {
    /** @var \Drupal\update_helper_checklist\UpdateChecklist $update_checklist */
    $update_checklist = \Drupal::service('update_helper_checklist.update_checklist');

    if ($successful) {
      $update_checklist->markUpdatesSuccessful(['thunder' => [$update_id]]);
    }
    else {
      $update_checklist->markUpdatesFailed(['thunder' => [$update_id]]);
    }
  }
  catch (EntityStorageException $ee) {
    $updateLogger->warning(t('Unable to mark update in checklist.'));
  }
  catch (ServiceNotFoundException $se) {
    // If service is not available, we will just ignore it.
  }
}

/**
 * Implements hook_update_last_removed().
 */
function thunder_update_last_removed(): int {
  return 8327;
}

/**
 * Add publish state and unpublish state fields to newly supported entity types.
 *
 * In Drupal 10.3 taxonomy terms can be moderated.
 */
function thunder_update_8328(): \Stringable|string {
  if (\Drupal::moduleHandler()->moduleExists('scheduler_content_moderation_integration')) {
    $output = _scheduler_content_moderation_integration_add_fields();
    return $output ? implode('</li><li>', $output) : t('No update required.');
  }
  return t('No update required.');
}
