<?php

declare(strict_types=1);

namespace App\Services\Baselines\SnapshotRendering;

final readonly class SnapshotTypeRendererRegistry
{
    /**
     * @param  array<int, SnapshotTypeRenderer>  $renderers
     */
    public function __construct(
        private array $renderers,
        private SnapshotTypeRenderer $fallbackRenderer,
    ) {}

    public function rendererFor(string $policyType): SnapshotTypeRenderer
    {
        foreach ($this->renderers as $renderer) {
            if ($renderer->supports($policyType)) {
                return $renderer;
            }
        }

        return $this->fallbackRenderer;
    }

    public function fallbackRenderer(): SnapshotTypeRenderer
    {
        return $this->fallbackRenderer;
    }

    public function usesFallback(string $policyType): bool
    {
        return $this->rendererFor($policyType) === $this->fallbackRenderer;
    }
}
