const cheerio=require("cheerio"),{writeFile:writeFile,readFileSync:readFileSync,readDir:readDir,isMobileProject:isMobileProject,isPrismProject:isPrismProject}=require("./wm-utils"),markScriptsAsLazy=s=>{const e=["./node_modules/d3/dist/d3.min.js","./node_modules/@wavemaker/nvd3/build/nv.d3.min.js","./node_modules/fullcalendar/main.min.js","./node_modules/summernote/dist/summernote-lite.min.js",global._WM_PACKAGE_PATH+"/scripts/datatable/datatable.js","./node_modules/jquery-ui/ui/widgets/sortable.js","./node_modules/jquery-ui/ui/widgets/droppable.js","./node_modules/jquery-ui/ui/widgets/resizable.js","./node_modules/hammerjs/hammer.min.js","./node_modules/iscroll/build/iscroll.js"];let t=s.replace(/^(\.\/)/,"").replace(/\//g,"-").replace(/@/g,"");return t=t.substring(0,t.lastIndexOf(".")),t+="-NOHASH",e.includes(s)?{input:s,inject:!1,bundleName:t}:s},setBuildCustomizations=async s=>{const e=s.projects["angular-app"].architect.build,t=e.options.scripts;e.options.scripts=t.map(s=>markScriptsAsLazy(s))},addToScripts=async(s,e,t=[],i=[])=>{const n=`${e}/angular.json`;let o=readFileSync(n,!0);const r=o.projects["angular-app"].architect.build;let a=r.options.scripts;const l=`${e+"/src/app/extensions"}`,c=await readDir(l);c.length&&i.push("./src/app/extensions/"+c[0]),r.options.scripts=[...t,...a,...i],await writeFile(n,JSON.stringify(o,null,4))},updateAngularJSON=async(s,e,t,i,n,o,r,a,l,c)=>{const u=`${e}/angular.json`;let d=readFileSync(u,!0);const p=d.projects["angular-app"].architect.build;r&&r.trim().length>0&&(p.options.deployUrl=r);let m=p.options.styles;if(isPrismProject(t)&&((m=m.filter(s=>"string"==typeof s?!(s.includes("src/assets/styles/css/wm-style.css")||s.includes("src/assets/styles/css/wm-responsive.css")):"object"!=typeof s||!s.input||!s.input.includes("src/assets/themes/material/style.css"))).push("./node_modules/@wavemaker/foundation-css/foundation/foundation.css"),p.options.styles=m),isMobileProject(t)){const s=m.findIndex(s=>{let e="object"==typeof s;if(e){return(e?s.input:s).includes("themes")}return!1}),e=m[s].input;m[s].bundleName="wm-android-styles",m[s].input=m[s].input.replace(new RegExp("/[a-z]*/style.css$"),`/${i.activeTheme}/android/style.css`);const t=Object.assign({},m[s]);t.input=e.replace(new RegExp("/[a-z]*/style.css$"),`/${i.activeTheme}/ios/style.css`),t.bundleName="wm-ios-styles",m.push(t);const n=m.findIndex(s=>{let e="object"==typeof s;if(e){return"src/assets/app.css"===(e?s.input:s)}return!1});m[n].bundleName="wm-android-styles";const o=Object.assign({},m[n]);o.bundleName="wm-ios-styles",m.push(o)}m.forEach((s,e)=>{let t="object"==typeof s,n=t?s.input:s;!n.includes("themes")||n.includes("/android/")||n.includes("/ios/")||(t?m[e].input=n.replace(new RegExp("/[a-z]*/style.css$"),`/${i.activeTheme}/style.css`):m[e]=n.replace(new RegExp("/[a-z]*/style.css$"),`/${i.activeTheme}/style.css`))}),await setBuildCustomizations(d);const y=p.configurations.production;if(y.aot=!1!==a.aot,y.buildOptimizer=!1!==a.buildOptimizer,c){const s=p.options.assets;s.push("src/manifest.json"),s.push("src/wmsw-worker.js"),y.serviceWorker=!0,y.ngswConfigPath="ngsw-config.json",delete p.options.customWebpackConfig}await writeFile(u,JSON.stringify(d,null,4))};module.exports={updateAngularJSON:updateAngularJSON,addToScripts:addToScripts};