<?php

/*
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     function
 * Name:     eval
 * Purpose:  evaluate a template variable as a template
 * -------------------------------------------------------------
 */

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\ProductSetsBundle\Utils\ProductCategories;

function smarty_function_insert_products_sets($params, &$smarty)
{
    if (empty($params['id_product'])) {
        trigger_error('products sets: missing product id!');

        return;
    }

    $pc = ServiceContainer::getService(ProductCategories::class);

    $data = [
        'categories' => $pc->getCategoriesWithProducts($params['id_product']),
    ];

    if ($params['assign'] ?? null) {
        $smarty->assign($params['assign'], $data);
    } else {
        $smarty->assign('set', $data);

        $_smarty_tpl_vars = $smarty->tpl_vars;
        echo $smarty->_subTemplateRender($params['template'] ?? 'product.sets.tpl', $smarty->cache_id, $smarty->compile_id, 0, null, [], 0, false);
        $smarty->tpl_vars = $_smarty_tpl_vars;
    }
}
