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

/**
 * @var Magento\PageBuilder\Block\GoogleMapsApi $block
 * @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
 * @var \Magento\Framework\Escaper $escaper
 */

$libraryUrl = $escaper->escapeJs($block->getLibraryUrl());
$style = $escaper->escapeJs($block->getStyle());
$apiKey = $escaper->escapeJs($block->getApiKey());
$invalidApiKeyMessage = $escaper->escapeJs($block->getInvalidApiKeyMessage());

$script = <<<SCRIPT
    require.config({
        paths: {
            googleMaps: '{$libraryUrl}'
        },
        config: {
            'Magento_PageBuilder/js/utils/map': {
                style: '{$style}',
            },
            'Magento_PageBuilder/js/content-type/map/preview': {
                apiKey: '{$apiKey}',
                apiKeyErrorMessage: '{$invalidApiKeyMessage}'
            },
            'Magento_PageBuilder/js/form/element/map': {
                apiKey: '{$apiKey}',
                apiKeyErrorMessage: '{$invalidApiKeyMessage}'
            },
        }
    });
SCRIPT;
// phpcs:ignore
echo /* @noEscape */ $secureRenderer->renderTag('script', [], $script, false);

/** Include the googleMaps dependency only if we have an API key set, this removes unnecessary calls to Google */
if ($block->shouldIncludeGoogleMapsLibrary()) {
    $script = <<<SCRIPT

    require.config({
        shim: {
            'Magento_PageBuilder/js/utils/map': {
                deps: ['googleMaps']
            }
        }
    });
SCRIPT;

    // phpcs:ignore
    echo /* @noEscape */ $secureRenderer->renderTag('script', [], $script, false);
}
