<?php

namespace KupShop\I18nBundle\DependencyInjection\Compiler;

use KupShop\I18nBundle\Util\TranslationLocator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class TranslationPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        if (!$container->has(TranslationLocator::class)) {
            return;
        }

        $locator = $container->findDefinition(TranslationLocator::class);

        $services = $container->findTaggedServiceIds('kupshop.translation');

        $locatableServices = [];
        $servicesList = [];
        foreach ($services as $id => $tags) {
            $locatableServices[$id] = new Reference($id);
            $servicesList[] = $id;
        }

        $locator->addArgument(ServiceLocatorTagPass::register($container, $locatableServices));
        $locator->addArgument($servicesList);
    }
}
