<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Visual;

final class DesignerToolboxVisualRuntime
{
    public function viewModel(array $components = [], array $favorites = [], array $recent = []): array
    {
        return [
            'search' => ['enabled' => true, 'placeholder' => 'Search components...'],
            'favorites' => ['pinned' => true, 'items' => $favorites],
            'recent' => ['pinned' => true, 'items' => $recent],
            'groups' => $this->groupComponents($components),
            'collapsedByDefault' => true,
            'virtualScroll' => true,
            'classes' => ['designer-toolbox-v2'],
        ];
    }

    public function defaultGroups(): array
    {
        return ['Layouts','Data','Forms','Actions','Navigation','Dashboard','Authentication','Advanced'];
    }

    private function groupComponents(array $components): array
    {
        $groups = [];
        foreach ($this->defaultGroups() as $group) {
            $groups[$group] = ['label' => $group, 'collapsed' => true, 'items' => []];
        }
        foreach ($components as $component) {
            $group = (string) ($component['category'] ?? 'Advanced');
            $groups[$group] ??= ['label' => $group, 'collapsed' => true, 'items' => []];
            $groups[$group]['items'][] = $component;
        }
        return $groups;
    }
}
