<?php

/*
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     function
 * Name:     get_contexts
 * Purpose:  get given contexts
 * -------------------------------------------------------------
 */

use KupShop\KupShopBundle\Context\CountryContext;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Context\DomainContext;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Context\PriceLevelContext;
use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Context\Wrapper\ContextWrapper;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\PricelistBundle\Context\PricelistContext;

/**
 * @param array  $params
 * @param Smarty $smarty
 *
 * @return array
 */
function smarty_function_get_contexts($params, &$smarty)
{
    $ret = [];

    if (isset($params['language'])) {
        $context = Contexts::get(LanguageContext::class);
        $ret['language'] = ContextWrapper::wrap($context);
    }

    if (isset($params['country'])) {
        $context = Contexts::get(CountryContext::class);
        $ret['country'] = ContextWrapper::wrap($context);
    }

    if (isset($params['currency'])) {
        $context = Contexts::get(CurrencyContext::class);
        $ret['currency'] = ContextWrapper::wrap($context);
    }

    if (isset($params['pricelist'])) {
        $context = Contexts::get(PricelistContext::class);
        $ret['pricelist'] = ContextWrapper::wrap($context);
    }

    if (isset($params['domain'])) {
        $context = Contexts::get(DomainContext::class);
        $ret['domain'] = $context;
    }

    if (isset($params['pricelevel'])) {
        $context = Contexts::get(PriceLevelContext::class);
        $ret['pricelevel'] = $context;
    }

    if (isset($params['user'])) {
        $context = Contexts::get(UserContext::class);
        $ret['user'] = $context;
    }

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