import{InvalidStateError}from"./errors.js";const stopped=(event,listener)=>event.immediatePropagationStopped||event.eventPhase===event.CAPTURING_PHASE&&!1===listener.capture||event.eventPhase===event.BUBBLING_PHASE&&!0===listener.capture,invokeEventListeners=(event,obj)=>{event.currentTarget=obj;for(const listener of obj.listeners.slice())event.type!==listener.type||stopped(event,listener)||listener.callback.call(event.currentTarget,event);const prop={abort:"onabort",blocked:"onblocked",complete:"oncomplete",error:"onerror",success:"onsuccess",upgradeneeded:"onupgradeneeded",versionchange:"onversionchange"}[event.type];if(void 0===prop)throw new Error(`Unknown event type: "${event.type}"`);const callback=event.currentTarget[prop];if(callback){const listener={callback:callback,capture:!1,type:event.type};stopped(event,listener)||listener.callback.call(event.currentTarget,event)}};class FakeEventTarget{listeners=[];addEventListener(type,callback,capture=!1){this.listeners.push({callback:callback,capture:capture,type:type})}removeEventListener(type,callback,capture=!1){const i=this.listeners.findIndex((listener=>listener.type===type&&listener.callback===callback&&listener.capture===capture));this.listeners.splice(i,1)}dispatchEvent(event){if(event.dispatched||!event.initialized)throw new InvalidStateError("The object is in an invalid state.");event.isTrusted=!1,event.dispatched=!0,event.target=this,event.eventPhase=event.CAPTURING_PHASE;for(const obj of event.eventPath)event.propagationStopped||invokeEventListeners(event,obj);if(event.eventPhase=event.AT_TARGET,event.propagationStopped||invokeEventListeners(event,event.target),event.bubbles){event.eventPath.reverse(),event.eventPhase=event.BUBBLING_PHASE;for(const obj of event.eventPath)event.propagationStopped||invokeEventListeners(event,obj)}return event.dispatched=!1,event.eventPhase=event.NONE,event.currentTarget=null,!event.canceled}}export default FakeEventTarget;