<?php

/*
 * This file is part of the VitiCore 2.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Manager;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Provider\Carrier\CarrierProviderInterface;
use CpCreation\VitiCore\Shop\Model\ShippingConfig;

final class CarrierProviderManager
{
    /** @var list<CarrierProviderInterface> */
    private array $providers = [];

    public function addProvider(CarrierProviderInterface $provider): void
    {
        $this->providers[] = $provider;
    }

    /**
     * @throws \RuntimeException
     */
    public function createProviderManager(ShippingConfig $config): CarrierProviderInterface
    {
        $carrier = $config->getCarrier();

        foreach ($this->providers as $provider) {
            if ($provider->getCarrier() === $carrier) {
                $provider->setData($config);

                return $provider;
            }
        }

        throw new \RuntimeException('Pas de provider de transporteur trouvé');
    }
}
