<?php
declare(strict_types=1);

namespace App\Modules\Shipments;

final class ShipmentProviderRegistry
{
    /**
     * @var array<string, ShipmentProviderInterface>
     */
    private array $providers = [];

    /**
     * @param iterable<int, ShipmentProviderInterface> $providers
     */
    public function __construct(iterable $providers)
    {
        foreach ($providers as $provider) {
            $this->providers[$provider->code()] = $provider;
        }
    }

    public function get(string $code): ?ShipmentProviderInterface
    {
        $key = strtolower(trim($code));
        if ($key === '') {
            return null;
        }

        return $this->providers[$key] ?? null;
    }

    /**
     * @return array<string, ShipmentProviderInterface>
     */
    public function all(): array
    {
        return $this->providers;
    }
}
