<?php

declare(strict_types=1);

namespace PpPhp\Core\Platform\Services\Blueprint;

final class BlueprintCollection
{
    /** @var array<string, ServiceBlueprint> */
    private array $items = [];

    public function add(ServiceBlueprint $blueprint): void
    {
        $this->items[$blueprint->id()] = $blueprint;
    }

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

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

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

    public function toArray(): array
    {
        return array_map(static fn (ServiceBlueprint $blueprint): array => $blueprint->toArray(), $this->all());
    }
}
