<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media_player\Hook;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Hook\Attribute\Hook;

/**
 * Library-related hook implementations for FFmpeg Media Player.
 */
final class LibraryHooks {

  /**
   * Constructs a LibraryHooks object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory.
   */
  public function __construct(
    protected readonly ConfigFactoryInterface $configFactory,
  ) {}

  /**
   * Implements hook_library_info_alter().
   *
   * Switches Vidstack library between CDN and local based on configuration.
   *
   * @phpstan-param array<string, array<string, mixed>> $libraries
   */
  #[Hook('library_info_alter')]
  public function libraryInfoAlter(array &$libraries, string $extension): void {
    if ($extension !== 'ffmpeg_media_player') {
      return;
    }

    $config = $this->configFactory->get('ffmpeg_media_player.settings');
    $playerSource = $config->get('player_source') ?? 'local';

    // Switch vidstack dependency based on setting.
    if (isset($libraries['vidstack'])) {
      $libraries['vidstack']['dependencies'] = $playerSource === 'local'
        ? ['ffmpeg_media_player/vidstack.local']
        : ['ffmpeg_media_player/vidstack.cdn'];
    }
  }

}
