import{ok,err}from"./_deps/shared/index.js";export class XBindEventEmitter{listeners=new Map;parent;disposed=!1;eventCounter=0;maxListeners=10;warningsEnabled=!0;constructor(e){this.parent=e}on(e,t,s={}){if(this.checkDisposed(),"function"!=typeof t)throw new Error("Listener must be a function");const r=this.listeners.get(e)??[],n={listener:t,once:s.once??!1,priority:s.priority??0};return r.push(n),r.sort((e,t)=>t.priority-e.priority),this.listeners.set(e,r),this.warningsEnabled&&this.maxListeners>0&&r.length>this.maxListeners&&console.warn(`[xBind EventEmitter] Possible memory leak detected: ${r.length} listeners for event '${e}'. Use emitter.setMaxListeners() to increase limit.`),this}once(e,t,s={}){return this.on(e,t,{...s,once:!0})}off(e,t){this.checkDisposed();const s=this.listeners.get(e);if(!s)return this;const r=s.filter(e=>e.listener!==t);return 0===r.length?this.listeners.delete(e):this.listeners.set(e,r),this}removeAllListeners(e){return this.checkDisposed(),e?this.listeners.delete(e):this.listeners.clear(),this}async emit(e,t,s={}){this.checkDisposed();const r={...t,eventId:s.eventId??this.generateEventId(),timestamp:(new Date).toISOString()},n=this.listeners.get(e);if(n&&n.length>0){const t=[...n];for(const n of t)try{if(await n.listener(r),n.once&&this.off(e,n.listener),s.stopPropagation)return ok(void 0)}catch(t){return console.error(`[xBind EventEmitter] Error in listener for '${e}':`,t),err("LISTENER_ERROR")}}return!1!==s.bubble&&this.parent?this.parent.emit(e,t,{...s,bubble:!0}):ok(void 0)}hasListeners(e){const t=this.listeners.get(e);return void 0!==t&&t.length>0}listenerCount(e){const t=this.listeners.get(e);return t?.length??0}eventNames(){return Array.from(this.listeners.keys())}setMaxListeners(e){if(e<0||!Number.isInteger(e))throw new Error("Max listeners must be a non-negative integer");return this.maxListeners=e,this}getMaxListeners(){return this.maxListeners}setWarningsEnabled(e){return this.warningsEnabled=e,this}setParent(e){return this.parent=e,this}getParent(){return this.parent}waitFor(e,t){return new Promise((s,r)=>{let n;const i=e=>{n&&clearTimeout(n),s(e)};this.once(e,i),void 0!==t&&t>0&&(n=setTimeout(()=>{this.off(e,i),r(new Error(`Timeout waiting for event '${e}' after ${t}ms`))},t))})}dispose(){this.disposed=!0,this.listeners.clear(),this.parent=void 0}isDisposed(){return this.disposed}checkDisposed(){if(this.disposed)throw new Error("EventEmitter has been disposed")}generateEventId(){return`evt-${Date.now()}-${++this.eventCounter}`}}export function createScopedEmitter(e,t){const s=new XBindEventEmitter(e),r=new Set(t);return{on:(e,t,n)=>{if(!r.has(e))throw new Error(`Event type '${String(e)}' not allowed in scoped emitter`);return s.on(e,t,n)},once:(e,t,n)=>{if(!r.has(e))throw new Error(`Event type '${String(e)}' not allowed in scoped emitter`);return s.once(e,t,n)},off:(e,t)=>s.off(e,t),emit:(e,t,n)=>r.has(e)?s.emit(e,t,n):Promise.resolve(err("INVALID_EVENT_TYPE")),hasListeners:e=>s.hasListeners(e),listenerCount:e=>s.listenerCount(e),dispose:()=>s.dispose()}}