"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PluginBuilder=exports.MiddlewareChain=void 0,exports.createPluginContext=createPluginContext,exports.createPlugin=createPlugin;const shared_1=require("../_deps/shared/index.js");class MiddlewareChain{plugins=[];initialized=!1;constructor(e=[]){this.plugins=[...e].sort((e,r)=>(e.priority??100)-(r.priority??100))}register(e){return this.plugins.some(r=>r.name===e.name)?(0,shared_1.err)(`PLUGIN_ALREADY_REGISTERED:${e.name}`):(this.plugins.push(e),this.plugins.sort((e,r)=>(e.priority??100)-(r.priority??100)),(0,shared_1.ok)(void 0))}unregister(e){const r=this.plugins.findIndex(r=>r.name===e);return-1===r?(0,shared_1.err)(`PLUGIN_NOT_FOUND:${e}`):(this.plugins.splice(r,1),(0,shared_1.ok)(void 0))}getPlugins(){return[...this.plugins]}async initialize(){if(this.initialized)return(0,shared_1.ok)(void 0);for(const e of this.plugins)if(e.onInit){const r=await e.onInit();if(!1===r.ok)return(0,shared_1.err)(`PLUGIN_INIT_FAILED:${e.name}:${r.error}`)}return this.initialized=!0,(0,shared_1.ok)(void 0)}async destroy(){const e=[];for(const r of this.plugins)if(r.onDestroy){const t=await r.onDestroy();!1===t.ok&&e.push(`${r.name}:${t.error}`)}return this.initialized=!1,e.length>0?(0,shared_1.err)(`PLUGIN_DESTROY_FAILED:${e.join(",")}`):(0,shared_1.ok)(void 0)}async executeSend(e,r){if(!this.initialized)return(0,shared_1.err)("MIDDLEWARE_NOT_INITIALIZED");let t=e;for(const e of this.plugins)if(e.onSend){const i=await e.onSend(t,r);if(!1===i.ok)return(0,shared_1.err)(`PLUGIN_HOOK_FAILED:${e.name}:${i.error}`);const n=i.value;if(n.abort)return(0,shared_1.err)(`PLUGIN_ABORTED:${e.name}:${n.reason??"no reason"}`);t=n.payload,n.metadata&&Object.assign(r.metadata,n.metadata)}return(0,shared_1.ok)(t)}async executeReceive(e,r){if(!this.initialized)return(0,shared_1.err)("MIDDLEWARE_NOT_INITIALIZED");let t=e;for(const e of this.plugins)if(e.onReceive){const i=await e.onReceive(t,r);if(!1===i.ok)return(0,shared_1.err)(`PLUGIN_HOOK_FAILED:${e.name}:${i.error}`);const n=i.value;if(n.abort)return(0,shared_1.err)(`PLUGIN_ABORTED:${e.name}:${n.reason??"no reason"}`);t=n.payload,n.metadata&&Object.assign(r.metadata,n.metadata)}return(0,shared_1.ok)(t)}async executeEncrypt(e,r){if(!this.initialized)return(0,shared_1.err)("MIDDLEWARE_NOT_INITIALIZED");let t=e;for(const e of this.plugins)if(e.onEncrypt){const i=await e.onEncrypt(t,r);if(!1===i.ok)return(0,shared_1.err)(`PLUGIN_HOOK_FAILED:${e.name}:${i.error}`);const n=i.value;if(n.abort)return(0,shared_1.err)(`PLUGIN_ABORTED:${e.name}:${n.reason??"no reason"}`);t=n.payload,n.metadata&&Object.assign(r.metadata,n.metadata)}return(0,shared_1.ok)(t)}async executeDecrypt(e,r){if(!this.initialized)return(0,shared_1.err)("MIDDLEWARE_NOT_INITIALIZED");let t=e;for(const e of this.plugins)if(e.onDecrypt){const i=await e.onDecrypt(t,r);if(!1===i.ok)return(0,shared_1.err)(`PLUGIN_HOOK_FAILED:${e.name}:${i.error}`);const n=i.value;if(n.abort)return(0,shared_1.err)(`PLUGIN_ABORTED:${e.name}:${n.reason??"no reason"}`);t=n.payload,n.metadata&&Object.assign(r.metadata,n.metadata)}return(0,shared_1.ok)(t)}}function createPluginContext(e,r={}){return{agent:e,recipient:r.recipient,scope:r.scope,timestamp:r.timestamp??Date.now(),state:r.state??new Map,metadata:r.metadata??{}}}exports.MiddlewareChain=MiddlewareChain;class PluginBuilder{plugin={};constructor(e,r){this.plugin.name=e,this.plugin.version=r}description(e){return this.plugin.description=e,this}priority(e){return this.plugin.priority=e,this}onSend(e){return this.plugin.onSend=e,this}onReceive(e){return this.plugin.onReceive=e,this}onEncrypt(e){return this.plugin.onEncrypt=e,this}onDecrypt(e){return this.plugin.onDecrypt=e,this}onInit(e){return this.plugin.onInit=e,this}onDestroy(e){return this.plugin.onDestroy=e,this}build(){if(!this.plugin.name||!this.plugin.version)throw new Error("Plugin name and version are required");return this.plugin}}function createPlugin(e,r){return new PluginBuilder(e,r)}exports.PluginBuilder=PluginBuilder;