<?php

namespace App\FieldVisibility;

use InvalidArgumentException;

/**
 * Maps an entity slug to its field catalogue. This is the reuse seam: new entity
 * types (new cars, offers, dealerships) register their own catalogue here.
 */
class FieldCatalogueRegistry
{
    /**
     * @var array<string, FieldCatalogue>
     */
    private array $catalogues = [];

    public function register(FieldCatalogue $catalogue): void
    {
        $this->catalogues[$catalogue->entity()] = $catalogue;
    }

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

    public function for(string $entity): FieldCatalogue
    {
        if (! $this->has($entity)) {
            throw new InvalidArgumentException("No field catalogue registered for entity [{$entity}].");
        }

        return $this->catalogues[$entity];
    }

    /**
     * @return list<string>
     */
    public function entities(): array
    {
        return array_keys($this->catalogues);
    }
}
