<?php
declare(strict_types=1);
namespace PpPhp\Packages\Ui\Ppui\Foundation;
use PpPhp\Packages\Ui\Ppui\Foundation\Contracts\RendererInterface;
use PpPhp\Packages\Ui\Ppui\Foundation\Definitions\ComponentDefinition;
use PpPhp\Packages\Ui\Ppui\Foundation\Registries\ComponentRegistry;
final class Renderer implements RendererInterface
{
    public function __construct(private readonly ComponentRegistry $components) {}
    public function render(array $component, array $context = []): string
    {
        $type = (string)($component['type'] ?? '');
        $definition = $this->components->get($type);
        if (!$definition instanceof ComponentDefinition) {
            return '<!-- PPUI: unknown component '.htmlspecialchars($type, ENT_QUOTES, 'UTF-8').' -->';
        }
        $props = is_array($component['props'] ?? null) ? $component['props'] : [];
        $label = htmlspecialchars((string)($props['label'] ?? $definition->label()), ENT_QUOTES, 'UTF-8');
        return '<div class="ppui-component ppui-component-'.htmlspecialchars($type, ENT_QUOTES, 'UTF-8').'" data-ppui-component="'.htmlspecialchars($type, ENT_QUOTES, 'UTF-8').'">'.$label.'</div>';
    }
}
