"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.onMessage=void 0,exports.initHandler=initHandler,exports.destroy=destroy,exports.messageHandler=messageHandler;const debug_1=require("../../utils/debug"),progressRecorder_1=require("../../utils/progressRecorder"),nativeCompiler_1=require("./nativeCompiler");let nativeCompiler,inited=!1;async function processSend(e){await(0,debug_1.shouldRunInMainProcess)()?emiter.emit("message",e):process.send(e)}async function initHandler(e){const{projectInfo:i,passData:n}=e.data;if(inited)return;const{miniappDirPath:t,devtoolsVersion:s}=n;nativeCompiler=new nativeCompiler_1.NativeCompiler({projectInfo:i,miniappDirPath:t,devtoolsVersion:s}),inited=!0}async function destroy(){nativeCompiler=void 0,inited=!1}const emiter=function(){const e={};return{on:(i,n)=>{e.hasOwnProperty(i)||(e[i]=[]),e[i].push(n)},emit:(i,...n)=>{const t=e[i];if((null==t?void 0:t.length)>0)for(const e of t)try{e(...n)}catch(e){}}}}(),onMessage=async e=>{emiter.on("message",e)};async function messageHandler(e){if("event"!==e.type&&"request"===e.type){const{id:i,name:n,data:t}=e,s=i,a=new progressRecorder_1.Recorder((e,i,n)=>{processSend({type:"progress",id:e,taskId:s,status:i,message:n})});t.fullEnv&&(global.fullEnv=t.fullEnv);try{let e;const r=`${n}-${s}`;if("buildNative"===n)e=await a.run(r,async()=>await nativeCompiler.build(t,a));else if("runNative"===n)e=await a.run(r,async()=>await nativeCompiler.run(t,a));else if("buildNativePlugin"===n)e=await a.run(r,async()=>await nativeCompiler.buildPlugin(t,a));else if("packIOSCloudBuildMaterial"===n)e=await a.run(r,async()=>await nativeCompiler.packIOSCloudBuildMaterial(t,a));else if("initUSBConnectionProcess"===n)e=await a.run(r,async()=>await nativeCompiler.initUSBConnectionProcess(t,a));else{if("codesignIOSApp"!==n)throw new Error("unknown command "+n);e=await a.run(r,async()=>await nativeCompiler.codesignIOSApp(t,a))}processSend({type:"response",id:i,data:e})}catch(e){processSend({type:"response",id:i,data:null,error:{code:e.code||-1,message:e.message,stack:e.stack,path:e.path||""}})}}}exports.onMessage=onMessage;