<?php
/**
 * Webkul Software.
 *
 * @category Webkul
 * @package Webkul_MpPayPal
 * @author Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license https://store.webkul.com/license.html
 */
namespace Webkul\MpPayPal\Block;
 
use Magento\Framework\View\Element\Template;
 
class Head extends Template
{
    /**
     * @var \Magento\Framework\View\Asset\Repository
     */
    protected $assetRepository;
 
    /**
     * Header constructor.
     * @param Template\Context $context
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        \Magento\Framework\View\Page\Config $pageConfig,
        \Webkul\MpPayPal\Gateway\Config\Config $configProvider,
        \Webkul\MpPayPal\Helper\Data $helper,
        \Magento\Framework\Encryption\EncryptorInterface $encryptor,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->pageConfig = $pageConfig;
        $this->configProvider = $configProvider;
        $this->helper = $helper;
        $this->encryptor = $encryptor;
        $this->manageScripts();
    }
 
    /**
     * @return void
     */
    public function manageScripts()
    {
        $clientId = $this->encryptor->decrypt(
            $this->configProvider->getClientId()
        );
        $isSellerInCart = $this->helper->checkSellerInCart();
        $isActive = $this->configProvider->isActive();
        $merchantIds = $this->helper->getSellerPayPalMerchantIds();
        $currencyCode = $this->_storeManager->getStore()->getCurrentCurrencyCode();
        if ($clientId && $isActive && $isSellerInCart && count($merchantIds)) {
            $attributes = [];
            if (count($merchantIds) > 1) {
                $attributes = ['attributes' => 'data-merchant-id="'.implode(',', $merchantIds).'"'];
                $merchantId = '*';
            } else {
                $merchantId = implode(',', $merchantIds);
            }
            $this->pageConfig->addRemotePageAsset(
                "https://www.paypal.com/sdk/js?client-id=".$clientId."&merchant-id=".$merchantId."&currency="
                .$currencyCode."&intent=capture&disable-funding=credit,card",
                'js',
                $attributes
            );
        }
    }
}
