<?php

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

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

/**
 * @param array  $params
 * @param Smarty $smarty
 *
 * @return string
 *
 * @throws Exception
 */
function smarty_function_get_old_gtm_data($params, &$smarty)
{
    $GTMService = ServiceContainer::getService(\KupShop\GTMOldBundle\Utils\GTMLoader::class);

    $data = '';
    if (!empty($params['type']) && $params['type'] == 'ecommerce') {
        if (!empty($params['method'])) {
            $data = $GTMService->getEcommerceData($params['method'], $params['data']);
        }
    } else {
        if (!empty($params['view']) && $params['view'] instanceof \KupShop\KupShopBundle\Views\View) {
            $data = $GTMService->getData($params['view']);
        }
    }

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