<?php

use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\UserManagerBundle\Util\UserManagerUtil;

function smarty_function_user_manager_insert_users_select($params, &$smarty)
{
    $userContext = Contexts::get(UserContext::class);

    $default = [
        'template' => 'user-manager/block.users-select.tpl',
        'id' => $userContext->getActiveId(),
    ];

    $params = array_merge($default, $params);

    if (empty($params['id'])) {
        throw new InvalidArgumentException('Missing required parameter \'id\'');
    }

    $userManagerUtil = ServiceContainer::getService(UserManagerUtil::class);

    $users = $userManagerUtil->getManagedUsers((int) $params['id'], (bool) ($params['activeOnly'] ?? false));

    if (!empty($params['template'])) {
        $params['users'] = $users;
        $_smarty_tpl_vars = $smarty->tpl_vars;
        echo $smarty->_subTemplateRender($params['template'], $smarty->cache_id, $smarty->compile_id, 0, null, $params, 0, false);
        $smarty->tpl_vars = $_smarty_tpl_vars;
    } elseif (!empty($params['assign'])) {
        $smarty->assign($params['assign'], $users);
    } else {
        return $users;
    }
}
