{*
* Alsendo - PrestaShop shipping module
*
* @author    Innovation Software
* @copyright 2026 Innovation Software
* @license   https://opensource.org/licenses/AFL-3.0  Academic Free License (AFL 3.0)
*}

<div id="alsendo-payment-pickup-point-info">
    <p>{$alsendo_pickup_point_display|escape:'html':'UTF-8'}</p>
</div>
<script>
    {*
    * Named, idempotent init. OPC modules that re-render the payment step after
    * DOMContentLoaded can call window.alsendoInitPaymentInfo() again safely.
    *}
    window.alsendoInitPaymentInfo = function() {
        var infoDiv = document.getElementById('alsendo-payment-pickup-point-info');
        var carrierNameSpan = document.querySelector('.summary-selected-carrier span.carrier-name');
        if (infoDiv && carrierNameSpan && carrierNameSpan.parentNode) {
            if (carrierNameSpan.nextSibling !== infoDiv) {
                carrierNameSpan.parentNode.insertBefore(infoDiv, carrierNameSpan.nextSibling);
            }
        }
    };
    {* Self-bootstrap + auto re-init: re-run when an OPC module rebuilds the payment step DOM. *}
    (function() {
        function alsendoSafePaymentInit() {
            if (window.__alsendoPayObserver) {
                window.__alsendoPayObserver.disconnect();
            }
            window.alsendoInitPaymentInfo();
            if (window.__alsendoPayObserver && document.body) {
                window.__alsendoPayObserver.observe(document.body, { childList: true, subtree: true });
            }
        }

        function alsendoPaymentBootstrap() {
            if (!window.__alsendoPayObserver && typeof MutationObserver !== 'undefined') {
                var scheduled = false;
                window.__alsendoPayObserver = new MutationObserver(function() {
                    if (scheduled) {
                        return;
                    }
                    scheduled = true;
                    setTimeout(function() {
                        scheduled = false;
                        alsendoSafePaymentInit();
                    }, 150);
                });
            }
            alsendoSafePaymentInit();
        }

        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', alsendoPaymentBootstrap);
        } else {
            alsendoPaymentBootstrap();
        }
    })();
</script>