<?php

declare(strict_types=1);

namespace Drupal\nodehive_path_alias\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

use function in_array;

/**
 * Registers NodeHive space API key auth on the router translate-path route.
 *
 * The decoupled_router route only allows a fixed list of auth providers.
 * This subscriber appends our space-API-key provider so that frontend apps
 * can authenticate with their space's Bearer token.
 */
final class RouterPathTranslationRouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection): void {
    $route = $collection->get('decoupled_router.path_translation');
    if ($route === NULL) {
      return;
    }

    $existing = $route->getOption('_auth') ?? [];
    if (!in_array('nodehive_router_space_api_key', $existing, TRUE)) {
      $existing[] = 'nodehive_router_space_api_key';
      $route->setOption('_auth', $existing);
    }
  }

}
