"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.addSkylineRendererToComponents=addSkylineRendererToComponents,exports.compileJSON=compileJSON;const tslib_1=require("tslib"),path_1=tslib_1.__importDefault(require("path")),app_1=require("../../json/app"),extJSON_1=require("../../json/app/extJSON"),common_1=require("../../../../../utils/common"),getPageJSON_1=require("../../json/page/getPageJSON"),taskstatus_1=require("../../../../../utils/taskstatus"),projectconfig_1=require("../../json/projectconfig"),common_2=require("../common");function addSkylineRendererToComponents(e,t){const n=new Set,o=new Set;function s(e,t){Object.values(t.usingComponents||{}).forEach(t=>{const s=t.startsWith("/")?t.substring(1)+".json":path_1.default.posix.join(path_1.default.posix.dirname(e),t+".json");n.has(s)||o.add(s)}),Object.values(t.componentGenerics||{}).forEach(t=>{if("object"==typeof t&&"string"==typeof t.default){const s=t.default.startsWith("/")?t.default.substring(1)+".json":path_1.default.posix.join(path_1.default.posix.dirname(e),t.default+".json");n.has(s)||o.add(s)}})}for(const t of Object.keys(e)){const n=JSON.parse(e[t]);"skyline"===n.renderer&&s(t,n)}for(;o.size>0;){const e=o.values().next().value;if(e&&(o.delete(e),n.add(e),t[e]&&"string"==typeof t[e])){const n=JSON.parse(t[e]);if("xr-frame"===n.renderer)continue;if("webview"===n.renderer)throw new Error(`The component (${e}) is configured with renderer: 'webview', but is used in skyline pages`);n.renderer="skyline",t[e]=JSON.stringify(n),s(e,n)}}}async function compilePageJSON(e,t,n,o){const{onProgressUpdate:s=(()=>{})}=o,a={};for(const i of t){const t=new taskstatus_1.TaskStatus(i);s(t);const r=await(o.disableSpreadingUsingComponents?getPageJSON_1.getPageJSONWithDisableSpreading:getPageJSON_1.getPageJSON)(e,{miniprogramRoot:n,pagePath:i});a[path_1.default.posix.join(n,i+".json")]=JSON.stringify(Object.assign(Object.assign({},r),{__warning__:void 0})),t.done(),s(t)}return a}async function compileJSON(e,t){const{onProgressUpdate:n=(()=>{})}=t,o=(0,projectconfig_1.getProjectConfigJSON)(e),{miniprogramRoot:s=""}=o;let a=new taskstatus_1.TaskStatus("app.json");n(a);const i=(0,app_1.getAppJSON)(e);let r;a.done(),n(a);const p=await e.attr();(null==p?void 0:p.platform)&&(a=new taskstatus_1.TaskStatus("ext.json"),n(a),r=await(0,extJSON_1.getExtJSON)(e),a.done(),n(a));const c=(0,common_1.getAllPages)(i),l=await compilePageJSON(e,c,s,t),d=new Set(["app.json","ext.json"].concat(c.map(e=>e+".json"))),g=(0,common_1.getAllTargetTypeFilesWithOtherTypeFilesOfSameName)(e,".json",[".wxml",".js"],s).filter(common_2.isNotIgnoredByProjectConfig.bind(null,o,s)).filter(e=>{const t=path_1.default.posix.relative(s,e);return!d.has(t)}).map(e=>path_1.default.posix.relative(s,e).replace(/\.json$/,"")),u=await compilePageJSON(e,g,s,t);g.forEach(e=>d.add(e+".json")),addSkylineRendererToComponents(l,u);const f=e.getFileList(s,".json").filter(common_2.isNotIgnoredByProjectConfig.bind(null,o,s)).filter(e=>{const t=path_1.default.posix.relative(s,e);return!d.has(t)}),m=await(0,common_2.compileOther)(e,f,t),_=Object.assign(Object.assign(Object.assign({[path_1.default.posix.join(s,"app.json")]:JSON.stringify(Object.assign(Object.assign({},i),{__warning__:void 0}))},l),u),m);return r&&(_[path_1.default.posix.join(s,"ext.json")]=JSON.stringify(r)),_}