<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Property;

final class DesignerInspectorGenerator
{
    public function generate(array $component, array $definition, DesignerPropertySchema $schema): array
    {
        $props = (array) ($component['props'] ?? []);
        $sections = [];

        foreach ($schema->toArray()['sections'] as $section) {
            $fields = [];
            foreach ((array) ($section['fields'] ?? []) as $field) {
                $name = (string) ($field['name'] ?? '');
                $fields[] = array_replace($field, [
                    'value' => $name === 'id'
                        ? ($component['id'] ?? '')
                        : ($name === 'label' ? ($component['label'] ?? $definition['title'] ?? '') : ($props[$name] ?? ($field['default'] ?? null))),
                ]);
            }
            $sections[] = array_replace($section, ['fields' => $fields]);
        }

        return [
            'component' => [
                'id' => $component['id'] ?? '',
                'type' => $component['type'] ?? '',
                'label' => $component['label'] ?? '',
            ],
            'definition' => [
                'type' => $definition['type'] ?? '',
                'title' => $definition['title'] ?? '',
                'category' => $definition['category'] ?? '',
                'icon' => $definition['icon'] ?? '',
            ],
            'sections' => $sections,
            'actions' => ['apply', 'reset', 'copy', 'paste', 'duplicate', 'delete'],
        ];
    }
}
