<?php

namespace App\Serp;

use App\Serp\Contracts\SerpProvider;
use App\Serp\Exceptions\ProviderConfigurationException;

final class SerpProviderRegistry
{
    private array $providers = [];

    public function register(SerpProvider $provider): void
    {
        $this->providers[$provider->key()] = $provider;
    }

    public function resolve(?string $key = null): SerpProvider
    {
        $key = $key ?: config('serp.provider');
        if (! $key || ! isset($this->providers[$key])) {
            throw new ProviderConfigurationException('Unknown provider.');
        }

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