/**
 * Author Pro Header - Mobile Menu Script
 */
(function () {
    console.log('AWT Header Script Loaded'); // Debug check

    document.addEventListener('click', (e) => {
        // 1. Find the toggle button
        const toggleBtn = e.target.closest('.mobile-toggle-btn');

        // 2. Helper to close all other menus (optional but good UX)
        const closeOtherMenus = (currentBlock) => {
            document.querySelectorAll('.awt-header-block').forEach(block => {
                if (block !== currentBlock) {
                    const menu = block.querySelector('.mobile-menu');
                    const btn = block.querySelector('.mobile-toggle-btn');
                    if (menu) menu.classList.add('hidden');
                    if (btn) btn.setAttribute('aria-expanded', 'false');
                }
            });
        };

        if (toggleBtn) {
            e.preventDefault();
            e.stopPropagation();

            const headerBlock = toggleBtn.closest('.awt-header-block');
            if (!headerBlock) return;

            const mobileMenu = headerBlock.querySelector('.mobile-menu');
            if (!mobileMenu) return;

            // Toggle Logic
            const isHidden = mobileMenu.classList.contains('hidden');

            if (isHidden) {
                mobileMenu.classList.remove('hidden');
                toggleBtn.setAttribute('aria-expanded', 'true');
                console.log('Menu Opened'); // Debug
            } else {
                mobileMenu.classList.add('hidden');
                toggleBtn.setAttribute('aria-expanded', 'false');
                console.log('Menu Closed'); // Debug
            }

            closeOtherMenus(headerBlock);
            return;
        }

        // 3. Handle Click Outside
        const headerBlock = e.target.closest('.awt-header-block');
        if (!headerBlock) {
            document.querySelectorAll('.mobile-menu').forEach(menu => menu.classList.add('hidden'));
            document.querySelectorAll('.mobile-toggle-btn').forEach(btn => btn.setAttribute('aria-expanded', 'false'));
        }
    });

    /**
     * Sticky Header Height Compensation
     * Prevents content overlap when header is fixed
     */
    const handleStickyOverlap = () => {
        // Find the header specifically when it has the 'fixed' class applied by render.php
        // Note: WordPress adds the block class automatically, PHP adds 'fixed'.
        // Adjust selector if necessary based on your render.php output classes.
        const stickyHeader = document.querySelector('.wp-block-awt-author-pro-header.fixed') || document.querySelector('.awt-header-block.fixed');

        if (!stickyHeader) return;

        const adjustOffset = () => {
            const height = stickyHeader.offsetHeight;
            
            // User requested targets: .awt-canvas-wrapper or main
            const target = document.querySelector('.awt-canvas-wrapper') || document.querySelector('main');

            if (target) {
                target.style.marginTop = `${height}px`;
            } else {
                // Fallback if specific wrappers don't exist
                document.body.style.paddingTop = `${height}px`;
            }
        };

        // Run initially
        adjustOffset();

        // Run on resize
        window.addEventListener('resize', adjustOffset);
    };

    // Initialize logic
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', handleStickyOverlap);
    } else {
        handleStickyOverlap();
    }

})();
