<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Descriptors;

/**
 * ComponentDescriptorRegistry
 *
 * Central registry for Designer component descriptors.
 *
 * This registry is intentionally small for R0007.1. It supports descriptor
 * registration and lookup without changing the existing Designer save/render
 * model.
 *
 * @package PPphp
 * @subpackage Designer\Descriptors
 * @since 0.7.1
 */
class ComponentDescriptorRegistry
{
    /**
     * @var array<string, ComponentDescriptor>
     */
    private array $descriptors = [];

    public static function default(): self
    {
        $registry = new self();

        $registry->register(ButtonDescriptor::make());

        return $registry;
    }

    public function register(ComponentDescriptor $descriptor): void
    {
        $this->descriptors[$descriptor->type()] = $descriptor;
    }

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

    public function get(string $type): ?ComponentDescriptor
    {
        return $this->descriptors[$type] ?? null;
    }

    public function all(): array
    {
        return $this->descriptors;
    }

    public function toArray(): array
    {
        return array_map(
            static fn (ComponentDescriptor $descriptor): array => $descriptor->toArray(),
            $this->descriptors
        );
    }
}
