(function () {
    'use strict';

    const peaklineLabels = new Set([
        'DESCUBRE PEAKLINE',
        'PRÓXIMAS SALIDAS',
        'QUÉ ES PEAKLINE',
        'POR QUÉ ELEGIR PEAKLINE',

        /* Versiones por si se cambia el idioma */
        'DISCOVER PEAKLINE',
        'UPCOMING TRIPS',
        'WHAT IS PEAKLINE',
        'WHY CHOOSE PEAKLINE'
    ]);

    function normalizeText(text) {
        return (text || '')
            .replace(/\s+/g, ' ')
            .trim()
            .toUpperCase();
    }

    function applyPeaklineEyebrows() {
        const elements = document.querySelectorAll('main *');

        elements.forEach(function (element) {
            const text = normalizeText(element.textContent);

            if (!peaklineLabels.has(text)) {
                return;
            }

            /* Evita asignar la clase a un contenedor padre
               cuando existe un hijo con exactamente el mismo texto */
            const childHasSameText = Array.from(element.children).some(
                function (child) {
                    return peaklineLabels.has(
                        normalizeText(child.textContent)
                    );
                }
            );

            if (!childHasSameText) {
                element.classList.add('pk-eyebrow');
            }
        });
    }

    let scheduled = false;

    function scheduleApply() {
        if (scheduled) return;

        scheduled = true;

        requestAnimationFrame(function () {
            scheduled = false;
            applyPeaklineEyebrows();
        });
    }

    if (document.readyState === 'loading') {
        document.addEventListener(
            'DOMContentLoaded',
            applyPeaklineEyebrows
        );
    } else {
        applyPeaklineEyebrows();
    }

    const observer = new MutationObserver(scheduleApply);

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();