<?php
/**
 * Magetop Store
 *
 * @category  Magetop Extensions
 * @package   Magetop_Quotesystem
 * @author    Magetop Developer
 * @copyright Copyright (c) Magetop (https://www.magetop.com)
 * @license   https://www.magetop.com/license.html
 */

namespace Magetop\Quotesystem\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class RemoveBlockForDiscount implements ObserverInterface
{
    /**
     * @var Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $_scopeConfig;

    /**
     * @var \Magetop\Quotesystem\Helper\Data
     */
    protected $quoteHelper;

    /**
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Magetop\Quotesystem\Helper\Data                    $quoteHelper
     */
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magetop\Quotesystem\Helper\Data $quoteHelper
    ) {
        $this->_scopeConfig = $scopeConfig;
        $this->quoteHelper = $quoteHelper;
    }

    /**
     * Remove Block For Discount action
     *
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        /**
         * @var \Magento\Framework\View\Layout $layout
         */
        $layout = $observer->getLayout();
        $block = $layout->getBlock('checkout.cart.coupon');

        if ($block) {
            if (!$this->quoteHelper->getDiscountEnable() && $this->quoteHelper->checkQuoteProductIsInCart()) {
                $layout->unsetElement('checkout.cart.coupon');
            }
        }
        return $this;
    }
}
