(function () {
    'use strict';

    function cleanText(text) {
        return (text || '')
            .replace(/\s+/g, ' ')
            .trim()
            .toLowerCase();
    }


    function setupPaymentMethod() {

        const elements =
            Array.from(
                document.querySelectorAll(
                    'button, a, label, div'
                )
            );


        const mercadopago =
            elements.find(function (el) {

                const text =
                    cleanText(el.textContent);

                return (
                    text === 'mercadopago' ||
                    text === 'mercado pago'
                );
            });


        if (!mercadopago) return;


        let current =
            mercadopago;


        for (
            let i = 0;
            i < 5 && current;
            i++
        ) {

            const rect =
                current.getBoundingClientRect();


            if (
                rect.width >= 150 &&
                rect.width <= 450 &&
                rect.height >= 120 &&
                rect.height <= 350
            ) {

                current.classList.add(
                    'pk-payment-method'
                );

                break;
            }


            current =
                current.parentElement;
        }
    }


    if (
        document.readyState ===
        'loading'
    ) {
        document.addEventListener(
            'DOMContentLoaded',
            setupPaymentMethod
        );
    } else {
        setupPaymentMethod();
    }


    new MutationObserver(
        setupPaymentMethod
    ).observe(
        document.body,
        {
            childList: true,
            subtree: true
        }
    );

})();