// source --> https://sportfood.gr/wp-content/plugins/wt-smart-coupons-for-woocommerce/public/js/wt-smart-coupon-public.js?ver=2.3.0 
jQuery(function ($) {
    "use strict";
    $('form.checkout').on('change.wt_sc_payment_method_change', 'input[name="payment_method"]', function () {

        let t = {updateTimer: !1, dirtyInput: !1,
            reset_update_checkout_timer: function () {
                clearTimeout(t.updateTimer)
            }, trigger_update_checkout: function () {
                t.reset_update_checkout_timer(), t.dirtyInput = !1,
                        $(document.body).trigger("update_checkout")
            }
        };
        t.trigger_update_checkout();
        
    });


    $('document').ready(function(){
        
        /* After the coupon box click event was done */
        $(document).on("wt_sc_api_coupon_click_done", function(e){
            wt_unblock_node($( 'div.wt_coupon_wrapper'));
            wt_unblock_node($("div.wt-mycoupons"));
            wt_unblock_node($("div.wt_store_credit"));
        });

        /** Handle keyboard Enter press on coupons */
        $( document ).on("keypress", '.wt-single-coupon.active-coupon', function(e){
            if( 13 === e.which ) { // Enter key
                $( this ).trigger( 'click' );
            }
        } );

        $(document).on("click", '.wt-single-coupon.active-coupon', function(){        
            
            if(!$('.woocommerce-notices-wrapper').length){
                $('#main').prepend('<div class="woocommerce-notices-wrapper"></div>');
            }
            
            const coupon_code = ( typeof $(this).attr('data-code') === 'undefined' ? $(this).find('code').text() : $(this).attr('data-code') );
            const coupon_id = $(this).attr('data-id');

            $('div.wt_coupon_wrapper, div.wt_store_credit').each(function(){
                if($(this).find('.wt-single-coupon').length)
                {
                    wt_block_node($(this));
                }
            });

            /* For checkout block compatibility */
            if( $('.wc-block-checkout, .wc-block-cart').length ) {
                
                const coupon_click_event = new CustomEvent("wt_sc_api_coupon_clicked", {
                    detail:{ 'coupon_code' : coupon_code, 'coupon_id': coupon_id}
                });
                document.dispatchEvent(coupon_click_event);
                return false;
            }
            
            const data = {
                'coupon_code'   : coupon_code,
                'coupon_id'     : coupon_id,
                '_wpnonce'      : WTSmartCouponOBJ.nonces.apply_coupon
            };

            $.ajax({
                type: "POST",
                async: true,
                url: WTSmartCouponOBJ.wc_ajax_url + 'apply_coupon_on_click',
                data: data,
                success: function (response) {
                    if ( $( '.woocommerce-cart-form' ).length) {
                        update_cart(true);  // need only for cart page
                    }
                    
                    wt_unblock_node( $( 'div.wt_coupon_wrapper' ) );
                    wt_unblock_node($("div.wt-mycoupons"));
                    wt_unblock_node($("div.wt_store_credit"));

                    $( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
                    show_notice( response );
                    $(document.body).trigger("update_checkout");
                    $( document.body ).trigger("applied_coupon");

                    $('html, body').animate({
                        scrollTop: $(".woocommerce").offset().top
                    }, 1000);
                }
            });

        });

        /* For checkout block */
        if( $('.wc-block-checkout').length ) {
            $( document ).on("click", '[name="radio-control-wc-payment-method-options"]', function(){
                WTSmartCouponOBJ.payment_method = $('[name="radio-control-wc-payment-method-options"]:checked').val();
                let parent_div = $('[name="radio-control-wc-payment-method-options"]').parents('.wc-block-components-radio-control');
                parent_div.find('.wc-block-components-radio-control__option').removeClass('wc-block-components-radio-control__option-checked');
                wbte_set_block_checkout_values();
            });

            setTimeout( wbte_set_block_checkout_values, 200);
        }
    
    });

    /** Update payment method on session, if coupons are changed update checkout */
    $( document.body ).on( 'payment_method_selected', 
        function() {
            jQuery( document ).on( "updated_checkout", 
                function(){
                    const selectedPaymentMethod = $( 'form.checkout' ).find( 'input[name="payment_method"]:checked' ).val();
                    $.ajax({
                        type: "POST",
                        url: WTSmartCouponOBJ.wc_ajax_url + 'wbte_sc_update_payment_method_on_session',
                        data: {
                            'payment_method': selectedPaymentMethod,
                            '_wpnonce': WTSmartCouponOBJ.nonces.public
                        },
                        success: function( response ) {
                            if ( response ) {
                                $( 'form.checkout' ).trigger( 'update' );
                            }
                        }
                    });
                }
            );
        }
    );

    const wbte_set_block_checkout_values = function() {

        let payment_method = '';
        const shipping_method = {};
      
        if( $('[name="radio-control-wc-payment-method-options"]').length ) {
            
            /* Prepare payment method from radio button */
            payment_method = $('[name="radio-control-wc-payment-method-options"]:checked').val();           

        }
        
        /* Prepare shipping method from radio button */
        if( $('.wc-block-components-shipping-rates-control input[type="radio"]').length ) {         
            $('.wc-block-components-shipping-rates-control input[type="radio"]:checked').each(function(index){
                shipping_method[index] = $(this).val(); 
            });
        }

        /* Store the value to global variable to prevent future auto refresh blocking */
        WTSmartCouponOBJ.shipping_method = shipping_method;
        WTSmartCouponOBJ.payment_method = payment_method;

        
        /* Send ajax request to set the value */
        let order_summary_block = $('.wp-block-woocommerce-checkout-order-summary-block');
        wt_block_node( order_summary_block );
        
        $.ajax({
            type: "POST",
            async: true,
            url: WTSmartCouponOBJ.wc_ajax_url + 'wbte_sc_set_block_checkout_values',
            data: { '_wpnonce': WTSmartCouponOBJ.nonces.public, 'payment_method': payment_method, 'shipping_method': shipping_method },
            dataType: 'json',
            success:function( data ) {
                wt_unblock_node( order_summary_block );
            },
            error:function() {
               wt_unblock_node( order_summary_block ); 
            }
        });

        /* Trigger checkout block refresh */
        setTimeout(function(){ 
            const checkout_value_updated_event = new CustomEvent("wbte_sc_checkout_value_updated", {
                detail:{ 'payment_method' : payment_method, 'shipping_method': shipping_method }
            });
            document.dispatchEvent(checkout_value_updated_event);
        }, 1000);
        
    }

    
    /**
     * Function from cart.js by woocommmerce
     * @param {bool} preserve_notices 
     */
    const update_cart = function( preserve_notices ) {
        const $form = $( '.woocommerce-cart-form' );
        wt_block_node( $form );
        wt_block_node( $( 'div.cart_totals' ) );
        
        

        // Make call to actual form post URL.
        $.ajax( {
            type:     $form.attr( 'method' ),
            url:      $form.attr( 'action' ),
            data:     $form.serialize(),
            dataType: 'html',
            success:  function( response ) {
                update_wc_div( response, preserve_notices );
            },
            complete: function() {
                wt_unblock_node( $form );
                wt_unblock_node( $( 'div.cart_totals' ) );
            }
        } );
    }


    /**
     * 
     * @param {string} html_str 
     * @param {bool} preserve_notices 
     */
    const update_wc_div = function( html_str, preserve_notices ) {
        const $html       = $.parseHTML( html_str );
        const $new_form   = $( '.woocommerce-cart-form', $html );
        const $new_totals = $( '.cart_totals', $html );
        const $notices    = $( '.woocommerce-error, .woocommerce-message, .woocommerce-info', $html );

        // No form, cannot do this.
        if ( $( '.woocommerce-cart-form' ).length === 0 ) {
            window.location.href = window.location.href;
            return;
        }

        // Remove errors
        if ( ! preserve_notices ) {
            $( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
        }

        if ( $new_form.length === 0 ) {
            // If the checkout is also displayed on this page, trigger reload instead.
            if ( $( '.woocommerce-checkout' ).length ) {
                window.location.href = window.location.href;
                return;
            }

            // No items to display now! Replace all cart content.
            const $cart_html = $( '.cart-empty', $html ).closest( '.woocommerce' );
            $( '.woocommerce-cart-form__contents' ).closest( '.woocommerce' ).replaceWith( $cart_html );

            // Display errors
            if ( $notices.length > 0 ) {
                show_notice( $notices );
            }
        } else {
            // If the checkout is also displayed on this page, trigger update event.
            if ( $( '.woocommerce-checkout' ).length ) {
                $( document.body ).trigger( 'update_checkout' );
            }

            $( '.woocommerce-cart-form' ).replaceWith( $new_form );
            $( '.woocommerce-cart-form' ).find( ':input[name="update_cart"]' ).prop( 'disabled', true );

            if ( $notices.length > 0 ) {
                show_notice( $notices );
            }

            update_cart_totals_div( $new_totals );
        }

        $( document.body ).trigger( 'updated_wc_div' );
    };
    

    /**
     * Function from woocmmerce cart.js
     * @param {string} html_str 
     */
    const update_cart_totals_div = function( html_str ) {
        $( '.cart_totals' ).replaceWith( html_str );
        $( document.body ).trigger( 'updated_cart_totals' );
    };



    /**
     * function from cart.js by wooocommerce
     * @param { jQuery object } node 
     */
    const wt_block_node = function( node ) {

        node.addClass( 'processing' );

        if(typeof $.fn.block === 'function')
        {
            node.block({
                message: null,
                overlayCSS: {
                    background: '#fff',
                    opacity: 0.6
                }
            });
        }
    }
    window.wbte_sc_block_node = wt_block_node;
    
    /**
     * function from cart.js by wooocommerce
     * @param {jQuery object} $node 
     */
    const wt_unblock_node = function( node ) {
        
        node.removeClass( 'processing' );
        
        if(typeof $.fn.unblock === 'function')
        {
            node.unblock();
        }      
    };
    window.wbte_sc_unblock_node = wt_unblock_node;


    const show_notice = function( html_element, $target ) {
        if ( ! $target ) {
            $target = $( '.woocommerce-notices-wrapper:first' ) || $( '.cart-empty' ).closest( '.woocommerce' ) || $( '.woocommerce-cart-form' );
        }
        $target.prepend( html_element );
    };

});
// source --> https://sportfood.gr/wp-content/plugins/translatepress-multilingual/assets/js/trp-frontend-compatibility.js?ver=3.2 
document.addEventListener("DOMContentLoaded", function(event) {
    function trpClearWooCartFragments(){

        // clear WooCommerce cart fragments when switching language
        var trp_language_switcher_urls = document.querySelectorAll(".trp-language-switcher-container a:not(.trp-ls-disabled-language), .trp-language-item:not(.trp-language-item__current)");

        for (var i = 0; i < trp_language_switcher_urls.length; i++) {
            trp_language_switcher_urls[i].addEventListener("click", function(){
                if ( typeof wc_cart_fragments_params !== 'undefined' && typeof wc_cart_fragments_params.fragment_name !== 'undefined' ) {
                    window.sessionStorage.removeItem(wc_cart_fragments_params.fragment_name);
                }
            });
        }
    }

    trpClearWooCartFragments();
});
// source --> https://sportfood.gr/wp-content/plugins/cartasi-x-pay/assets/js/xpay.js?ver=8.3.3 
/**
 * Copyright (c) 2019 Nexi Payments S.p.A.
 *
 * @author      iPlusService S.r.l.
 * @category    Payment Module
 * @package     Nexi XPay
 * @version     6.0.0
 * @copyright   Copyright (c) 2019 Nexi Payments S.p.A. (https://ecommerce.nexi.it)
 * @license     GNU General Public License v3.0
 */

function checkApplePay($) {
    if (!window.ApplePaySession) {
        if ($(".payment_method_xpay_applepay").length) {
            $(".payment_method_xpay_applepay").remove();
        }

        if ($(".payment_method_xpay_npg_applepay").length) {
            $(".payment_method_xpay_npg_applepay").remove();
        }
    }
}

(function ($) {
    $(document).ready(function () {
        receipt_form = $("#nexi_xpay_receipt_form");

        if (receipt_form.length > 0) {
            receipt_form.submit();
        }

        checkApplePay($);

        setInterval(function () {
            checkApplePay($);
        }, 700);

        $(document).on("change", "#pagodil-installments-number", function () {
            window.localStorage.setItem(
                "lastSelectedInstallments",
                parseInt($("#pagodil-installments-number").val()),
            );

            installmentsCalc();

            if ($("#installments")) {
                $("#installments").val($("#pagodil-installments-number").val());
            }
        });

        jQuery("form.checkout").on("change", 'input[name="payment_method"]', function () {
            if (
                jQuery('input[name="payment_method"]:checked').val() === "xpay_googlepay_button" ||
                jQuery('input[name="payment_method"]:checked').val() === "xpay_applepay_button"
            ) {
                jQuery("#place_order").hide();
            } else {
                jQuery("#place_order").show();
            }
        });
    });
})(jQuery);

function installmentsCalc() {
    var admin_url = jQuery("#xpay_admin_url").val();

    jQuery.ajax({
        type: "POST",
        data: {
            action: "calc_installments",
            installments: jQuery("#pagodil-installments-number").val(),
        },
        url: admin_url + "admin-ajax.php",
        success: function (response) {
            jQuery("#pagodil-installment-info").html(response.installmentsLabel);
        },
        complete: function () {},
    });
};
// source --> https://sportfood.gr/wp-content/plugins/translatepress-multilingual/assets/js/trp-frontend-language-switcher.js?ver=3.2 
class BaseSwitcher {
    constructor(rootEl) {
        this.root = rootEl;
        this.list = rootEl?.querySelector('.trp-switcher-dropdown-list') || null;
        this.isOpen = false;
        this._pendingFocusOnOpen = false;
        if (!this.root || !this.list) return;

        if (!this.list.id) {
            this.list.id = `trp-list-${Math.random().toString(36).slice(2, 9)}`;
        }

        // Single transitionend handler to drop .is-transitioning and finalize hidden/inert
        this._onTe = (e) => {
            if (e.target !== this.list || e.propertyName !== 'max-height') return;

            this.root.classList.remove('is-transitioning');

            if (!this.isOpen) {
                this.list.hidden = true;
                this.list.setAttribute('inert', '');
            } else if (this._pendingFocusOnOpen) {
                this._pendingFocusOnOpen = false;
                const first = this.list.querySelector(
                    '[role="option"], a, button, [tabindex]:not([tabindex="-1"])'
                );
                first?.focus?.({ preventScroll: true });
            }
        };
        this.list.addEventListener('transitionend', this._onTe);

        this.collapse();
        this.setAutoWidth();
        this.bindKeyboard();
    }

    /**
     * Returns true if the list has a non-zero transition duration (for any property).
     * We use this to decide whether to rely on `transitionend` or fall back to sync behavior.
     */
    _hasAnimatedTransition() {
        if (!this.list) return false;

        const cs = getComputedStyle(this.list);
        const durationsRaw = cs.transitionDuration || '';

        if (!durationsRaw) return false;

        const durations = durationsRaw
            .split(',')
            .map(str => parseFloat(str) || 0);

        return durations.some(d => d > 0);
    }

    collapse() {
        this.list.hidden = true;
        this.list.setAttribute('inert', '');
        this.setExpanded(false);
        this.root.classList.remove('is-transitioning');
    }

    /**
     * If width is set to auto, fix it to the calculated width + 20px
     * We do this in order to avoid width shift on hover
     * */
    setAutoWidth() {
        const bonusWidth = 10;

        const cs = getComputedStyle(this.root);
        const declaredWidth = cs.getPropertyValue('--switcher-width').trim();

        if (declaredWidth === 'auto' && this.root.querySelector('.trp-language-item-name')) { // In case trp-language-item-name is not present, we are in flags only mode - so no auto width sizing is needed
            const initialWidth = this.root.getBoundingClientRect().width;

            this.root.style.setProperty('--switcher-width', (initialWidth + bonusWidth) + 'px');
        }
    }

    setExpanded(open) {
        const trigger = this.root.querySelector('.trp-language-item__current[role="button"]');
        const val = String( !!open );
        trigger?.setAttribute('aria-expanded', val);
        this.root.classList.toggle('is-open', !!open);
    }

    setOpen( open, { source = null } = {} ) {
        if ( !this.root || !this.list || open === this.isOpen ) return;

        const prefersReduced = window.matchMedia?.('(prefers-reduced-motion: reduce)')?.matches;
        const hasTransition  = !prefersReduced && this._hasAnimatedTransition();

        this.isOpen = open;

        // No transitions (0s duration) OR reduced motion: do everything synchronously,
        if ( !hasTransition ) {
            if ( open ) {
                this.list.hidden = false;
                this.list.removeAttribute( 'inert' );
                this.setExpanded( true );

                this._pendingFocusOnOpen = ( source?.type === 'keydown' );
                if ( this._pendingFocusOnOpen ) {
                    this._pendingFocusOnOpen = false;
                    const first = this.list.querySelector(
                        '[role="option"], a, button, [tabindex]:not([tabindex="-1"])'
                    );
                    first?.focus?.({ preventScroll: true });
                }
            } else {
                this.setExpanded( false );
                this.list.hidden = true;
                this.list.setAttribute( 'inert', '' );
                this._pendingFocusOnOpen = false;
            }
            return;
        }

        // Animated path: rely on transitionend to remove .is-transitioning
        if ( open ) {
            // Must be visible for CSS transition to run
            this.list.hidden = false;
            this.list.removeAttribute( 'inert' );

            this._pendingFocusOnOpen = ( source?.type === 'keydown' );

            this.root.classList.add( 'is-transitioning' );
            // Next frame so browser registers pre-open (max-height: 0) state
            requestAnimationFrame( () => this.setExpanded( true ) );
        } else {
            this.root.classList.add( 'is-transitioning' );
            this.setExpanded( false );
        }
    }

    bindKeyboard() {
        const trigger = this.root.querySelector('.trp-language-item__current[role="button"]');
        if ( !trigger ) return;

        trigger.addEventListener('keydown', (e) => {
            const inList = !!e.target.closest?.('.trp-switcher-dropdown-list');

            if ( e.key === 'Enter' || e.key === ' ' ) {
                e.preventDefault();
                this.setOpen(!this.isOpen, { source: e });
                return;
            }

            if ( e.key === 'ArrowDown' && !this.isOpen ) {
                e.preventDefault();
                this.setOpen(true, { source: e });
            }

            if ( e.key === 'Escape' ) {
                this.setOpen(false, { source: e });
                trigger.focus?.();
            }
        });
    }
}

class ShortcodeSwitcher extends BaseSwitcher {
    constructor(wrapper) {
        const overlay =
                  wrapper.querySelector('.trp-language-switcher.trp-shortcode-overlay')
                  || [...wrapper.querySelectorAll('.trp-language-switcher')]
                      .find(el => el.classList.contains('trp-shortcode-overlay'));

        // Overlay must be interactable; ensure no accidental hidden/inert from server
        overlay.hidden = false;
        overlay.removeAttribute('hidden');
        overlay.removeAttribute('inert');
        if ('inert' in overlay) overlay.inert = false;

        super(overlay);

        if (!this.root || !this.list) return;

        const control = this.root.querySelector('.trp-language-item__current[role="button"]');
        if (control && this.list && !control.hasAttribute('aria-controls')) {
            control.setAttribute('aria-controls', this.list.id);
        }

        const isClickMode =
                  this.root.classList.contains('trp-open-on-click') ||
                  wrapper.dataset.openMode === 'click' ||
                  wrapper.classList.contains('trp-open-on-click');

        if (isClickMode) {
            // Click anywhere on overlay EXCEPT inside the list
            this.root.addEventListener('click', (e) => {
                const inList = e.target.closest('.trp-switcher-dropdown-list');
                if (!inList) {
                    e.preventDefault();
                    e.stopPropagation();
                    this.setOpen(!this.isOpen, { source: e });
                }
            }, true);

            // Outside click closes
            this.onDocClick = (evt) => {
                if (!wrapper.contains(evt.target)) this.setOpen(false, { source: evt });
            };
            document.addEventListener('click', this.onDocClick, true);

            // Focus leaving wrapper closes
            wrapper.addEventListener('focusout', () => {
                setTimeout(() => {
                    if (!wrapper.contains(document.activeElement)) {
                        this.setOpen(false, { source: 'keyboard' });
                    }
                }, 0);
            });
        } else {
            // Hover mode on overlay
            this.root.addEventListener('mouseenter', (e) => this.setOpen(true,  { source: e }));
            this.root.addEventListener('mouseleave', (e) => this.setOpen(false, { source: e }));
        }
    }
}

class FloaterSwitcher extends BaseSwitcher {
    constructor(el) {
        super(el);

        el.addEventListener('mouseenter', (e) => this.setOpen(true,  { source: e }));
        el.addEventListener('mouseleave', (e) => this.setOpen(false, { source: e }));

        this.onDocClick = (evt) => { if (!el.contains(evt.target)) this.setOpen(false, { source: evt }); };
        document.addEventListener('click', this.onDocClick, true);
    }
}

document.addEventListener('DOMContentLoaded', () => {
    // Front-end or classic admin
    initLanguageSwitchers(document);

    // If no wrapper and we are in Gutenberg, watch for async SSR insert
    if (
        inGutenberg() &&
        !getEditorDoc().querySelector(WRAPPER)
    ) {
        observeWrapperUntilFound();
    }

    if ( !inGutenberg() )
        observeShortcodeSwitcher();
});

/** For shortcode switcher
 *  Mark the shortcodes that were initialized
 * */
const TRP_BOUND = new WeakSet();
const mark = (el) => TRP_BOUND.add(el);
const isMarked = (el) => TRP_BOUND.has(el);

const WRAPPER = '.trp-shortcode-switcher__wrapper';
const OVERLAY = '.trp-language-switcher:not(.trp-opposite-button)';

// Helpers
function inGutenberg() {
    return document.body?.classList?.contains('block-editor-page')
        || !!(window.wp?.data?.select?.('core/block-editor'));
}

// If editor uses an iframe canvas, work inside it
function getEditorDoc() {
    const ifr = document.querySelector('iframe[name="editor-canvas"], .editor-canvas__iframe');

    return (ifr && ifr.contentDocument) ? ifr.contentDocument : document;
}

function initLanguageSwitchers(root = document) {
    const floater = root.querySelector(
        '.trp-language-switcher.trp-ls-dropdown:not(.trp-shortcode-switcher):not(.trp-opposite-language)'
    );

    if (floater)
        new FloaterSwitcher(floater);

    root.querySelectorAll(WRAPPER)
        .forEach(wrapper => {
            const overlay = wrapper.querySelector('.trp-language-switcher:not(.trp-opposite-button)');

            if (overlay && !isMarked(overlay)) {
                mark(overlay);
                new ShortcodeSwitcher(wrapper);
            }
        });
}

/**
 * Observes the document for dynamically inserted shortcode switchers and initializes them automatically when detected.
 */
function observeShortcodeSwitcher() {
    const initWrapper = ( wrapper ) => {
        if ( !wrapper )
            return;

        const overlay = wrapper.querySelector( OVERLAY );

        if ( !overlay || isMarked( overlay ) )
            return;

        mark( overlay );

        new ShortcodeSwitcher( wrapper );
    }

    const mo = new MutationObserver( ( mutations ) => {
        for ( const m of mutations ) {
            for ( const n of m.addedNodes ) {
                if ( n.nodeType !== 1 )
                    continue;

                if ( n.matches?.( WRAPPER ) )
                    initWrapper( n );

                n.querySelectorAll?.( WRAPPER ).forEach( initWrapper );
            }
        }
    });

    mo.observe( document, { childList: true, subtree: true } );
}

/**
 * Observe Gutenberg for the shortcode wrapper being inserted asynchronously.
 *
 * Supports both Blocks API v2 (no editor iframe; wrapper appears in the outer document)
 * and Blocks API v3 (editor content rendered inside an iframe canvas).
 *
 * Strategy:
 *  1) Check the current editor document for `.trp-shortcode-switcher__wrapper` and init immediately.
 *  2) If an editor canvas iframe exists, watch its document (and reattach on iframe load) for the wrapper.
 *  3) If no iframe yet, watch the outer document for either the iframe (v3) or the wrapper itself (v2).
 *
 * Initialization is performed once per context to avoid duplicate bindings.
 */
function observeWrapperUntilFound() {
    // If wrapper already exists in current editor doc, init
    const edDoc = getEditorDoc();
    const existing = edDoc.querySelector(WRAPPER);

    if ( existing ) {
        initLanguageSwitchers( edDoc );
        return;
    }

    // Helper to locate the editor canvas iframe in the OUTER document
    const findCanvasIframe = () => document.querySelector('iframe[name="editor-canvas"], .editor-canvas__iframe');

    // If iframe is already present in the outer doc, start watching inside it
    const iframeNow = findCanvasIframe();
    if ( iframeNow ) {
        watchIframe( iframeNow );
        return;
    }

    // Otherwise, observe the OUTER document until the iframe appears
    const outerMO = new MutationObserver( ( mutations ) => {
        for ( const m of mutations ) {
            for ( const n of m.addedNodes ) {
                if ( n.nodeType !== 1 ) continue;

                const iframe =
                          n.matches?.('iframe[name="editor-canvas"], .editor-canvas__iframe')
                              ? n
                              : n.querySelector?.('iframe[name="editor-canvas"], .editor-canvas__iframe');

                if ( iframe ) {
                    outerMO.disconnect();
                    watchIframe( iframe );
                    return;
                }

                // Also catch shortcode wrapper added directly to the outer document (API v2, no iframe)
                const wrapper =
                          n.matches?.(WRAPPER)
                              ? n
                              : n.querySelector?.(WRAPPER);

                if ( wrapper ) {
                    outerMO.disconnect();
                    initLanguageSwitchers( document );
                    return;
                }

            }
        }
    } );
    outerMO.observe( document, { childList: true, subtree: true } );

    function watchIframe( iframe ) {
        // Try immediately (some builds inject srcdoc synchronously)
        tryAttachInside();

        // Also on load/navigate (Gutenberg may reload the canvas)
        iframe.addEventListener( 'load', tryAttachInside );

        function tryAttachInside() {
            let doc;
            try {
                doc = iframe.contentDocument || iframe.contentWindow?.document;
            } catch (e) {
                console.warn('Cannot access iframe content due to cross-origin restrictions', e);
                return;
            }
            if ( !doc ) return;

            // If wrapper is already there, init once and stop.
            const hit = doc.querySelector(WRAPPER);
            if ( hit ) {
                initLanguageSwitchers( doc );
                return;
            }

            // Observe INSIDE the iframe until wrapper appears
            const innerMO = new MutationObserver( ( muts ) => {
                for ( const mm of muts ) {
                    for ( const nn of mm.addedNodes ) {
                        if ( nn.nodeType !== 1 ) continue;
                        if (
                            nn.matches?.(WRAPPER) ||
                            nn.querySelector?.(WRAPPER)
                        ) {
                            innerMO.disconnect();
                            initLanguageSwitchers( doc );
                            return;
                        }
                    }
                }
                if ( doc.querySelector(WRAPPER) ) {
                    innerMO.disconnect();
                    initLanguageSwitchers( doc );
                }
            } );

            innerMO.observe( doc, { childList: true, subtree: true } );
        }
    }
};