<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Compatibility;

use PpPhp\Core\Platform\Compatibility\Contracts\CompatibilityAdapterInterface;

final class CompatibilityRegistry
{
    private array $adapters = [];
    private array $notices = [];

    public function register(CompatibilityAdapterInterface $adapter): void
    {
        $this->adapters[$adapter->id()] = $adapter;
    }

    public function has(string $id): bool { return isset($this->adapters[$id]); }
    public function get(string $id): ?CompatibilityAdapterInterface { return $this->adapters[$id] ?? null; }
    public function all(): array { return array_values($this->adapters); }
    public function addNotice(DeprecationNotice $notice): void { $this->notices[] = $notice; }
    public function notices(): array { return $this->notices; }

    public function toArray(): array
    {
        return [
            'adapters' => array_map(static fn (CompatibilityAdapterInterface $a): array => $a->toArray(), $this->all()),
            'notices' => array_map(static fn (DeprecationNotice $n): array => $n->toArray(), $this->notices),
        ];
    }
}
