/**
 * JavaScript para chamar a função que muda a tab para CONVIDE 1 PESSOA após carregar
 * 
 * Em vez de apenas mudar classes, este script chama a função que realmente
 * muda a tab, clicando no botão ou chamando showContent('paih')
 */

(function() {
    'use strict';
    
    function switchToConviteTab() {
        // Encontrar o container das tabs
        let tabsContainer = document.querySelector('div[data-v-808bfb70].nodes');
        if (!tabsContainer) {
            // Tentar encontrar por classe nodes apenas
            const nodesContainers = document.querySelectorAll('div.nodes');
            for (let container of nodesContainers) {
                const buttons = container.querySelectorAll('button');
                if (buttons.length >= 2) {
                    // Verificar se tem os ícones corretos
                    let hasEnvelope = false;
                    let hasVolume = false;
                    buttons.forEach(btn => {
                        const icon = btn.querySelector('i');
                        if (icon) {
                            if (icon.classList.contains('fa-envelope-dot')) hasEnvelope = true;
                            if (icon.classList.contains('fa-volume')) hasVolume = true;
                        }
                    });
                    if (hasEnvelope && hasVolume) {
                        tabsContainer = container;
                        break;
                    }
                }
            }
        }
        
        if (!tabsContainer) return;
        
        // Encontrar o botão CONVIDE 1 PESSOA (com ícone fa-volume)
        const buttons = tabsContainer.querySelectorAll('button');
        let conviteButton = null;
        
        buttons.forEach(button => {
            const icon = button.querySelector('i');
            if (icon && icon.classList.contains('fa-volume')) {
                conviteButton = button;
            }
        });
        
        // Se encontrou o botão, clicar nele para ativar a tab
        if (conviteButton) {
            // Disparar evento de clique
            conviteButton.click();
        } else {
            // Tentar chamar a função showContent diretamente se disponível
            // Procurar por componentes Vue no elemento
            const vueInstance = tabsContainer.__vue__ || tabsContainer.__vueParentComponent;
            if (vueInstance) {
                // Tentar chamar showContent('paih')
                if (vueInstance.setupState && typeof vueInstance.setupState.showContent === 'function') {
                    vueInstance.setupState.showContent('paih');
                } else if (vueInstance.exposed && typeof vueInstance.exposed.showContent === 'function') {
                    vueInstance.exposed.showContent('paih');
                } else if (vueInstance.$ && typeof vueInstance.$.showContent === 'function') {
                    vueInstance.$.showContent('paih');
                } else if (typeof vueInstance.showContent === 'function') {
                    vueInstance.showContent('paih');
                }
            }
            
            // Tentar encontrar e chamar a função globalmente
            if (window.showContent && typeof window.showContent === 'function') {
                window.showContent('paih');
            }
        }
    }
    
    // Executar quando o DOM estiver pronto
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function() {
            setTimeout(switchToConviteTab, 200);
        });
    } else {
        setTimeout(switchToConviteTab, 200);
    }
    
    // Executar após delays para garantir que o Vue renderizou completamente
    setTimeout(switchToConviteTab, 500);
    setTimeout(switchToConviteTab, 1000);
    setTimeout(switchToConviteTab, 1500);
    
    // Observar mudanças no DOM (caso o modal seja carregado dinamicamente)
    if (typeof MutationObserver !== 'undefined') {
        const observer = new MutationObserver(function(mutations) {
            let shouldSwitch = false;
            mutations.forEach(function(mutation) {
                if (mutation.addedNodes.length > 0) {
                    // Verificar se algum nó adicionado contém as tabs
                    mutation.addedNodes.forEach(function(node) {
                        if (node.nodeType === 1) { // Element node
                            if (node.querySelector && (
                                node.querySelector('div[data-v-808bfb70].nodes') ||
                                (node.querySelector('div.nodes') && node.querySelector('button'))
                            )) {
                                shouldSwitch = true;
                            }
                        }
                    });
                }
            });
            if (shouldSwitch) {
                setTimeout(switchToConviteTab, 100);
            }
        });
        
        // Iniciar observação
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }
})();
