/* =============================================
   Admin Header — Help Dropdown Button
   ============================================= */
(function () {
    'use strict';

    var iafwzdeskHelpBtn      = document.getElementById('iafwzdesk-help-btn');
    var iafwzdeskHelpDropdown = document.getElementById('iafwzdesk-help-dropdown');

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

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

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

    // Copy-to-clipboard for the email support link
    var iafwzdeskCopyBtns = document.querySelectorAll('.iafwzdesk-help-copy-btn');
    iafwzdeskCopyBtns.forEach(function (btn) {
        btn.addEventListener('click', function (e) {
            e.preventDefault();
            e.stopPropagation();
            var text = btn.getAttribute('data-copy');
            if (!text) return;

            function iafwzdeskMarkCopied() {
                btn.classList.add('iafwzdesk-copied');
                setTimeout(function () { btn.classList.remove('iafwzdesk-copied'); }, 2000);
            }

            if (navigator.clipboard && navigator.clipboard.writeText) {
                navigator.clipboard.writeText(text).then(iafwzdeskMarkCopied);
            } else {
                var ta = document.createElement('textarea');
                ta.value = text;
                ta.style.cssText = 'position:fixed;opacity:0;pointer-events:none;';
                document.body.appendChild(ta);
                ta.select();
                document.execCommand('copy');
                document.body.removeChild(ta);
                iafwzdeskMarkCopied();
            }
        });
    });
})();

/* =============================================
   Existing inline-tooltip JS (unchanged)
   ============================================= */
jQuery(document).ready(function () {
    jQuery('.iafwzdesk-help-icon-wrapper').hover(function () {
        const tooltip = jQuery(this).find('.iafwzdesk-help-tooltip');
        const wrapper = jQuery(this);

        // Get dimensions of the wrapper and tooltip
        const wrapperRect = wrapper.get(0).getBoundingClientRect();
        const tooltipWidth = tooltip.outerWidth();
        const tooltipHeight = tooltip.outerHeight();

        // Calculate default positions
        let tooltipTop = wrapperRect.top + wrapperRect.height + 10; // Below the icon with a gap of 10px
        let tooltipLeft = wrapperRect.left + (wrapperRect.width / 2) - (tooltipWidth / 2); // Center horizontally

        // Ensure tooltip does not go out of the viewport (adjust for overflow)
        if (tooltipLeft + tooltipWidth > window.innerWidth) {
            tooltipLeft = window.innerWidth - tooltipWidth - 10; // Adjust to fit on the right side
        }
        if (tooltipLeft < 0) {
            tooltipLeft = 10; // Adjust to fit on the left side
        }

        // If the tooltip is below the viewport, position it above
        if (tooltipTop + tooltipHeight > window.innerHeight) {
            tooltipTop = wrapperRect.top - tooltipHeight - 10; // Above the wrapper
        }

        // Apply calculated positions and ensure visibility
        tooltip.css({
            position: 'fixed', // Ensure it appears above overlay
            top: `${tooltipTop}px`,
            left: `${tooltipLeft}px`,
            zIndex: 10000, // Higher than overlay and popup
            visibility: 'visible',
            opacity: 1,
        });
    }, function () {
        // Hide the tooltip when hover ends
        jQuery(this).find('.iafwzdesk-help-tooltip').css({
            visibility: 'hidden',
            opacity: 0,
        });
    });
});
