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/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,"-");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)=>{const c=`${s}/angular.json`;let d=readFileSync(c,!0);const u=d.projects["angular-app"].architect.build;o&&o.trim().length>0&&(u.options.deployUrl=o);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 o=Object.assign({},p[n]);o.bundleName="wm-ios-styles",p.push(o)}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(d);const m=u.configurations.production;if(m.aot=!1!==a.aot,m.buildOptimizer=!1!==a.buildOptimizer,l){const e=u.options.assets;e.push("src/manifest.json"),e.push("src/wmsw-worker.js"),m.serviceWorker=!0,m.ngswConfigPath="ngsw-config.json",delete u.options.customWebpackConfig}await writeFile(c,JSON.stringify(d,null,4))};module.exports={updateAngularJSON:updateAngularJSON,addToScripts:addToScripts};