// Preloader Removal
window.addEventListener('load', function() {
    setTimeout(function() {
        const preloader = document.getElementById('app-preloader');

        if (!preloader) {
            return;
        }

        preloader.classList.add('fade-out');
        setTimeout(() => {
            preloader.style.display = 'none';
            document.body.classList.remove('overflow-hidden');
        }, 400);
    }, 800);
});

// Mobile Menu Toggle logic
const mobileMenuBtn = document.getElementById('mobile-menu-btn');
const mobileMenu = document.getElementById('mobile-menu');

if (mobileMenuBtn && mobileMenu) {
    mobileMenuBtn.addEventListener('click', () => {
        mobileMenu.classList.toggle('hidden');
    });
}

document.querySelectorAll('.mobile-submenu-trigger').forEach(button => {
    button.addEventListener('click', () => {
        toggleMobileSub(button.dataset.mobileSub, button);
    });
});

// Mobile Submenu Accordion Toggle
function toggleMobileSub(menuId, btnElement) {
    const submenu = document.getElementById(menuId);
    const icon = btnElement.querySelector('i');

    if (!submenu || !icon) {
        return;
    }

    if (submenu.classList.contains('active')) {
        submenu.classList.remove('active');
        icon.style.transform = 'rotate(0deg)';
    } else {
        document.querySelectorAll('.mobile-submenu').forEach(sub => sub.classList.remove('active'));
        document.querySelectorAll('#mobile-menu .fa-chevron-down').forEach(ico => {
            ico.style.transform = 'rotate(0deg)';
        });

        submenu.classList.add('active');
        icon.style.transform = 'rotate(180deg)';
    }
}

// invdevteam1-shared-desktop-menu-toggle
document.addEventListener('DOMContentLoaded', function () {
    var groups = Array.prototype.slice.call(document.querySelectorAll('nav .group'));
    function dropdownFor(group) {
        return group ? group.querySelector(':scope > .dropdown-menu') : null;
    }
    function hide(group) {
        var menu = dropdownFor(group);
        if (!menu) return;
        menu.style.opacity = '';
        menu.style.visibility = '';
        menu.style.pointerEvents = '';
        menu.style.transform = '';
        group.classList.remove('menu-click-open');
        var trigger = group.querySelector(':scope > button');
        if (trigger) trigger.setAttribute('aria-expanded', 'false');
    }
    function hideAll(except) {
        groups.forEach(function (group) {
            if (group !== except) hide(group);
        });
    }
    function show(group) {
        var menu = dropdownFor(group);
        if (!menu) return;
        hideAll(group);
        menu.style.opacity = '1';
        menu.style.visibility = 'visible';
        menu.style.pointerEvents = 'auto';
        menu.style.transform = 'translateY(0)';
        group.classList.add('menu-click-open');
        var trigger = group.querySelector(':scope > button');
        if (trigger) trigger.setAttribute('aria-expanded', 'true');
    }
    groups.forEach(function (group) {
        var trigger = group.querySelector(':scope > button');
        var menu = dropdownFor(group);
        if (!trigger || !menu) return;
        trigger.setAttribute('aria-haspopup', 'true');
        trigger.setAttribute('aria-expanded', 'false');
        trigger.addEventListener('click', function (event) {
            event.preventDefault();
            event.stopPropagation();
            var isOpen = group.classList.contains('menu-click-open');
            hideAll();
            if (!isOpen) show(group);
        });
        menu.addEventListener('click', function (event) {
            event.stopPropagation();
        });
    });
    document.addEventListener('click', function () { hideAll(); });
    document.addEventListener('keydown', function (event) {
        if (event.key === 'Escape') hideAll();
    });
});
