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