// Inicializa los iconos de Lucide si la librería está cargada.
if (window.lucide && typeof lucide.createIcons === "function") {
    lucide.createIcons();
}

// Lógica para el menú lateral (sidebar) y el panel de ayuda.
(function () {
    const body = document.body;
    const sidebar = document.getElementById("sidebar");
    const navOverlay = document.getElementById("nav-overlay");
    const openBtn = document.getElementById("open-sidebar");
    const closeBtn = document.getElementById("close-sidebar");

    function openSidebar() {
        sidebar.classList.remove("-translate-x-full");
        navOverlay.classList.remove("pointer-events-none", "opacity-0");
        body.classList.add("overflow-hidden");
    }

    function closeSidebar() {
        sidebar.classList.add("-translate-x-full");
        navOverlay.classList.add("pointer-events-none", "opacity-0");
        body.classList.remove("overflow-hidden");
    }

    openBtn && openBtn.addEventListener("click", openSidebar);
    closeBtn && closeBtn.addEventListener("click", closeSidebar);
    navOverlay && navOverlay.addEventListener("click", closeSidebar);

    const helpPanel = document.getElementById("help-panel");
    const helpOverlay = document.getElementById("help-overlay");
    const openHelpTriggers = document.querySelectorAll("#open-help, #open-help-top, #open-help-mobile");
    const helpClosers = document.querySelectorAll("[data-close-help]");

    function openHelpPanel() {
        helpPanel.classList.remove("translate-x-full");
        helpOverlay.classList.remove("pointer-events-none", "opacity-0");
        body.classList.add("overflow-hidden");
    }

    function closeHelpPanel() {
        helpPanel.classList.add("translate-x-full");
        helpOverlay.classList.add("pointer-events-none", "opacity-0");
        body.classList.remove("overflow-hidden");
    }

    openHelpTriggers.forEach(btn => btn && btn.addEventListener("click", openHelpPanel));
    helpOverlay && helpOverlay.addEventListener("click", closeHelpPanel);
    helpClosers.forEach(btn => btn.addEventListener("click", closeHelpPanel));

    document.addEventListener("keydown", (e) => {
        if (e.key === "Escape") {
            closeSidebar();
            closeHelpPanel();
        }
    });
})();

// Lógica para el desplegable de cambio de plataforma (Suite FlowCRM).
(function() {
    const switcherButton = document.getElementById('platform-switcher-button');
    const switcherDropdown = document.getElementById('platform-switcher-dropdown');
    const mainNav = document.getElementById('main-nav');

    if (switcherButton && switcherDropdown && mainNav) {
        switcherButton.addEventListener('click', (event) => {
            event.stopPropagation();
            const buttonRect = switcherButton.getBoundingClientRect();
            const navRect = mainNav.getBoundingClientRect();
            const spaceBelow = navRect.bottom - buttonRect.bottom;
            const dropdownHeight = switcherDropdown.offsetHeight || 180;

            switcherDropdown.classList.remove('opens-up', 'opens-down');
            if (spaceBelow < dropdownHeight && buttonRect.top > dropdownHeight) {
                switcherDropdown.classList.add('opens-up');
            } else {
                switcherDropdown.classList.add('opens-down');
            }
            switcherDropdown.classList.toggle('hidden');
        });

        document.addEventListener('click', (e) => {
            if (!switcherButton.contains(e.target) && !switcherDropdown.contains(e.target)) {
                switcherDropdown.classList.add('hidden');
            }
        });
    }
})();

// Lógica para los menús de tipo acordeón.
document.addEventListener('DOMContentLoaded', function () {
    document.querySelectorAll('.accordion-toggle').forEach(button => {
        const content = button.nextElementSibling;
        const icon = button.querySelector('.accordion-icon');
        
        // Si un submenú tiene un elemento activo, lo expande al cargar la página.
        if (content && content.hasAttribute('data-active')) {
            content.style.maxHeight = content.scrollHeight + "px";
            if(icon) icon.style.transform = 'rotate(180deg)';
        }

        button.addEventListener('click', () => {
            if (content.style.maxHeight) {
                content.style.maxHeight = null;
                if(icon) icon.style.transform = 'rotate(0deg)';
            } else {
                content.style.maxHeight = content.scrollHeight + "px";
                if(icon) icon.style.transform = 'rotate(180deg)';
            }
        });
    });
});