"use strict";var __rest=this&&this.__rest||function(e,t){var o={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(o[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(i=Object.getOwnPropertySymbols(e);r<i.length;r++)t.indexOf(i[r])<0&&Object.prototype.propertyIsEnumerable.call(e,i[r])&&(o[i[r]]=e[i[r]])}return o},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});const protocol_1=require("../../protocol"),compile_1=require("../../protocol/compile"),path_1=__importDefault(require("path")),Module_1=__importDefault(require("../../common/Module")),flow_1=__importDefault(require("../../common/flow")),protocol_2=require("../../types/protocol"),unpack_1=require("./help/unpack"),simulator_1=require("../../types/simulator"),common_1=require("../../config/common"),block_1=__importDefault(require("./help/block")),gadget_1=__importDefault(require("./help/gadget"));class CompileModule extends Module_1.default{constructor(){super(protocol_2.ModuleName.compile),this.compileMap=new Map,this.compilePath="",this.compileTaskId=0}findTaskId(e,t){const o=[];for(const[i,r]of this.compileMap)r.options.sourcePath===e&&("all"!==t&&t!==r.compileType||o.push(i));return o}getOutputPath(e,t,o="gadget"){switch(e){case"debug":return this.execute(protocol_2.ModuleName.storage,protocol_1.StorageCmdEnum.Get,t,o,"tmpConfig").debugDist;case"live":return this.execute(protocol_2.ModuleName.storage,protocol_1.StorageCmdEnum.Get,t,o,"tmpConfig").livePreviewDist;default:return this.execute(protocol_2.ModuleName.storage,protocol_1.StorageCmdEnum.Get,t,o,"tmpConfig").dist}}async compile(e){const t=this.log.bind(this,"info","compile");t("receiveParams","end",{opt:e});const{from:o,compilePath:i}=e,r=__rest(e,["from","compilePath"]),l=++this.compileTaskId,p=await this.execute(protocol_2.ModuleName.sniffer,protocol_1.SnifferCmdEnum.GetAppType,i);t("getAppType","end",{appType:p}),this.emit(compile_1.EventEnum.Start,{compileId:l,from:o,appType:p});const a=this.getOutputPath(o,i,p)||path_1.default.join(i,"__dist__");t("getTargetPath","end",{targetPath:a});const{useUpgrade:m}=this.execute(protocol_2.ModuleName.sniffer,protocol_1.SnifferCmdEnum.GetFrameworkOption,e.compilePath);this[compile_1.CmdEnum.ClearByPath](i,o);const s=new(this.getCompile(p))(this,{sourcePath:i,distPath:a,appType:p,type:o,appInfo:Object.assign(Object.assign({},r),{frameWorkType:m?"upgrade":"default"})});return this.compileMap.set(l,s),s.run().then(e=>(t("compile","end",{targetPath:a,res:e}),this.emit(compile_1.EventEnum.End,{compileId:l,result:e}),{error:(null==e?void 0:e.errMsg)||"",taskId:l,distPath:a}))}async[compile_1.CmdEnum.Compile](e){return await this.compile(e)}async[compile_1.CmdEnum.SetPath](e){this.log.bind(this,"info",compile_1.CmdEnum.SetPath)("receiveParams","end",{compilePath:e});const t=this.compilePath||void 0;if(this.execute(protocol_2.ModuleName.sniffer,protocol_1.SnifferCmdEnum.GetAppCompileType,e)===simulator_1.CompilePathType.unknown)return Promise.resolve(!1);this.compilePath=e;const o=await this.execute(protocol_2.ModuleName.sniffer,protocol_1.SnifferCmdEnum.GetAppType,e);return new Promise((i,r)=>{this.execute(protocol_2.ModuleName.storage,protocol_1.StorageCmdEnum.InitProject,e,o).then(()=>{this.emit(compile_1.EventEnum.PathChange,{old:t,now:e}),i(!0)},r)})}[compile_1.CmdEnum.GetPath](){return this.compilePath||void 0}[compile_1.CmdEnum.ListPath](e){const t=this.log.bind(this,"info",compile_1.CmdEnum.ListPath),o={};return e?o[e]=flow_1.default.runWithContext(this,e,"listCompilePaths"):flow_1.default.appTypes.filter(e=>"block"!==e||(0,common_1.featureGatingBlock)()).forEach(e=>{o[e]=flow_1.default.runWithContext(this,e,"listCompilePaths")}),t("listPath","end",{result:o}),Promise.resolve(o)}[compile_1.CmdEnum.Unpack](e,t){return(0,unpack_1.unpack)(e,t)}[compile_1.CmdEnum.ClearByPath](e,t="all"){this.findTaskId(e,t).forEach(e=>this[compile_1.CmdEnum.Clear](e))}[compile_1.CmdEnum.Clear](e){if(void 0===e)return this.compileMap.forEach(e=>null==e?void 0:e.destroy()),void this.compileMap.clear();if(this.compileMap.has(e)){const t=this.compileMap.get(e);null==t||t.destroy(),this.compileMap.delete(e)}}getCompile(e){switch(e){case"block":return block_1.default;default:return gadget_1.default}}}exports.default=CompileModule;