<?php

declare(strict_types=1);

namespace Drupal\flowdrop_runtime\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
 * Registers tagged pipeline storage services into PipelineStorageRegistry.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
class RegisterPipelineStoragesPass implements CompilerPassInterface {

  /**
   * {@inheritdoc}
   */
  public function process(ContainerBuilder $container): void {
    if (!$container->hasDefinition('flowdrop_runtime.pipeline_storage_registry')) {
      return;
    }
    $registry = $container->getDefinition('flowdrop_runtime.pipeline_storage_registry');
    foreach ($container->findTaggedServiceIds('flowdrop_runtime.pipeline_storage') as $id => $tags) {
      foreach ($tags as $tag) {
        if (!empty($tag['bundle'])) {
          $registry->addMethodCall('addStorage', [$tag['bundle'], new Reference($id)]);
        }
      }
    }
  }

}
