// header-scroll.js – kompakter Header beim Scrollen + Suche Toggle
document.addEventListener('DOMContentLoaded', function () {

    // ── Customizer Guard ──────────────────────────────────────────────────
    if (
        window.location.href.includes('/wp-admin/customize.php') ||
        (window.parent && window.parent.wp && window.parent.wp.customize)
    ) {
        return;
    }

    // ── Scroll-Effekt + kompakter Header ─────────────────────────────────
    const header = document.querySelector('.site-header');
    const brandingRow = header ? header.querySelector('.header-row-branding') : null;

    if (header) {
        let lastScrollY = Math.max(window.scrollY, 0);
        let directionAnchorY = lastScrollY;
        let isCompact = header.classList.contains('header-compact');
        let lastStateChangeAt = 0;
        let ticking = false;

        const compactEnterThreshold = 110;
        const compactExitThreshold = 24;
        const directionThreshold = 10;
        const expandDelta = 120;
        const stateChangeCooldown = 260;

        const setCompactState = function (nextCompact, now) {
            if (isCompact === nextCompact) {
                return;
            }

            isCompact = nextCompact;
            lastStateChangeAt = now;
            directionAnchorY = Math.max(window.scrollY, 0);

            header.classList.toggle('header-compact', nextCompact);

            if (brandingRow) {
                brandingRow.classList.toggle('branding-hidden', nextCompact);
            }
        };

        const updateHeaderState = function () {
            ticking = false;

            const now = window.performance && typeof window.performance.now === 'function'
                ? window.performance.now()
                : Date.now();
            const currentScrollY = Math.max(window.scrollY, 0);
            const delta = currentScrollY - lastScrollY;
            const canChangeState = now - lastStateChangeAt >= stateChangeCooldown;

            header.classList.toggle('scrolled', currentScrollY > 50);

            if (currentScrollY <= compactExitThreshold) {
                setCompactState(false, now);
            } else if (canChangeState && delta > directionThreshold) {
                directionAnchorY = currentScrollY;

                if (currentScrollY > compactEnterThreshold) {
                    setCompactState(true, now);
                }
            } else if (canChangeState && delta < -directionThreshold) {
                if (directionAnchorY - currentScrollY >= expandDelta) {
                    setCompactState(false, now);
                }
            }

            lastScrollY = currentScrollY;
        };

        const onScroll = function () {
            if (ticking) {
                return;
            }

            ticking = true;
            window.requestAnimationFrame(updateHeaderState);
        };

        updateHeaderState();
        window.addEventListener('scroll', onScroll, { passive: true });
    }

    // ── Suche Toggle ──────────────────────────────────────────────────────
    const searchToggle  = document.querySelector('.header-search-toggle');
    const searchDropdown = document.querySelector('.header-search-dropdown');

    if (searchToggle && searchDropdown) {
        searchToggle.addEventListener('click', function (e) {
            e.stopPropagation();
            const isOpen = searchDropdown.classList.toggle('open');
            searchToggle.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
            searchDropdown.setAttribute('aria-hidden', isOpen ? 'false' : 'true');

            if (isOpen) {
                // Fokus ins Suchfeld setzen
                const field = searchDropdown.querySelector('.search-field');
                if (field) setTimeout(function () { field.focus(); }, 50);
            }
        });

        // Schließen bei Außenklick
        document.addEventListener('click', function (e) {
            if (!searchDropdown.contains(e.target) && !searchToggle.contains(e.target)) {
                searchDropdown.classList.remove('open');
                searchToggle.setAttribute('aria-expanded', 'false');
                searchDropdown.setAttribute('aria-hidden', 'true');
            }
        });

        // Schließen mit Escape
        document.addEventListener('keydown', function (e) {
            if (e.key === 'Escape' && searchDropdown.classList.contains('open')) {
                searchDropdown.classList.remove('open');
                searchToggle.setAttribute('aria-expanded', 'false');
                searchDropdown.setAttribute('aria-hidden', 'true');
                searchToggle.focus();
            }
        });
    }
});