// Iframe navigation interception script
(function() {
    // Always set up navigation interception
    const originalAssign = window.location.assign;
    const originalReplace = window.location.replace;
    
    // Check if we're in an iframe OR if we have iframe parameter (meaning we should stay in iframe mode)
    function isInIframeMode() {
        return (window.self !== window.top) || window.location.search.includes('iframe=1');
    }
    
    function shouldOpenInTab(url) {
        if (!url || url.startsWith('#') || url.startsWith('javascript:')) return false;
        if (url.includes('print_') || url.includes('vouchers/')) return false;
        if (url.includes('pos.php') || url.includes('login.php') || url.includes('logout.php')) return false;
        return true;
    }
    
    function addIframeParam(url) {
        // Add iframe=1 parameter to preserve iframe mode
        // Check if already has iframe parameter
        if (url.includes('iframe=1')) {
            return url;
        }
        
        // Parse URL to handle query parameters properly
        try {
            const urlObj = new URL(url, window.location.href);
            urlObj.searchParams.set('iframe', '1');
            return urlObj.href.replace(window.location.origin, '');
        } catch(e) {
            // Fallback for relative URLs or invalid URLs
            if (url.includes('?')) {
                // URL already has parameters
                const separator = url.endsWith('?') || url.endsWith('&') ? '' : '&';
                return url + separator + 'iframe=1';
            } else {
                // URL has no parameters
                return url + '?iframe=1';
            }
        }
    }
    
    // Only intercept if we're in iframe mode
    if (isInIframeMode()) {
        // Intercept window.location.href assignments
        try {
            const locationHrefDescriptor = Object.getOwnPropertyDescriptor(Location.prototype, 'href') ||
                                           Object.getOwnPropertyDescriptor(window.location, 'href');
            
            if (locationHrefDescriptor && locationHrefDescriptor.set) {
                const originalSetHref = locationHrefDescriptor.set;
                const originalGetHref = locationHrefDescriptor.get;
                
                try {
                    Object.defineProperty(Location.prototype, 'href', {
                        set: function(url) {
                            const urlString = String(url);
                            
                            // For any navigation in iframe mode, add iframe parameter
                            if (shouldOpenInTab(urlString)) {
                                const urlWithIframe = addIframeParam(urlString);
                                return originalSetHref.call(this, urlWithIframe);
                            }
                            
                            // Default behavior
                            return originalSetHref.call(this, urlString);
                        },
                        get: originalGetHref,
                        configurable: true,
                        enumerable: true
                    });
                } catch(e1) {
                    // Fallback if Location.prototype fails
                }
            }
        } catch(e) {
            // Location interception failed
        }
        
        // Intercept window.location.assign
        window.location.assign = function(url) {
            const urlString = String(url);
            if (shouldOpenInTab(urlString)) {
                const urlWithIframe = addIframeParam(urlString);
                return originalAssign.call(this, urlWithIframe);
            }
            return originalAssign.call(this, urlString);
        };
        
        // Intercept window.location.replace
        window.location.replace = function(url) {
            const urlString = String(url);
            if (shouldOpenInTab(urlString)) {
                const urlWithIframe = addIframeParam(urlString);
                return originalReplace.call(this, urlWithIframe);
            }
            return originalReplace.call(this, urlString);
        };
    }
})();

