(function () {
    'use strict';

    function cleanText(text) {
        return (text || '')
            .replace(/\s+/g, ' ')
            .trim();
    }


    /* ======================================================
       1. TABS DESCRIPCIÓN / DETALLES
       ====================================================== */

    function setupTabs() {

        const candidates = Array.from(
            document.querySelectorAll('a, button, div, span')
        );

        const tabs = candidates.filter(function (el) {
            const text = cleanText(el.textContent);

            return (
                text === 'Descripción' ||
                text === 'Detalles'
            ) && el.children.length === 0;
        });

        if (!tabs.length) return;


        tabs.forEach(function (tab) {

            tab.classList.add('pk-service-tab');

            if (!tab.dataset.pkTabReady) {

                tab.dataset.pkTabReady = 'true';

                tab.addEventListener('click', function () {

                    tabs.forEach(function (other) {
                        other.classList.remove(
                            'pk-service-tab-active'
                        );
                    });

                    tab.classList.add(
                        'pk-service-tab-active'
                    );

                });
            }
        });


        /* El primer estado de Vivirly suele ser Descripción */

        const alreadyActive = tabs.some(function (tab) {
            return tab.classList.contains(
                'pk-service-tab-active'
            );
        });

        if (!alreadyActive) {

            const description = tabs.find(function (tab) {
                return cleanText(tab.textContent) ===
                    'Descripción';
            });

            if (description) {
                description.classList.add(
                    'pk-service-tab-active'
                );
            }
        }
    }



    /* ======================================================
       2. FINALIZA TU COMPRA
       ====================================================== */

    function setupCheckoutTitle() {

        const elements = Array.from(
            document.querySelectorAll(
                'h1, h2, h3, div, p'
            )
        );

        const title = elements.find(function (el) {
            return cleanText(el.textContent) ===
                'Finaliza tu compra' &&
                el.children.length === 0;
        });

        if (title) {
            title.classList.add(
                'pk-checkout-title'
            );
        }


        const subtitle = elements.find(function (el) {
            return cleanText(el.textContent) ===
                'Ingresa tu información personal' &&
                el.children.length === 0;
        });

        if (subtitle) {
            subtitle.classList.add(
                'pk-checkout-subtitle'
            );
        }
    }



    /* ======================================================
       3. MENSAJE DE CONFIANZA EN PAGO
       ====================================================== */

    function setupPaymentTrust() {

        if (
            document.querySelector(
                '.pk-payment-trust'
            )
        ) {
            return;
        }


        const elements = Array.from(
            document.querySelectorAll(
                'h1, h2, h3, h4, p, div'
            )
        );

        const paymentTitle = elements.find(
            function (el) {

                return cleanText(el.textContent) ===
                    '¿Cómo vas a pagar?' &&
                    el.children.length === 0;

            }
        );


        if (!paymentTitle) return;


        const trust = document.createElement('div');

        trust.className =
            'pk-payment-trust';

        trust.innerHTML = `
            <div class="pk-payment-trust-icon">
                <svg
                    viewBox="0 0 24 24"
                    aria-hidden="true"
                >
                    <path d="
                        M12 2
                        4.5 5.2
                        v5.4
                        c0 5
                        3.2 9.4
                        7.5 11.4
                        4.3-2
                        7.5-6.4
                        7.5-11.4
                        V5.2
                        L12 2z

                        M10.8 15.8
                        7.9 13
                        9.3 11.6
                        10.8 13.1
                        14.9 9
                        16.3 10.4
                        10.8 15.8z
                    "></path>
                </svg>
            </div>

            <div>
                <strong>
                    Pago protegido con Mercado Pago
                </strong>

                <span>
                    Tus datos de pago son procesados
                    mediante Mercado Pago bajo estándares
                    de seguridad PCI DSS.
                </span>
            </div>
        `;


        paymentTitle.insertAdjacentElement(
            'afterend',
            trust
        );
    }



    /* ======================================================
       4. RESUMEN AZUL DE LA RESERVA
       Detectamos el panel que contiene "Cantidad:"
       ====================================================== */

    function setupReservationSummary() {

        const elements = Array.from(
            document.querySelectorAll('div, p, span')
        );


        const quantityElement = elements.find(
            function (el) {

                const text =
                    cleanText(el.textContent);

                return (
                    /^Cantidad:\s*\d+$/i.test(text) &&
                    el.children.length === 0
                );

            }
        );


        if (!quantityElement) return;


        let current =
            quantityElement.parentElement;


        for (
            let i = 0;
            i < 6 && current;
            i++
        ) {

            const bg =
                getComputedStyle(current)
                    .backgroundColor;


            const match =
                bg.match(
                    /rgba?\((\d+),\s*(\d+),\s*(\d+)/
                );


            if (match) {

                const r = Number(match[1]);
                const g = Number(match[2]);
                const b = Number(match[3]);


                /* Detectamos un fondo claramente azul */

                if (
                    b > r + 25 &&
                    b > g + 10
                ) {

                    current.classList.add(
                        'pk-reservation-summary'
                    );

                    break;
                }
            }


            current =
                current.parentElement;
        }
    }



    /* ======================================================
       5. CALENDARIO
       ====================================================== */

    const monthRegex =
        /^(enero|febrero|marzo|abril|mayo|junio|julio|agosto|septiembre|octubre|noviembre|diciembre)\s+\d{4}$/i;


    function findCalendar() {

        const elements = Array.from(
            document.querySelectorAll(
                'h1,h2,h3,h4,div,span'
            )
        );


        const monthTitle =
            elements.find(function (el) {

                return (
                    monthRegex.test(
                        cleanText(el.textContent)
                    ) &&
                    el.children.length === 0
                );

            });


        if (!monthTitle) return null;


        let current =
            monthTitle.parentElement;


        for (
            let i = 0;
            i < 6 && current;
            i++
        ) {

            const text =
                cleanText(current.textContent)
                    .toLowerCase();


            const weekdayCount = [
                'domingo',
                'lunes',
                'martes',
                'miércoles',
                'jueves',
                'viernes',
                'sábado'
            ].filter(function (day) {
                return text.includes(day);
            }).length;


            if (weekdayCount >= 5) {

                current.classList.add(
                    'pk-calendar'
                );

                return current;
            }


            current =
                current.parentElement;
        }


        return null;
    }


    function styleCalendar() {

        const calendar =
            findCalendar();


        if (!calendar) return;


        const elements =
            calendar.querySelectorAll(
                'button, a, div, span'
            );


        elements.forEach(function (el) {

            const text =
                cleanText(el.textContent);


            if (/^\d{1,2}$/.test(text)) {

                const rect =
                    el.getBoundingClientRect();


                if (
                    rect.width <= 70 &&
                    rect.height <= 70
                ) {

                    el.classList.add(
                        'pk-calendar-date'
                    );


                    const bg =
                        getComputedStyle(el)
                            .backgroundColor;


                    const match =
                        bg.match(
                            /rgba?\((\d+),\s*(\d+),\s*(\d+)/
                        );


                    if (match) {

                        const r =
                            Number(match[1]);

                        const g =
                            Number(match[2]);

                        const b =
                            Number(match[3]);


                        if (
                            b > r + 25 &&
                            b > g + 10
                        ) {

                            el.classList.add(
                                'pk-calendar-selected'
                            );
                        }
                    }
                }
            }
        });


        if (!calendar.dataset.pkCalendarReady) {

            calendar.dataset.pkCalendarReady =
                'true';


            calendar.addEventListener(
                'click',
                function (event) {

                    setTimeout(function () {

                        calendar
                            .querySelectorAll(
                                '.pk-calendar-selected'
                            )
                            .forEach(function (el) {

                                el.classList.remove(
                                    'pk-calendar-selected'
                                );

                            });


                        styleCalendar();

                    }, 80);

                }
            );
        }
    }



    /* ======================================================
       EJECUCIÓN
       ====================================================== */

    function applyPeaklinePurchaseUI() {

        setupTabs();

        setupCheckoutTitle();

        setupPaymentTrust();

        setupReservationSummary();

        styleCalendar();
    }


    let scheduled = false;


    function scheduleUpdate() {

        if (scheduled) return;


        scheduled = true;


        requestAnimationFrame(function () {

            scheduled = false;

            applyPeaklinePurchaseUI();

        });
    }


    if (
        document.readyState ===
        'loading'
    ) {

        document.addEventListener(
            'DOMContentLoaded',
            applyPeaklinePurchaseUI
        );

    } else {

        applyPeaklinePurchaseUI();

    }


    const observer =
        new MutationObserver(
            scheduleUpdate
        );


    observer.observe(
        document.body,
        {
            childList: true,
            subtree: true
        }
    );

})();(function () {
    'use strict';

    function cleanText(text) {
        return (text || '')
            .replace(/\s+/g, ' ')
            .trim();
    }

    function setupPeaklineTabsV2() {

        const elements = Array.from(
            document.querySelectorAll(
                'a, button, div, span'
            )
        );

        const tabs = elements.filter(function (el) {

            const text =
                cleanText(el.textContent);

            return (
                (
                    text === 'Descripción' ||
                    text === 'Detalles'
                ) &&
                el.children.length === 0
            );
        });


        if (tabs.length < 2) return;


        tabs.forEach(function (tab) {
            tab.classList.add(
                'pk-service-tab'
            );
        });


        /* Buscamos el primer ancestro común */

        let parent =
            tabs[0].parentElement;


        while (
            parent &&
            !parent.contains(tabs[1])
        ) {
            parent =
                parent.parentElement;
        }


        if (parent) {
            parent.classList.add(
                'pk-tabs-container'
            );
        }


        /* Estado inicial */

        const description =
            tabs.find(function (tab) {
                return cleanText(
                    tab.textContent
                ) === 'Descripción';
            });


        if (
            description &&
            !tabs.some(function (tab) {
                return tab.classList.contains(
                    'pk-service-tab-active'
                );
            })
        ) {
            description.classList.add(
                'pk-service-tab-active'
            );
        }


        /* Cambio al hacer clic */

        tabs.forEach(function (tab) {

            if (tab.dataset.pkTabsV2) return;

            tab.dataset.pkTabsV2 =
                'true';


            tab.addEventListener(
                'click',
                function () {

                    tabs.forEach(
                        function (item) {
                            item.classList.remove(
                                'pk-service-tab-active'
                            );
                        }
                    );


                    tab.classList.add(
                        'pk-service-tab-active'
                    );
                }
            );
        });
    }


    function run() {
        setupPeaklineTabsV2();
    }


    if (
        document.readyState ===
        'loading'
    ) {
        document.addEventListener(
            'DOMContentLoaded',
            run
        );
    } else {
        run();
    }


    new MutationObserver(run)
        .observe(
            document.body,
            {
                childList: true,
                subtree: true
            }
        );

})();