/** * SUREXS - English Version Script
 * Evita errores de "null" si faltan elementos y corrige rutas desde /en/
 */

document.addEventListener("DOMContentLoaded", function () {

    // --- 1. CONFIGURACIÓN DE RUTAS ---
    // Aseguramos que los links vayan a la raíz (/) y no se queden en /en/
    const urls = {
        "1": "/cotizacion-beneficios",
        "2": "/cotizacion-flotilla",
        "3": "/cotizacion-danos",
        "4": "/cotizacion-flexibles",
        "5": "/cotizacion-nomina"
    };

    const selectElement = document.getElementById('selectOptions');
    const actionButton = document.getElementById('actionButton');

    // Solo se ejecuta si el selector existe en el HTML
    if (selectElement && actionButton) {
        actionButton.classList.add('disabled');
        selectElement.addEventListener('change', () => {
            const selectedValue = selectElement.value;
            if (urls[selectedValue]) {
                actionButton.href = urls[selectedValue];
                actionButton.classList.remove('disabled');
            } else {
                actionButton.href = "#contactus"; // Fallback al ancla de contacto
                actionButton.classList.add('disabled');
            }
        });
    }

    // --- 2. SWIPERS (Carga segura) ---
    const initSwiper = (selector, config) => {
        if (document.querySelector(selector)) {
            new Swiper(selector, config);
        }
    };

    // Swiper Productos
    initSwiper('.swiper-productos', {
        direction: 'horizontal',
        autoplay: { delay: 5000 },
        loop: true,
        slidesPerView: 1,
        spaceBetween: 10,
        pagination: { el: '.swiper-pagination', clickable: true },
        navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev' },
        breakpoints: {
            768: { slidesPerView: 2, spaceBetween: 20 },
            992: { slidesPerView: 3, spaceBetween: 25 },
            1200: { slidesPerView: 3.5, spaceBetween: 25 }
        }
    });

    // Swiper Medios
    initSwiper('.medios__swiper', {
        direction: 'horizontal',
        slidesPerView: 1.5,
        spaceBetween: 16,
        pagination: { el: '.swiper-pagination' },
        breakpoints: {
            768: { slidesPerView: 2.5 },
            992: { slidesPerView: 3 },
            1200: { slidesPerView: 4 }
        }
    });

    // --- 3. VIDEOS & MODALS ---
    const youtubeModal = document.getElementById('youtubeModal');
    const videoFrame = document.getElementById('videoFrame');

    if (youtubeModal && videoFrame) {
        youtubeModal.addEventListener('show.bs.modal', () => {
            videoFrame.src = "https://www.youtube.com/embed/3l48ZqxESX8?autoplay=1";
        });
        youtubeModal.addEventListener('hidden.bs.modal', () => {
            videoFrame.src = "";
        });
    }

    // --- 4. FORMULARIO HUBSPOT (Lazy Load) ---
    // Usamos el ID 'contactus' que definimos para la versión en inglés
    const formSection = document.querySelector("#contactus");
    const skeleton = document.getElementById("hubspot-skeleton");
    const formContainer = document.getElementById("hubspot-form");

    if (formSection) {
        let loaded = false;
        const observer = new IntersectionObserver((entries) => {
            if (entries[0].isIntersecting && !loaded) {
                loaded = true;
                const s = document.createElement("script");
                s.src = "https://js.hsforms.net/forms/shell.js";
                s.async = true;
                document.body.appendChild(s);
                s.onload = () => {
                    hbspt.forms.create({
                        region: "na1",
                        portalId: "21007866",
                        formId: "61fddcca-c4d9-42db-82ba-59d379aee912",
                        target: "#hubspot-form"
                    });
                    if(skeleton) skeleton.style.display = "none";
                    if(formContainer) formContainer.style.display = "block";
                };
            }
        }, { rootMargin: "0px 0px 200px 0px" });
        observer.observe(formSection);
    }
});

// --- 5. FUNCIONES GLOBALES (Videos Facade) ---
window.youtubeFacadeInit = function(idFacade, idThumnail) {
    const frame = document.getElementById(idFacade);
    const thumb = document.getElementById(idThumnail);
    if (!frame || !thumb) return;

    const ids = {
        'videoFrameInsurtech': '5CD1Urippdw',
        'videoFrameTestimonial': 'rOO3j5D20-U',
        'videoFrameImagen': 'EJ-th9IBGuc',
        'videoFrameContratarSeguro': 'oIeqSz06sNA'
    };

    if (ids[frame.id]) {
        frame.src = `https://www.youtube.com/embed/${ids[frame.id]}?autoplay=1`;
        frame.classList.add("visible");
        thumb.classList.remove("visible");
    }
};

window.youtubeFacadeOff = function() {
    document.querySelectorAll('.index-videos__iframe').forEach(f => {
        f.src = "";
        f.classList.remove("visible");
    });
    document.querySelectorAll('.index-videos__thumbnail').forEach(t => {
        t.classList.add("visible");
    });
};