<?php
/**
 * Magetop
 *
 * @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\Plugin\Checkout\Model;

use Magento\Framework\Exception\LocalizedException;

class Cart
{
    /**
     * @var \Magetop\Quotesystem\Helper\Data
     */
    protected $quoteHelper;
    
    /**
     * @param \Magetop\Quotesystem\Helper\Data $quoteHelper
     */
    public function __construct(
        \Magetop\Quotesystem\Helper\Data $quoteHelper
    ) {
        $this->quoteHelper = $quoteHelper;
    }

    /**
     * Cart Add product before process plugin.
     *
     * @param array     $subject
     * @param array     $productInfo
     * @param array     $requestInfo
     * @return array
     */
    public function beforeAddProduct($subject, $productInfo, $requestInfo = null)
    {
        if ($this->quoteHelper->getModuleStatus()) {
            $quoteStatus = $productInfo->getQuoteStatus();
            $allowAddToCart = (int)$this->quoteHelper->getConfigAddToCart();
            if (($quoteStatus == 1) && !$allowAddToCart) {
                if (array_key_exists('quote_id', $requestInfo)) {
                    return [$productInfo, $requestInfo];
                }
                throw new LocalizedException(__('Add to cart for this product is not allowed'));
            }
        }
        return [$productInfo, $requestInfo];
    }
}
