
(function() {
    'use strict';
    
    function isAppPage() {
        const path = window.location.pathname;
        return path === '/app' || path.startsWith('/app/');
    }
    
    if (!isAppPage()) {
        return;
    }
    
    function hideStandardPreloaderAggressive() {
        const indicators = document.querySelectorAll('.page-loading-indicator, .frappe-preloader, .loading-indicator');
        indicators.forEach(function(el) {
            el.style.cssText = 'display: none !important; opacity: 0 !important; visibility: hidden !important; width: 0 !important; height: 0 !important;';
        });
        
        const svgs = document.querySelectorAll('.page-loading-indicator svg, .page-loading-indicator *');
        svgs.forEach(function(el) {
            el.style.cssText = 'display: none !important; opacity: 0 !important; visibility: hidden !important;';
        });
    }
    
    hideStandardPreloaderAggressive();
    
    function createPreloader() {
        if (document.getElementById('page-preloader')) {
            return;
        }
        
        const preloader = document.createElement('div');
        preloader.id = 'page-preloader';
        preloader.setAttribute('aria-hidden', 'false');
        
        const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
        svg.id = 'preloader-svg';
        svg.setAttribute('preserveAspectRatio', 'none');
        
        const defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
        const mask = document.createElementNS('http://www.w3.org/2000/svg', 'mask');
        mask.id = 'hole-mask';
        
        const rect1 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
        rect1.setAttribute('x', '0');
        rect1.setAttribute('y', '0');
        rect1.setAttribute('width', '100%');
        rect1.setAttribute('height', '100%');
        rect1.setAttribute('fill', 'white');
        
        const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
        circle.id = 'hole';
        circle.setAttribute('cx', '50%');
        circle.setAttribute('cy', '50%');
        circle.setAttribute('r', '1');
        circle.setAttribute('fill', 'black');
        
        mask.appendChild(rect1);
        mask.appendChild(circle);
        defs.appendChild(mask);
        svg.appendChild(defs);
        
        const rect2 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
        rect2.setAttribute('width', '100%');
        rect2.setAttribute('height', '100%');
        rect2.setAttribute('fill', 'white');
        rect2.setAttribute('mask', 'url(#hole-mask)');
        
        svg.appendChild(rect2);
        preloader.appendChild(svg);
        
        if (document.body) {
            document.body.insertBefore(preloader, document.body.firstChild);
        } else {
            document.addEventListener('DOMContentLoaded', function() {
                document.body.insertBefore(preloader, document.body.firstChild);
            });
        }
    }
    
    function hidePreloader() {
        const preloader = document.getElementById('page-preloader');
        const hole = document.getElementById('hole');
        
        if (!preloader || !hole) {
            return;
        }
        
        const centerX = Math.round(window.innerWidth / 2);
        const centerY = Math.round(window.innerHeight / 2);
        
        hole.setAttribute('cx', centerX + 'px');
        hole.setAttribute('cy', centerY + 'px');
        
        function finalRadius(x, y) {
            const w = window.innerWidth;
            const h = window.innerHeight;
            const dx = Math.max(Math.abs(x - 0), Math.abs(x - w));
            const dy = Math.max(Math.abs(y - 0), Math.abs(y - h));
            return Math.ceil(Math.sqrt(dx * dx + dy * dy)) + 20;
        }
        
        function easeOutCubic(t) {
            return 1 - Math.pow(1 - t, 3);
        }
        
        const delayBeforeExpand = 150;
        const expandDuration = 900;
        
        setTimeout(function() {
            const start = performance.now();
            const startR = 1;
            const endR = finalRadius(centerX, centerY);
            
            function frame(now) {
                const elapsed = now - start;
                const t = Math.min(1, elapsed / expandDuration);
                const v = easeOutCubic(t);
                const r = startR + (endR - startR) * v;
                
                hole.setAttribute('r', r);
                
                if (t < 1) {
                    requestAnimationFrame(frame);
                } else {
                    preloader.style.transition = 'opacity 400ms ease';
                    preloader.style.opacity = '0';
                    preloader.setAttribute('aria-hidden', 'true');
                    
                    setTimeout(function() {
                        preloader.classList.add('hidden');
                        preloader.style.opacity = '';
                    }, 420);
                }
            }
            
            requestAnimationFrame(frame);
        }, delayBeforeExpand);
    }
    
    createPreloader();
    
    if (document.readyState === 'complete') {
        hidePreloader();
    } else {
        window.addEventListener('load', function() {
            hidePreloader();
        });
    }
    
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function() {
            hideStandardPreloaderAggressive();
        });
    }
    
    const hideInterval = setInterval(function() {
        hideStandardPreloaderAggressive();
    }, 50);
    
    setTimeout(function() {
        clearInterval(hideInterval);
    }, 2000);
    
    setTimeout(function() {
        const preloader = document.getElementById('page-preloader');
        if (preloader && !preloader.classList.contains('hidden')) {
            hidePreloader();
        }
    }, 10000);
    
})();

(function() {
    const path = window.location.pathname;
    if (path !== '/app' && !path.startsWith('/app/')) {
        return;
    }
    
    function hideIt() {
        const elements = document.querySelectorAll('.page-loading-indicator, .page-loading-indicator *, .frappe-preloader, .loading-indicator');
        elements.forEach(function(el) {
            el.style.cssText = 'display: none !important; opacity: 0 !important; visibility: hidden !important; width: 0 !important; height: 0 !important;';
        });
    }
    
    hideIt();
    
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', hideIt);
    }
    
    const interval = setInterval(hideIt, 50);
    setTimeout(function() {
        clearInterval(interval);
    }, 2000);
})();
