<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\Hook;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Hook\Attribute\Hook;

/**
 * Form-related hook implementations.
 */
final class FormHooks {

  /**
   * Implements hook_form_alter().
   *
   * Attaches shared admin styles to all FFmpeg Media config forms.
   *
   * @param array<string, mixed> $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   * @param string $form_id
   *   The form ID.
   */
  #[Hook('form_alter')]
  public function formAlter(array &$form, FormStateInterface $form_state, string $form_id): void {
    if (str_starts_with($form_id, 'ffmpeg_media')) {
      $form['#attached']['library'][] = 'ffmpeg_media/admin';
    }
  }

  /**
   * Attaches formatter settings JS to the Manage Display form.
   *
   * The library must load with the page (not via AJAX) so the jQuery
   * delegation handler is registered before the formatter settings
   * subform is opened.
   *
   * @param array<string, mixed> $form
   *   The form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   * @param string $form_id
   *   The form ID.
   */
  #[Hook('form_entity_view_display_edit_form_alter')]
  public function entityViewDisplayFormAlter(array &$form, FormStateInterface $form_state, string $form_id): void {
    $form['#attached']['library'][] = 'ffmpeg_media/formatter_settings';
  }

}
