<?php
declare(strict_types=1);

namespace ROITheme\Admin\Shared\Infrastructure\FieldMapping;

use ROITheme\Admin\Shared\Domain\Contracts\FieldMapperInterface;

/**
 * Registro central de Field Mappers
 *
 * RESPONSABILIDAD:
 * - Registrar mappers de cada modulo
 * - Resolver mapper por nombre de componente
 *
 * PRINCIPIOS:
 * - OCP: Nuevos mappers se registran sin modificar esta clase
 * - SRP: Solo gestiona el registro, no contiene mapeos
 */
final class FieldMapperRegistry
{
    /** @var array<string, FieldMapperInterface> */
    private array $mappers = [];

    /**
     * Registra un mapper
     */
    public function register(FieldMapperInterface $mapper): void
    {
        $this->mappers[$mapper->getComponentName()] = $mapper;
    }

    /**
     * Obtiene un mapper por nombre de componente
     *
     * @throws \InvalidArgumentException Si no existe mapper para el componente
     */
    public function getMapper(string $componentName): FieldMapperInterface
    {
        if (!isset($this->mappers[$componentName])) {
            throw new \InvalidArgumentException(
                "No field mapper registered for component: {$componentName}"
            );
        }

        return $this->mappers[$componentName];
    }

    /**
     * Verifica si existe mapper para un componente
     */
    public function hasMapper(string $componentName): bool
    {
        return isset($this->mappers[$componentName]);
    }

    /**
     * Obtiene todos los mappers registrados
     *
     * @return array<string, FieldMapperInterface>
     */
    public function getAllMappers(): array
    {
        return $this->mappers;
    }
}
