<?php

namespace App\Core\Citizenship\Api;

class CitizenshipApiProviderManager
{
    /** @var array<string, class-string> */
    protected array $providers = [];

    public function register(string $code, string $className): void
    {
        $this->providers[$code] = $className;
    }

    public function has(string $code): bool
    {
        return isset($this->providers[$code]);
    }

    public function get(string $code, array $config = []): ?CitizenshipApiProviderInterface
    {
        if (! isset($this->providers[$code])) {
            return null;
        }

        return new $this->providers[$code]($config);
    }

    public function listAvailable(): array
    {
        $list = [];
        foreach ($this->providers as $code => $class) {
            try {
                $list[$code] = (new $class([]))->getProviderInfo();
            } catch (\Throwable) {
                // skip
            }
        }

        return $list;
    }
}
