<?php

/*
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     function
 * Name:     get_slider
 * Purpose:  get a slider of known name or id
 * -------------------------------------------------------------
 */

use KupShop\ContentBundle\Util\InfoPanelLoader;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

/**
 * @param array  $params
 * @param Smarty $smarty
 *
 * @return array
 */
function smarty_function_get_infopanel($params, &$smarty)
{
    $assign = null;
    $ret = false;
    extract($params);

    $infoPanelLoader = ServiceContainer::getService(InfoPanelLoader::class);
    $infoPanel = $infoPanelLoader->loadInfoPanel($type ?? null);

    if (str_contains($infoPanel['body'] ?? '', '{DOPRAVA_ZDARMA_OD}')) {
        $currencyContext = ServiceContainer::getService(CurrencyContext::class);
        $smarty->loadPlugin('Smarty_function_get_stats');
        $freeDelivery = smarty_function_get_stats(['type' => 'free_delivery'], $smarty);
        $infoPanel['body'] = str_replace('{DOPRAVA_ZDARMA_OD}', "{$freeDelivery->asInteger()} {$currencyContext->getActive()->getSymbol()}", $infoPanel['body']);
    }

    if (!empty($infoPanel)) {
        $ret = $infoPanel;
        $ret['text'] = $infoPanel['body'];
    }

    if (!empty($assign)) {
        $smarty->assign($assign, $ret);
    } else {
        return $ret;
    }
}
