<?php

/**
 * @file
 * The module file.
 */

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\thunder_workflow\ThunderWorkflowFormHelper;

/**
 * Implements hook_entity_base_field_info_alter().
 */
function thunder_workflow_entity_base_field_info_alter(array &$fields, EntityTypeInterface $entity_type): void {
  if (\Drupal::config('system.theme')->get('admin') === 'thunder_admin') {
    /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
    $moderation_info = \Drupal::service('content_moderation.moderation_information');
    if ($moderation_info->canModerateEntitiesOfEntityType($entity_type) && isset($fields['moderation_state'])) {
      $fields['moderation_state']->setDisplayOptions('form', [
        'type' => 'thunder_moderation_state_default',
        'weight' => 100,
        'settings' => [],
      ]);
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function thunder_workflow_form_node_form_alter(array &$form, FormStateInterface $form_state): void {
  \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(ThunderWorkflowFormHelper::class)
    ->formAlter($form, $form_state);
}
