const{getIndexHtmlPath:getIndexHtmlPath,readFile:readFile,writeFile:writeFile,isMobileProject:isMobileProject,createDir:createDir}=require("./wm-utils"),{addToScripts:addToScripts}=require("./update-angular-json"),cheerio=require("cheerio"),prettier=require("prettier"),isResourceJS=e=>/^resources\/./.test(e),generateIndexHtml=async(e,t,s,r)=>{const n=await readFile(getIndexHtmlPath(e),"utf8"),i=cheerio.load(n);let o=[],l=[],c=!1,a=0,m=[];const p=["application/ld+json"];await createDir(t+"/resources/wm-inline",{recursive:!0}),i("script").each((e,t)=>{m.push(t)});for(const e of m){let s=i(e).attr("src");if(s)!c&&s.includes("wm-libs.min.js")&&(c=!0),isResourceJS(s)&&(c?l.push(s):o.push(s),i(e).remove());else if(!p.includes(i(e).attr("type"))){s=`resources/wm-inline/wm-inline-${++a}.js`;try{await writeFile(`${t}/${s}`,i(e).html()),c?l.push(s):o.push(s),i(e).remove()}catch(e){console.log(`WM-CODEGEN | Error in inline script generation | ${e}`)}}}await addToScripts(e,t,o,l),i('script[src$="/wm-loader.min.js"]').remove(),i('script[src$="/wm-libs.min.js"]').remove(),i('script[src$="/wm-mobileloader.min.js"]').remove(),i("body").append(i('script[src$="/wmProperties.js"]'));const d=i('meta[name="description"]');if(d.attr("content")||d.attr("content","App built on wavemaker"),r&&r.endsWith("/")&&(r=r.substr(0,r.length-1)),isMobileProject(s)){i('link[href$="/wm-style.css"]').attr("href",r+"/assets/styles/css/wm-style.css"),i('link[href$="/wm-responsive.css"]').attr("href",r+"/assets/styles/css/wm-responsive.css");const e=i('link[theme="wmtheme"]').attr("href");i('link[theme="wmtheme"]').attr("href",r+"/assets/"+e),i('link[href="app.css"]').attr("href",r+"/assets/app.css"),i('link[href="print.css"]').remove(),i("body").append(i('script[src="cordova.js"]'))}else i('link[href$="/wm-style.css"]').remove(),i('link[href="print.css"]').remove(),i('link[theme="wmtheme"]').remove(),i('link[href$="wm-responsive.css"]').remove(),i('link[href^="themes"][href$="style.css"]').remove(),i('link[href="app.css"]').remove(),i("*").contents().each(function(){8===this.nodeType&&i(this).remove()}),i("body").append('<script>\n            (function () {\n                if (typeof WMStylesPath !== "undefined") {\n                    let styleType = WMStylesPath.split(".").pop();\n                    let styleNode;\n                    if(styleType==="css"){\n                        styleNode = document.createElement("link");\n                        styleNode.type = "text/css";\n                        styleNode.rel = "stylesheet";\n                        styleNode.href = WMStylesPath;\n                    }\n                    else if(styleType==="js"){\n                        styleNode = document.createElement("script");\n                        styleNode.type = "text/javascript";\n                        styleNode.src = WMStylesPath;\n                        styleNode.defer = true;\n                    }\n                    styleNode && document\n                        .getElementsByTagName("head")[0]\n                        .appendChild(styleNode);\n                }\n            })()\n            window.onload = function() {\n                 var printCssNode = document.createElement(\'link\');\n                 printCssNode.type = \'text/css\';\n                 printCssNode.rel = \'stylesheet\';\n                 printCssNode.href = \'print.css\';\n                 printCssNode.media = \'print\';\n                 document.getElementsByTagName("head")[0].appendChild(printCssNode);\n             }\n            <\/script>');process.env.APPMETRICS_ENABLED,process.env.APPMETRICS_SERVER_URL;r?i('meta[name="cdnUrl"]').attr("content",r+"/"):i('meta[name="cdnUrl"]').remove(),i('link[href^="favicon."]').attr("rel","icon"),await writeFile(`${t}/src/index.html`,prettier.format(i.html(),{htmlWhitespaceSensitivity:"ignore",parser:"html",tabWidth:4}))};module.exports={generateIndexHtml:generateIndexHtml};