document.addEventListener('DOMContentLoaded', function() {
    var backToTopBtn = document.querySelector('.wtols-back-to-top');
    
    if (backToTopBtn) {
        window.addEventListener('scroll', function() {
            if (window.pageYOffset > 300) {
                backToTopBtn.classList.add('wtols-btt-visible');
            } else {
                backToTopBtn.classList.remove('wtols-btt-visible');
            }
        });

        backToTopBtn.addEventListener('click', function(e) {
            e.preventDefault();
            window.scrollTo({
                top: 0,
                behavior: 'smooth'
            });
        });
    }

    // ── Instant Page Prefetching ──
    if (typeof wtolsPrefetch !== 'undefined' && wtolsPrefetch.enabled) {
        var prefetchedUrls = new Set();
        var hoverTimeout = null;

        var prefetchUrl = function(url) {
            if (prefetchedUrls.has(url)) return;
            prefetchedUrls.add(url);

            var link = document.createElement('link');
            link.rel = 'prefetch';
            link.href = url;
            document.head.appendChild(link);
        };

        var isEligible = function(anchor) {
            if (!anchor || !anchor.href) return false;
            
            // Only HTTP/HTTPS
            if (!anchor.href.startsWith('http://') && !anchor.href.startsWith('https://')) return false;
            
            // Check origin
            if (anchor.origin !== window.location.origin) return false;
            
            // Exclude admin pages, logins, and dynamic actions
            var pathname = anchor.pathname;
            if (pathname.indexOf('/wp-admin') !== -1 || pathname.indexOf('/wp-login') !== -1 || pathname.indexOf('/wp-json') !== -1 || anchor.search.indexOf('action=') !== -1 || pathname.indexOf('/cart') !== -1 || pathname.indexOf('/checkout') !== -1 || pathname.indexOf('/my-account') !== -1) return false;
            
            // Exclude file downloads
            if (/\.(zip|pdf|docx?|xlsx?|pptx?|epub|tar|gz|mp3|mp4|wav|avi|jpg|jpeg|png|gif|svg)$/i.test(pathname)) return false;
            
            return true;
        };

        var handleMouseEnter = function(e) {
            var anchor = e.target.closest('a');
            if (!isEligible(anchor)) return;

            hoverTimeout = setTimeout(function() {
                prefetchUrl(anchor.href);
            }, 65); // 65ms delay to prevent prefetching on accidental hover
        };

        var handleMouseLeave = function() {
            if (hoverTimeout) {
                clearTimeout(hoverTimeout);
                hoverTimeout = null;
            }
        };

        var handleTouchStart = function(e) {
            var anchor = e.target.closest('a');
            if (!isEligible(anchor)) return;
            prefetchUrl(anchor.href);
        };

        // Event delegation
        document.addEventListener('mouseover', handleMouseEnter, { passive: true });
        document.addEventListener('mouseout', handleMouseLeave, { passive: true });
        document.addEventListener('touchstart', handleTouchStart, { passive: true });
    }
});
