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,r,t,i)=>{const s=await readFile(getIndexHtmlPath(e),"utf8"),n=cheerio.load(s);let o=[],c=[],a=!1,l=0,m=[];const p=["application/ld+json"];await createDir(r+"/resources/wm-inline",{recursive:!0}),n("script").each((e,r)=>{m.push(r)});for(const e of m){let t=n(e).attr("src");if(t)!a&&t.includes("wm-libs.min.js")&&(a=!0),isResourceJS(t)&&(a?c.push(t):o.push(t),n(e).remove());else if(!p.includes(n(e).attr("type"))){t=`resources/wm-inline/wm-inline-${++l}.js`;try{await writeFile(`${r}/${t}`,n(e).html()),a?c.push(t):o.push(t),n(e).remove()}catch(e){console.log(`WM-CODEGEN | Error in inline script generation | ${e}`)}}}await addToScripts(e,r,o,c),n('script[src$="/wm-loader.min.js"]').remove(),n('script[src$="/wm-libs.min.js"]').remove(),n('script[src$="/wm-mobileloader.min.js"]').remove(),n("body").append(n('script[src$="/wmProperties.js"]'));const d=n('meta[name="description"]');d.attr("content")||d.attr("content","App built on wavemaker"),i&&i.endsWith("/")&&(i=i.substr(0,i.length-1)),isMobileProject(t)&&n("body").append(n('script[src="cordova.js"]')),n('link[href$="/wm-style.css"]').remove(),n('link[href="print.css"]').remove(),n('link[theme="wmtheme"]').remove(),n('link[href$="wm-responsive.css"]').remove(),n('link[href^="themes"][href$="style.css"]').remove(),n('link[href="app.css"]').remove(),n("*").contents().each(function(){8===this.nodeType&&n(this).remove()});process.env.APPMETRICS_ENABLED,process.env.APPMETRICS_SERVER_URL;i?n('meta[name="cdnUrl"]').attr("content",i+"/"):n('meta[name="cdnUrl"]').remove(),n('link[href^="favicon."]').attr("rel","icon"),await writeFile(`${r}/src/index.html`,prettier.format(n.html(),{htmlWhitespaceSensitivity:"ignore",parser:"html",tabWidth:4}))};module.exports={generateIndexHtml:generateIndexHtml};