<?php

declare (strict_types=1);
namespace GuardDog\Vendor\Webauthn\ClientDataCollector;

use GuardDog\Vendor\Webauthn\AuthenticatorResponse;
use GuardDog\Vendor\Webauthn\CollectedClientData;
use GuardDog\Vendor\Webauthn\Exception\AuthenticatorResponseVerificationException;
use GuardDog\Vendor\Webauthn\PublicKeyCredentialOptions;
use function in_array;
final class ClientDataCollectorManager
{
    /**
     * @param ClientDataCollector[] $clientDataCollectors
     */
    public function __construct(private readonly iterable $clientDataCollectors)
    {
    }
    public function collect(CollectedClientData $collectedClientData, PublicKeyCredentialOptions $publicKeyCredentialOptions, AuthenticatorResponse $authenticatorResponse, string $host): void
    {
        foreach ($this->clientDataCollectors as $clientDataCollector) {
            if (in_array($collectedClientData->type, $clientDataCollector->supportedTypes(), \true)) {
                $clientDataCollector->verifyCollectedClientData($collectedClientData, $publicKeyCredentialOptions, $authenticatorResponse, $host);
                return;
            }
        }
        throw AuthenticatorResponseVerificationException::create('No client data collector found.');
    }
}
