const cheerio=require("cheerio"),{writeFile:writeFile,readFileSync:readFileSync,readDir:readDir,isMobileProject:isMobileProject}=require("./wm-utils"),markScriptsAsLazy=e=>{const s=["./node_modules/d3/d3.min.js","./node_modules/@wavemaker.com/nvd3/build/nv.d3.min.js","./node_modules/fullcalendar/dist/fullcalendar.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=e.replace(/^(\.\/)/,"").replace(/\//g,"-");return t=t.substring(0,t.lastIndexOf(".")),t+="-NOHASH",s.includes(e)?{input:e,lazy:!0,bundleName:t}:e},setBuildCustomizations=async e=>{const s=e.projects["angular-app"].architect.build,t=s.options.scripts;s.options.scripts=t.map(e=>markScriptsAsLazy(e))},getLazyModules=(e=[],s=[],t)=>{const i=e.filter(e=>"PAGE"!=e.type).map(e=>`src/app/partials/${e.name}/${e.name}.module`);for(const e of s.keys()){i.push(`src/app/prefabs/${e}/${e}.module`);const s=t[e];for(var n=0;n<s.length;n++)i.push(`src/app/prefabs/${e}/partials/${s[n].name}/${s[n].name}.module`)}return i},addToScripts=async(e,s,t=[],i=[])=>{const n=`${s}/angular.json`;let a=readFileSync(n,!0);const l=a.projects["angular-app"].architect.build;let o=l.options.scripts;const r=`${s+"/src/app/extensions"}`,c=await readDir(r);c.length&&i.push("./src/app/extensions/"+c[0]),l.options.scripts=[...t,...o,...i],await writeFile(n,JSON.stringify(a,null,4))},updateAngularJSON=async(e,s,t,i,n,a,l,o)=>{const r=`${s}/angular.json`;let c=readFileSync(r,!0);const u=c.projects["angular-app"].architect.build;let d=u.options.lazyModules;d.length=0,d.push(...getLazyModules(i,n,o)),a&&a.trim().length>0&&(u.options.deployUrl=a);const p=u.options.styles;if(isMobileProject(t)){const e=p.findIndex(e=>{let s="object"==typeof e;if(s){return(s?e.input:e).includes("themes")}return!1}),s=p[e].input;p[e].bundleName="wm-android-styles",p[e].input=p[e].input.replace(new RegExp("/[a-z]*/style.css$"),`/${t.activeTheme}/android/style.css`);const i=Object.assign({},p[e]);i.input=s.replace(new RegExp("/[a-z]*/style.css$"),`/${t.activeTheme}/ios/style.css`),i.bundleName="wm-ios-styles",p.push(i);const n=p.findIndex(e=>{let s="object"==typeof e;if(s){return"src/assets/app.css"===(s?e.input:e)}return!1});p[n].bundleName="wm-android-styles";const a=Object.assign({},p[n]);a.bundleName="wm-ios-styles",p.push(a)}p.forEach((e,s)=>{let i="object"==typeof e,n=i?e.input:e;!n.includes("themes")||n.includes("/android/")||n.includes("/ios/")||(i?p[s].input=n.replace(new RegExp("/[a-z]*/style.css$"),`/${t.activeTheme}/style.css`):p[s]=n.replace(new RegExp("/[a-z]*/style.css$"),`/${t.activeTheme}/style.css`))}),await setBuildCustomizations(c);const m=u.configurations.production;m.aot=!1!==l.aot,m.buildOptimizer=!1!==l.buildOptimizer,await writeFile(r,JSON.stringify(c,null,4))};module.exports={updateAngularJSON:updateAngularJSON,addToScripts:addToScripts};