"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.runSummerPluginHook=runSummerPluginHook;const customError_1=require("../../../utils/customError"),summerCPProject_1=require("../../../project/summerCPProject"),index_1=require("../../precompiler/index"),initPlugin_1=require("./initPlugin");class PluginContainer{constructor(){this.initedPlugins=new Map}getPluginInstance(r,e,t){let n=this.initedPlugins.get(r);return n||(n=(0,initPlugin_1.initPlugin)(r,e,t),this.initedPlugins.set(r,n)),n}clear(){this.initedPlugins.clear()}}const pluginContainer=new PluginContainer;async function runSummerPluginHook(r){var e;if("clear"===r.command)return pluginContainer.clear(),{};const{plugin:t,projectInfo:n,pluginOption:i,method:o,args:u}=r,l=new summerCPProject_1.SummerCPProject(n);await l.ready();const{targetPlatform:s,targetPlatformDefines:a}=n,c=new index_1.PreCompiler(l,void 0,void 0),P=await c.getPreCompileProject({targetPlatform:s,targetPlatformDefines:a,runEnv:"worker cp"}),g=pluginContainer.getPluginInstance(t,P,i);if("runMethod"===r.command){const r=null===(e=g.workerMethods)||void 0===e?void 0:e[o];if(!r||"function"!=typeof r)throw new Error(`the ${r} is not a workerMethod of plugin(${t})`);try{return{result:await r(...u)}}catch(r){return{error:r instanceof customError_1.CustomError?r.toJSON():r}}}return{}}