<?php

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\OrderingBundle\Cart;
use KupShop\OrderingBundle\Util\Purchase\PurchaseUtil;

function smarty_function_get_coupon_price($params, &$smarty)
{
    if (empty($params['cart'])) {
        $params['cart'] = ServiceContainer::getService(Cart::class);
    }

    if (empty($params['cart']) || empty($params['coupon'])) {
        throw new InvalidArgumentException('Missing required parameters \'cart\' or \'coupon\'');
    }

    $cart = $params['cart'];
    $purchaseUtil = ServiceContainer::getService(PurchaseUtil::class);
    $couponPrice = $purchaseUtil->getCouponPrice($cart->getPurchaseState(), $params['coupon']);

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