<?php

/**
 * @file
 * Contains hook implementations for the sobki_background_image module.
 */

declare(strict_types=1);

use Drupal\Core\Form\FormStateInterface;
use Drupal\block\BlockInterface;
use Drupal\sobki_background_image\HookHandler\BlockPresave;
use Drupal\sobki_background_image\HookHandler\FormBlockFormAlter;
use Drupal\sobki_background_image\HookHandler\FormLayoutBuilderBlockAlter;
use Drupal\sobki_background_image\HookHandler\PreprocessBlock;

/**
 * Implements hook_ENTITY_TYPE_presave() for 'block'.
 */
function sobki_background_image_block_presave(BlockInterface $entity): void {
  /** @var \Drupal\sobki_background_image\HookHandler\BlockPresave $instance */
  $instance = \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(BlockPresave::class);
  $instance->setThirdPartySettings($entity);
}

/**
 * Implements hook_form_FORM_ID_alter() for 'block_form'.
 */
function sobki_background_image_form_block_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  /** @var \Drupal\sobki_background_image\HookHandler\FormBlockFormAlter $instance */
  $instance = \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(FormBlockFormAlter::class);
  $instance->blockFormAlter($form, $form_state);
}

/**
 * Implements hook_form_FORM_ID_alter() for 'layout_builder_add_block'.
 */
function sobki_background_image_form_layout_builder_add_block_alter(array &$form, FormStateInterface $form_state): void {
  /** @var \Drupal\sobki_background_image\HookHandler\FormLayoutBuilderBlockAlter $instance */
  $instance = \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(FormLayoutBuilderBlockAlter::class);
  $instance->formAlter($form, $form_state);
}

/**
 * Implements hook_form_FORM_ID_alter() for 'layout_builder_update_block'.
 */
function sobki_background_image_form_layout_builder_update_block_alter(array &$form, FormStateInterface $form_state): void {
  /** @var \Drupal\sobki_background_image\HookHandler\FormLayoutBuilderBlockAlter $instance */
  $instance = \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(FormLayoutBuilderBlockAlter::class);
  $instance->formAlter($form, $form_state);
}

/**
 * Implements hook_preprocess_HOOK() for 'block'.
 */
function sobki_background_image_preprocess_block(array &$variables): void {
  /** @var \Drupal\sobki_background_image\HookHandler\PreprocessBlock $instance */
  $instance = \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(PreprocessBlock::class);
  $instance->preprocess($variables);
}
