<?php

/**
 * @file
 * Update hooks and schema definition for the update_test_schema module.
 */

declare(strict_types=1);

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Update\EquivalentUpdate;

if (\Drupal::state()->get('equivalent_update_test_last_removed', FALSE)) {

  /**
   * Implements hook_update_last_removed().
   */
  function equivalent_update_test_update_last_removed(): int {
    return \Drupal::state()->get('equivalent_update_test_update_last_removed', 100000);
  }

  /**
   * Schema version 100001.
   *
   * A regular update.
   */
  function equivalent_update_test_update_100001(): void {
  }

}
else {

  /**
   * Schema version 100000.
   *
   * Used to determine the initial schema version.
   */
  function equivalent_update_test_update_100000(): void {
    throw new \Exception('This code should never be reached.');
  }

}


if (\Drupal::state()->get('equivalent_update_test_update_100002', FALSE)) {

  /**
   * Schema version 100002.
   *
   * Tests that the future update 100101 can be marked as an equivalent.
   */
  function equivalent_update_test_update_100002(): void {
    \Drupal::service('update.update_hook_registry')->markFutureUpdateEquivalent(100101, '11.1.0');
  }

}

if (\Drupal::state()->get('equivalent_update_test_update_100101', FALSE)) {

  /**
   * Schema version 100101.
   *
   * This update will be skipped due 100002.
   */
  function equivalent_update_test_update_100101(): void {
    throw new \Exception('This code should never be reached.');
  }

}

if (\Drupal::state()->get('equivalent_update_test_update_100201', FALSE)) {

  /**
   * Schema version 100201.
   *
   * This update tests that updates can be skipped using inline code.
   */
  function equivalent_update_test_update_100201(): TranslatableMarkup|null {
    \Drupal::service('update.update_hook_registry')->markFutureUpdateEquivalent(100201, '11.1.0');
    // Test calling the getEquivalentUpdate() method in an update function to
    // ensure it correctly determines the update number.
    $equivalent_update = \Drupal::service('update.update_hook_registry')->getEquivalentUpdate();
    if ($equivalent_update instanceof EquivalentUpdate) {
      return $equivalent_update->toSkipMessage();
    }
    throw new \Exception('This code should never be reached.');
  }

}

if (\Drupal::state()->get('equivalent_update_test_update_100301', FALSE)) {

  /**
   * Schema version 100301.
   *
   * This update tests that inline code can determine the update number
   * correctly and return a NULL when it does not match.
   */
  function equivalent_update_test_update_100301(): void {
    \Drupal::service('update.update_hook_registry')->markFutureUpdateEquivalent(100302, '11.1.0');
    // Test calling the getEquivalentUpdate() method in an update function to
    // ensure it correctly determines the update number.
    $equivalent_update = \Drupal::service('update.update_hook_registry')->getEquivalentUpdate();
    if ($equivalent_update instanceof EquivalentUpdate) {
      throw new \Exception('This code should never be reached.');
    }
  }

  /**
   * Schema version 100302.
   *
   * This update will be skipped by 100301.
   */
  function equivalent_update_test_update_100302(): void {
    throw new \Exception('This code should never be reached.');
  }

}

if (\Drupal::state()->get('equivalent_update_test_update_100400', FALSE)) {

  /**
   * Schema version 100400.
   *
   * Tests that the future update 100402 can be marked as an equivalent.
   */
  function equivalent_update_test_update_100400(): void {
    \Drupal::service('update.update_hook_registry')->markFutureUpdateEquivalent(100402, '11.2.0');
  }

}

if (\Drupal::state()->get('equivalent_update_test_update_100401', FALSE)) {

  /**
   * Schema version 100401.
   *
   * Tests that the future update 100402 can be marked as an equivalent again.
   */
  function equivalent_update_test_update_100401(): void {
    \Drupal::service('update.update_hook_registry')->markFutureUpdateEquivalent(100402, '11.2.0');
  }

}

if (\Drupal::state()->get('equivalent_update_test_update_100402', FALSE)) {

  /**
   * Schema version 100402.
   *
   * This update will be skipped by 100400 and 100401.
   */
  function equivalent_update_test_update_100402(): void {
    throw new \Exception('This code should never be reached.');
  }

}

if (\Drupal::state()->get('equivalent_update_test_update_100501', FALSE)) {

  /**
   * Schema version 100501.
   *
   * This update will trigger an exception because 100501 is bigger than 100302.
   */
  function equivalent_update_test_update_100501(): void {
    \Drupal::service('update.update_hook_registry')->markFutureUpdateEquivalent(100302, '11.1.0');
  }

}
