const cheerio=require("cheerio"),{writeFile:writeFile,readFileSync:readFileSync,readDir:readDir,isMobileProject:isMobileProject}=require("./wm-utils"),markScriptsAsLazy=e=>{const s=["./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=e.replace(/^(\.\/)/,"").replace(/\//g,"-").replace(/@/g,"");return t=t.substring(0,t.lastIndexOf(".")),t+="-NOHASH",s.includes(e)?{input:e,inject:!1,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))},addToScripts=async(e,s,t=[],i=[])=>{const n=`${s}/angular.json`;let o=readFileSync(n,!0);const a=o.projects["angular-app"].architect.build;let r=a.options.scripts;const l=`${s+"/src/app/extensions"}`,c=await readDir(l);c.length&&i.push("./src/app/extensions/"+c[0]),a.options.scripts=[...t,...r,...i],await writeFile(n,JSON.stringify(o,null,4))},updateAngularJSON=async(e,s,t,i,n,o,a,r,l,c)=>{const d=`${s}/angular.json`;let u=readFileSync(d,!0);const p=u.projects["angular-app"].architect.build;a&&a.trim().length>0&&(p.options.deployUrl=a);const m=p.options.styles;if(isMobileProject(t)){const e=m.findIndex(e=>{let s="object"==typeof e;if(s){return(s?e.input:e).includes("themes")}return!1}),s=m[e].input;m[e].bundleName="wm-android-styles",m[e].input=m[e].input.replace(new RegExp("/[a-z]*/style.css$"),`/${i.activeTheme}/android/style.css`);const t=Object.assign({},m[e]);t.input=s.replace(new RegExp("/[a-z]*/style.css$"),`/${i.activeTheme}/ios/style.css`),t.bundleName="wm-ios-styles",m.push(t);const n=m.findIndex(e=>{let s="object"==typeof e;if(s){return"src/assets/app.css"===(s?e.input:e)}return!1});m[n].bundleName="wm-android-styles";const o=Object.assign({},m[n]);o.bundleName="wm-ios-styles",m.push(o)}m.forEach((e,s)=>{let t="object"==typeof e,n=t?e.input:e;!n.includes("themes")||n.includes("/android/")||n.includes("/ios/")||(t?m[s].input=n.replace(new RegExp("/[a-z]*/style.css$"),`/${i.activeTheme}/style.css`):m[s]=n.replace(new RegExp("/[a-z]*/style.css$"),`/${i.activeTheme}/style.css`))}),await setBuildCustomizations(u);const g=p.configurations.production;if(g.aot=!1!==r.aot,g.buildOptimizer=!1!==r.buildOptimizer,c){const e=p.options.assets;e.push("src/manifest.json"),e.push("src/wmsw-worker.js"),g.serviceWorker=!0,g.ngswConfigPath="ngsw-config.json",delete p.options.customWebpackConfig}await writeFile(d,JSON.stringify(u,null,4))};module.exports={updateAngularJSON:updateAngularJSON,addToScripts:addToScripts};