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.string)},writeComponentCssFile=async(e,t,a)=>{const n=`${a}/${e}.component.css`;await writeFile(n,t.string)},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"))},writeComponentExpressionsFile=async(e,t,a,n)=>{const r=getHandlebarTemplate(e)({expressions:a});await writeFile(`${n}/${t}.component.expressions.ts`,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:p,expressions: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(p),e),writeComponentExpressionsFile("component-expressions",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,s)=>{let m,p,l,g;if("PAGE"===n?(g=`${r}/src/app/pages/${a}`,m="page-component-ts",p="page-component-script",l="page-module-ts"):(g=o?`${r}/partials/${a}`:`${r}/src/app/partials/${a}`,m="partial-component-ts",p="partial-component-script",l="partial-module-ts"),fs.existsSync(`${g}/${a}.component.ts`))return;let c=`${e}/${a}`;o&&(c=`${getPrefabPagesDirPath(i,o)}/${a}`);const P=await getPageContents(t,c,`${a}`,a,n,void 0,s);try{const n=cheerio.load(P.markup),c=findRequiredPartials(n),f=findRequiredPrefabs(n);for(const a of c)await generatePagePartialComponent(e,t,a.name,a.type,r,o,i,s);await generateComponent(g,a,void 0,P,m,p),await writePagePartialModuleTsFile(l,a,g,c,f,P.requiredWMComponents,P.hasCustomElement,o)}catch(e){throw console.log("failed in "+a),e}},generateComponents=async(e,t,a,n,r,o,i)=>{const s=getPagesDirPath(e);for(const a of n.keys()){const p="__self__"===a?`${s}/Main`:`${getPrefabPagesDirPath(e,a)}/Main`,l=await getPageContents(t,p,"Main",a,"PREFAB",n.get(a),i),g=`${r}/src/app/prefabs/${a}`,c=cheerio.load(l.markup),P=findRequiredPartials(c);await generateComponent(g,a,a,l,"prefab-component-ts","prefab-component-script");const f=o[a];for(var m=0;m<f.length;m++)await generatePagePartialComponent(s,t,f[m].name,f[m].type,g,a,e,i);await writePrefabModuleTsFile(a,g,l.requiredWMComponents,l.hasCustomElement,P)}if(!isPrefabProject(t))for(let n of a)await generatePagePartialComponent(s,t,n.name,n.type,r,n.prefabName,e,i)};module.exports={generateComponents:generateComponents,generatePagePartialComponent:generatePagePartialComponent};