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