// Sistema de Menu de Ações - VERSÃO SIMPLES E FUNCIONAL
(function () {
    'use strict';

    let activeDropdown = null;
    let activeTrigger = null;

    function initActionMenus() {
        closeAllMenus();
        document.addEventListener('click', handleDocumentClick);
        window.addEventListener('scroll', handleScroll);
        window.addEventListener('resize', handleResize);
        
        const triggers = document.querySelectorAll('.action-menu-trigger');
        triggers.forEach(trigger => trigger.addEventListener('click', handleTriggerClick));
    }

    function handleScroll() {
        if (activeDropdown && activeTrigger) {
            updateDropdownPosition(activeTrigger, activeDropdown);
        }
    }

    function handleResize() {
        if (activeDropdown && activeTrigger) {
            updateDropdownPosition(activeTrigger, activeDropdown);
        }
    }

    function handleTriggerClick(event) {
        event.preventDefault();
        event.stopPropagation();

        const trigger = event.currentTarget;
        const wrapper = trigger.closest('.action-menu-wrapper');
        const dropdown = wrapper.querySelector('.action-menu-dropdown');

        closeAllMenus();

        // Abrir dropdown
        dropdown.classList.add('show');
        dropdown.style.display = 'flex';
        
        // Posicionar imediatamente
        updateDropdownPosition(trigger, dropdown);

        activeDropdown = dropdown;
        activeTrigger = trigger;
    }

    function updateDropdownPosition(trigger, dropdown) {
        const rect = trigger.getBoundingClientRect();
        
        // Posicionar à esquerda do botão (mais perto)
        let top = rect.bottom + 2; // Reduzido de 6 para 2
        let left = rect.left - 150 - 2; // Reduzido de 10 para 2
        
        // Se não couber à esquerda, colocar à direita
        if (left < 10) {
            left = rect.right + 2; // Reduzido de 10 para 2
        }
        
        // Se não couber embaixo, colocar em cima
        if (top + 50 > window.innerHeight - 10) {
            top = rect.top - 50 - 2; // Reduzido de 6 para 2
        }
        
        // Aplicar posição
        dropdown.style.position = 'fixed';
        dropdown.style.top = top + 'px';
        dropdown.style.left = left + 'px';
        dropdown.style.zIndex = '999999';
    }

    function handleDocumentClick(event) {
        if (!event.target.closest('.action-menu-wrapper') && !event.target.closest('.action-menu-dropdown')) {
            closeAllMenus();
        }
    }

    function closeAllMenus() {
        const dropdowns = document.querySelectorAll('.action-menu-dropdown');
        dropdowns.forEach(dropdown => {
            dropdown.classList.remove('show');
            dropdown.style.display = 'none';
        });
        activeDropdown = null;
        activeTrigger = null;
    }

    // API global
    window.toggleDropdown = function (element) {
        const wrapper = element.closest('.action-menu-wrapper');
        const dropdown = wrapper.querySelector('.action-menu-dropdown');
        
        if (dropdown.classList.contains('show')) {
            closeAllMenus();
        } else {
            element.click();
        }
    };

    // Inicializar
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initActionMenus);
    } else {
        initActionMenus();
    }
    window.addEventListener('load', initActionMenus);

})();