const{createDir:createDir,writeFile:writeFile,isPrefabProject:isPrefabProject,getPagesDirPath:getPagesDirPath,formatContents:formatContents,getComponentName:getComponentName,getComponentModuleName:getComponentModuleName,getPrefabPagesDirPath:getPrefabPagesDirPath}=require("./wm-utils"),fs=require("fs"),cheerio=require("cheerio"),{getPageContents:getPageContents}=require("./pages-util"),{getHandlebarTemplate:getHandlebarTemplate,safeString:safeString}=require("./handlebar-helpers"),writeComponentHtmlFile=async(e,t,a)=>{const n=`${a}/${e}.component.html`;await writeFile(n,t)},writeComponentCssFile=async(e,t,a)=>{const n=`${a}/${e}.component.css`;await writeFile(n,t)},writeComponentVariablesFile=async(e,t,a,n)=>{let r=getHandlebarTemplate(e)({variables:a});await writeFile(`${n}/${t}.component.variables.ts`,formatContents(r))},writeComponentJsFile=async(e,t,a,n)=>{const r=getHandlebarTemplate(e)({jsContent:a});await writeFile(`${n}/${t}.component.script.js`,formatContents(r,"javascript"))},writeComponentTsFile=async(e,t,a,n)=>{let r=getHandlebarTemplate(e)({name:t,prefabName:a,componentName:getComponentName(t)});await writeFile(`${n}/${t}.component.ts`,r)},writePagePartialModuleTsFile=async(e,t,a,n,r,o,i=!1,s)=>{let m=getHandlebarTemplate(e)({name:t,componentName:getComponentName(t),moduleName:getComponentModuleName(t),requiredPartials:n,requiredPrefabs:r,requiredWMComponents:o,hasCustomElement:i,insidePrefab:!!s});await writeFile(`${a}/${t}.module.ts`,m)},writePrefabModuleTsFile=async(e,t,a,n=!1,r)=>{let o=getHandlebarTemplate("prefab-module-ts")({name:e,componentName:getComponentName(e),moduleName:getComponentModuleName(e),requiredPartials:r,requiredWMComponents:a,hasCustomElement:n});await writeFile(`${t}/${e}.module.ts`,o)},generateComponent=async(e,t,a,n,r,o)=>{await createDir(e,{recursive:!0});const{markup:i,styles:s,variables:m,script:l}=n;return Promise.all([writeComponentHtmlFile(t,safeString(i),e),writeComponentCssFile(t,safeString(s),e),writeComponentVariablesFile("component-variables",t,safeString(m),e),writeComponentJsFile(o,t,safeString(l),e),writeComponentTsFile(r,t,a,e)])},findRequiredPartials=e=>{const t={};return e("[partialContainer]").map((t,a)=>e(a).attr("content")).toArray().map(e=>({type:"PARTIAL",name:e.trim()})).filter(e=>!t[e.name]&&(t[e.name]=!0))},findRequiredPrefabs=e=>{const t={};return e("[wmPrefab]").map((t,a)=>e(a).attr("prefabname")).toArray().map(e=>({type:"PREFAB",name:e.trim()})).filter(e=>!t[e.name]&&(t[e.name]=!0))},generatePagePartialComponent=async(e,t,a,n,r,o,i)=>{let s,m,l,p;if("PAGE"===n?(p=`${r}/src/app/pages/${a}`,s="page-component-ts",m="page-component-script",l="page-module-ts"):(p=o?`${r}/partials/${a}`:`${r}/src/app/partials/${a}`,s="partial-component-ts",m="partial-component-script",l="partial-module-ts"),fs.existsSync(`${p}/${a}.component.ts`))return;let g=`${e}/${a}`;o&&(g=`${getPrefabPagesDirPath(i,o)}/${a}`);const c=await getPageContents(t,g,`${a}`,a,n);try{const n=cheerio.load(c.markup),g=findRequiredPartials(n),P=findRequiredPrefabs(n);for(const a of g)await generatePagePartialComponent(e,t,a.name,a.type,r,o,i);await generateComponent(p,a,void 0,c,s,m),await writePagePartialModuleTsFile(l,a,p,g,P,c.requiredWMComponents,c.hasCustomElement,o)}catch(e){throw console.log("failed in "+a),e}},generateComponents=async(e,t,a,n,r,o)=>{const i=getPagesDirPath(e);for(const a of n.keys()){const n="__self__"===a?`${i}/Main`:`${getPrefabPagesDirPath(e,a)}/Main`,m=await getPageContents(t,n,"Main",a,"PREFAB"),l=`${r}/src/app/prefabs/${a}`,p=cheerio.load(m.markup),g=findRequiredPartials(p);await generateComponent(l,a,a,m,"prefab-component-ts","prefab-component-script");const c=o[a];for(var s=0;s<c.length;s++)await generatePagePartialComponent(i,t,c[s].name,c[s].type,l,a,e);await writePrefabModuleTsFile(a,l,m.requiredWMComponents,m.hasCustomElement,g)}if(!isPrefabProject(t))for(let n of a)await generatePagePartialComponent(i,t,n.name,n.type,r,n.prefabName,e)};module.exports={generateComponents:generateComponents,generatePagePartialComponent:generatePagePartialComponent};