/* jshint esversion: 6, laxcomma:true  */

// fbt_show_cross_report.embed_from_extfiles.js
function dts() { return new Date().toISOString().substring(11,23); }
document.addEventListener
(
    "DOMContentLoaded",
    function(event) {
        "use strict";
        // let snipLst = document.querySelectorAll('div.resultsSnippet[data-fromfile]');
        let snipLst = document.querySelectorAll('div.externalFile[data-fromfile]');
        let fileWithSnippet;
        let rawSources = [];
        let fileExt;
        let rawFile;
        let snipId;
        function getFileExtension(filename) {
            const parts = filename.split('.');
            return parts.length > 1 ? parts.pop() : '';
        }
        for (let i = 0; i < snipLst.length; i++) {
            snipId=snipLst[i].getAttribute("id");
            fileWithSnippet = snipLst[i].getAttribute("data-fromfile");
            fileExt = getFileExtension(fileWithSnippet);
            if (fileWithSnippet) {
                rawFile = new XMLHttpRequest();
                rawFile.open("GET", fileWithSnippet );
                rawFile.setRequestHeader("Cache-Control", "no-cache, no-store, max-age=0");
                // Optional: Fallbacks for older browsers or specific scenarios
                rawFile.setRequestHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT");
                rawFile.setRequestHeader("Pragma", "no-store,no-cache");
                rawFile.responseType = fileExt == "lzma" ? 'arraybuffer' : 'text';
                rawSources.push([rawFile, fileExt, snipId]);
                // rest will be done in separate loop.
            }
        }
        for (let i = 0; i < rawSources.length; i++) {
            rawSources[i][0].onreadystatechange = function () {
                let srcDiv, buffer, inStream, outStream, utf8decoder, divText;
                if(rawSources[i][0].readyState === 4) {
                    if ( [200, 0].includes(rawSources[i][0].status) ) {
                       srcDiv = document.getElementById(rawSources[i][2]);
                       if (rawSources[i][1] == "lzma") {
                           buffer = rawSources[i][0].response;
                           inStream = new LZMA.iStream(buffer);
                           outStream = LZMA.decompressFile(inStream).toUint8Array();
                           utf8decoder = new TextDecoder("utf-8");
                           divText = utf8decoder.decode(outStream);
                       } else {
                           divText = rawSources[i][0].responseText;
                       }
                       srcDiv.innerHTML = divText;
                       // Google AI. Find and execute scripts within the newly added HTML
                       const scripts = srcDiv.querySelectorAll('script');
                       scripts.forEach(oldScript => {
                           const newScript = document.createElement('script');
                           if (oldScript.src) {
                               newScript.src = oldScript.src;
                           } else {
                               newScript.textContent = oldScript.textContent;
                           }
                           // Append the new script to the same parent or document.body
                           oldScript.parentNode.replaceChild(newScript, oldScript);
                       });
                    }
                }
            };
            rawSources[i][0].send(null);
        }
    }
);
