jQuery(document).ready(function($) {
    // Help dropdown toggle
    var iafwzcHelpBtn      = document.getElementById('iafwzc-help-btn');
    var iafwzcHelpDropdown = document.getElementById('iafwzc-help-dropdown');

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

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

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

    // Help email copy button
    function iafwzcShowCopied(copyBtn) {
        copyBtn.classList.add('iafwzc-copied');
        setTimeout(function () {
            copyBtn.classList.remove('iafwzc-copied');
        }, 2000);
    }

    function iafwzcFallbackCopy(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('.iafwzc-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 () {
                iafwzcShowCopied(copyBtn);
            }).catch(function () {
                iafwzcFallbackCopy(text);
                iafwzcShowCopied(copyBtn);
            });
        } else {
            iafwzcFallbackCopy(text);
            iafwzcShowCopied(copyBtn);
        }
    });

    // Get the tab parameter from the URL
    var urlParams = new URLSearchParams(window.location.search);
    var currentPage = urlParams.get('page');
    if (currentPage === 'integrate-any-form-with-zoho-crm') {
        var activeTab = urlParams.get('tab') || 'accounts'; // 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', 'accounts');
            window.history.replaceState(null, null, '?' + urlParams.toString());
        }

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

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