<?php

declare(strict_types=1);

namespace PpPhp\Core\Diagnostics\Registry;

use PpPhp\Core\Diagnostics\Contracts\DiagnosticProviderInterface;

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

    public function register(DiagnosticProviderInterface $provider): void
    {
        $this->providers[$provider->id()] = $provider;

        uasort(
            $this->providers,
            static fn (DiagnosticProviderInterface $a, DiagnosticProviderInterface $b): int => $a->priority() <=> $b->priority()
        );
    }

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

    public function get(string $id): ?DiagnosticProviderInterface
    {
        return $this->providers[$id] ?? null;
    }

    /** @return DiagnosticProviderInterface[] */
    public function all(): array
    {
        return array_values($this->providers);
    }
}
