<?php

use KupShop\ComponentsBundle\Utils\ComponentRenderer;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

function smarty_function_render_component($params, &$smarty)
{
    if (!isset($params['name'])) {
        throw new Exception('Missing required parameter: name');
    }

    $renderer = ServiceContainer::getService(ComponentRenderer::class);
    $name = $params['name'];
    $data = $params['data'] ?? [];

    $html = $renderer->renderToString($name, $data);

    if (!empty($params['assign'])) {
        $smarty->assign($params['assign'], $html);
    } else {
        return $html;
    }
}
