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=[])=>{const t=e.filter(e=>"PAGE"!=e.type).map(e=>`src/app/partials/${e.name}/${e.name}.module`);for(const e of s.keys())t.push(`src/app/prefabs/${e}/${e}.module`);return t},addToScripts=async(e,s,t=[],i=[])=>{const a=`${s}/angular.json`;let n=readFileSync(a,!0);const l=n.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(a,JSON.stringify(n,null,4))},updateAngularJSON=async(e,s,t,i,a,n,l)=>{const o=`${s}/angular.json`;let r=readFileSync(o,!0);const c=r.projects["angular-app"].architect.build;let d=c.options.lazyModules;d.length=0,d.push(...getLazyModules(i,a)),n&&n.trim().length>0&&(c.options.deployUrl=n);const u=c.options.styles;if(isMobileProject(t)){const e=u.findIndex(e=>{let s="object"==typeof e;if(s){return(s?e.input:e).includes("themes")}return!1}),s=`mobile_${t.activeTheme}`,i=u[e].input;u[e].bundleName=s+"_android",u[e].input=u[e].input.replace(new RegExp("/[a-z]*/style.css$"),`/${t.activeTheme}/android/style.css`);const a=Object.assign({},u[e]);a.input=i.replace(new RegExp("/[a-z]*/style.css$"),`/${t.activeTheme}/ios/style.css`),a.bundleName=s+"_ios",u.push(a)}u.forEach((e,s)=>{let i="object"==typeof e,a=i?e.input:e;!a.includes("themes")||a.includes("/android/")||a.includes("/ios/")||(i?u[s].input=a.replace(new RegExp("/[a-z]*/style.css$"),`/${t.activeTheme}/style.css`):u[s]=a.replace(new RegExp("/[a-z]*/style.css$"),`/${t.activeTheme}/style.css`))}),await setBuildCustomizations(r);const p=c.configurations.production;p.aot=!1!==l.aot,p.buildOptimizer=!1!==l.buildOptimizer,await writeFile(o,JSON.stringify(r,null,4))};module.exports={updateAngularJSON:updateAngularJSON,addToScripts:addToScripts};