jQuery(document).ready(function($) {
    // ── Help dropdown toggle ──
    var integrazoWzHelpBtn      = document.getElementById('integrazo-wz-help-btn');
    var integrazoWzHelpDropdown = document.getElementById('integrazo-wz-help-dropdown');

    if (integrazoWzHelpBtn && integrazoWzHelpDropdown) {
        integrazoWzHelpBtn.addEventListener('click', function (e) {
            e.stopPropagation();
            var isOpen = !integrazoWzHelpDropdown.hidden;
            integrazoWzHelpDropdown.hidden = isOpen;
            integrazoWzHelpBtn.setAttribute('aria-expanded', String(!isOpen));
        });

        document.addEventListener('click', function (e) {
            if (e.target.closest('.integrazo-wz-help-copy-btn')) return;
            integrazoWzHelpDropdown.hidden = true;
            integrazoWzHelpBtn.setAttribute('aria-expanded', 'false');
        });

        document.addEventListener('keydown', function (e) {
            if (e.key === 'Escape') {
                integrazoWzHelpDropdown.hidden = true;
                integrazoWzHelpBtn.setAttribute('aria-expanded', 'false');
            }
        });
    }

    // ── Help email copy button ──
    function integrazoWzShowCopied(copyBtn) {
        copyBtn.classList.add('integrazo-wz-copied');
        setTimeout(function () {
            copyBtn.classList.remove('integrazo-wz-copied');
        }, 2000);
    }

    function integrazoWzFallbackCopy(text) {
        var ta = document.createElement('textarea');
        ta.value = text;
        ta.style.cssText = 'position:fixed;opacity:0;top:0;left:0;';
        document.body.appendChild(ta);
        ta.focus();
        ta.select();
        try { document.execCommand('copy'); } catch (err) {}
        document.body.removeChild(ta);
    }

    document.addEventListener('click', function (e) {
        var copyBtn = e.target.closest('.integrazo-wz-help-copy-btn');
        if (!copyBtn) return;
        e.preventDefault();
        e.stopPropagation();
        var text = copyBtn.getAttribute('data-copy');
        if (!text) return;
        if (navigator.clipboard && window.isSecureContext) {
            navigator.clipboard.writeText(text).then(function () {
                integrazoWzShowCopied(copyBtn);
            }).catch(function () {
                integrazoWzFallbackCopy(text);
                integrazoWzShowCopied(copyBtn);
            });
        } else {
            integrazoWzFallbackCopy(text);
            integrazoWzShowCopied(copyBtn);
        }
    });

    // Get the tab parameter from the URL
    var urlParams = new URLSearchParams(window.location.search);
    var currentPage = urlParams.get('page');
    if (currentPage === 'integrazo-integration-for-zoho-apps') {
        var activeTab = urlParams.get('tab') || 'setup'; // Default to 'auth' tab if no tab parameter is set

        // If no 'tab' parameter exists in the URL, update the URL to include 'auth' as the default
        if (!urlParams.has('tab')) {
            // Append the 'auth' tab to the URL without reloading the page
            urlParams.set('tab', 'setup');
            window.history.replaceState(null, null, '?' + urlParams.toString());
        }

        // Hide all tab content and remove active class from all tabs
        $('.integrazo-wz-tab').removeClass('integrazo-wz-tab-active');
        $('.integrazo-wz-tab-content').hide();

        // Show the active tab content and add active class to the active tab
        $('#integrazo-wz-tab-' + activeTab).addClass('integrazo-wz-tab-active');
        $('#integrazo-wz-content-' + activeTab).show();
    }
});
