<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Component;

final class DesignerToolboxRuntime
{
    public function __construct(private readonly DesignerComponentRegistry $registry) {}

    public function toolbox(): array
    {
        $groups = [];
        foreach ($this->registry->categories() as $category) {
            $groups[] = [
                'category' => $category->toArray(),
                'components' => array_map(static fn($d) => $d->toArray(), $this->registry->byCategory($category->categoryId())),
            ];
        }
        return $groups;
    }

    public function search(string $query): array
    {
        return array_map(static fn($d) => $d->toArray(), $this->registry->search($query));
    }
}
