<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Template for displaying product price at product view page, gift registry and wish-list
 *
 * @var $block \Magento\Msrp\Pricing\Render\PriceBox
 */
?>
<?php

/** @var Magento\Msrp\Pricing\Price\MsrpPriceInterface $priceType */
$priceType = $block->getPrice();

/** @var $product \Magento\Catalog\Model\Product */
$product = $block->getSaleableItem();
$productId = $product->getId();

$amount = $block->getMsrpPriceCalculator()->getMsrpPriceValue($product);

$msrpPrice = $block->renderAmount(
    $priceType->getCustomAmount($amount),
    [
        'price_id' => $block->getPriceId() ? $block->getPriceId() : 'old-price-' . $productId,
        'include_container' => false,
        'skip_adjustments' => true
    ]
);
$priceElementIdPrefix = $block->getPriceElementIdPrefix() ? $block->getPriceElementIdPrefix() : 'product-price-';
?>

<?php if ($amount) : ?>
    <span class="old-price map-old-price"><?= /* @noEscape */ $msrpPrice ?></span>
    <span class="map-fallback-price normal-price"><?= /* @noEscape */ $msrpPrice ?></span>
<?php endif; ?>

<?php if ($priceType->isShowPriceOnGesture()) : ?>
    <?php

    $addToCartUrl = '';
    if ($product->isSaleable()) {
        /** @var Magento\Catalog\Block\Product\AbstractProduct $addToCartUrlGenerator */
        $addToCartUrlGenerator = $block->getLayout()->getBlockSingleton(\Magento\Catalog\Block\Product\AbstractProduct::class);
        // phpcs:disable
        $addToCartUrl = $addToCartUrlGenerator->getAddToCartUrl(
            $product,
            ['_query' => [
                \Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED =>
                    $this->helper(\Magento\Framework\Url\Helper\Data::class)->getEncodedUrl(
                        $addToCartUrlGenerator->getAddToCartUrl($product)
                    ),
            ]]
        );
        // phpcs:enable
    }

    $priceElementId = $priceElementIdPrefix . $productId . $block->getIdSuffix();
    $popupId = 'msrp-popup-' . $productId . $block->getRandomString(20);
    $data = [
        'origin'=> 'msrp',
        'popupId' => '#' . $popupId,
        'productName' => $block->escapeJs($block->escapeHtml($product->getName())),
        'productId' => $productId,
        'productIdInput' => 'input[type="hidden"][name="product"]',
        'realPrice' => $block->getRealPriceHtml(),
        'isSaleable' => $product->isSaleable(),
        'msrpPrice' => $msrpPrice,
        'priceElementId' => $priceElementId,
        'closeButtonId' => '#map-popup-close',
        'addToCartUrl' => $addToCartUrl,
        'paymentButtons' => '[data-label=or]'
    ];
    if ($block->getRequest()->getFullActionName() === 'catalog_product_view') {
        $data['addToCartButton'] = '#product_addtocart_form [type=submit]';
    } else {
        $data['addToCartButton'] = sprintf(
            'form:has(input[type="hidden"][name="product"][value="%s"]) button[type="submit"]',
            (int) $productId . ',' .
            sprintf(
                '.block.widget .price-box[data-product-id=%s]+.product-item-actions button.tocart',
                (int)$productId
            )
        );
    }
    ?>
    <span id="<?= $block->escapeHtmlAttr($block->getPriceId() ? $block->getPriceId() : $priceElementId) ?>" style="display:none"></span>
    <a href="javascript:void(0);"
       id="<?= /* @noEscape */ ($popupId) ?>"
       class="action map-show-info"
       <?php //phpcs:disable ?>
       data-mage-init='{"addToCart":<?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($data) ?>}'>
        <?php //phpcs:enable ?>
        <?= $block->escapeHtml(__('Click for price')) ?>
    </a>
<?php else : ?>
    <span class="msrp-message">
     <?= $block->escapeHtml($priceType->getMsrpPriceMessage()) ?>
    </span>
<?php endif; ?>

<?php if ($block->getZone() == \Magento\Framework\Pricing\Render::ZONE_ITEM_VIEW) : ?>
    <?php $helpLinkId = 'msrp-help-' . $productId . $block->getRandomString(20); ?>
    <a href="javascript:void(0);"
       id="<?= /* @noEscape */ $helpLinkId ?>"
       class="action map-show-info"
       data-mage-init='{"addToCart":{"origin": "info",
                                     "helpLinkId": "#<?= /* @noEscape */ $helpLinkId ?>",
                                     "productName": "<?= $block->escapeJs($block->escapeHtml($product->getName())) ?>",
                                     "closeButtonId": "#map-popup-close"}}'><span><?= $block->escapeHtml(__("What's this?")) ?></span>
    </a>
<?php endif; ?>
