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