<?php

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Util\System\UrlFinder;

function smarty_function_encore_entry_link_tags($params, &$smarty)
{
    if (empty($params['entry'])) {
        trigger_error("encore_entry_link_tags: missing 'entry' parameter");

        return '';
    }

    $defaults = [
        'package' => null,
        'build' => '_default',
    ];

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

    static $renderer = null;
    static $urlFinder = null;

    if (is_null($renderer)) {
        $urlFinder = ServiceContainer::getService(UrlFinder::class);
        $renderer = ServiceContainer::getService('webpack_encore.tag_renderer');
    }

    // Package "cdn" serves assets from configured CDN prefix
    if ($urlFinder->hasCDN()) {
        $params['package'] = 'cdn';
    }

    return $renderer->renderWebpackLinkTags($params['entry'], $params['package'], $params['build']);
}
