(function(l,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],c):(l=typeof globalThis<"u"?globalThis:l||self,c(l.VueEventsBackbone={},l.Vue))})(this,function(l,c){"use strict";var P=Object.defineProperty;var A=(l,c,f)=>c in l?P(l,c,{enumerable:!0,configurable:!0,writable:!0,value:f}):l[c]=f;var y=(l,c,f)=>A(l,typeof c!="symbol"?c+"":c,f);const f=Symbol("EventsBackboneEmitter"),w=function(s){return s=Object.defineProperties(s,{watchedIsUnmounted:{enumerable:!0,writable:!0,value:s.isUnmounted},isUnmountedCallbacks:{enumerable:!0,writable:!0,value:[]}}),Object.defineProperty(s,"isUnmounted",{get(){return this.watchedIsUnmounted},set(e){if(e!==this.watchedIsUnmounted)try{for(const t of this.isUnmountedCallbacks)t();this.isUnmountedCallbacks.length=0}catch(t){console.error(t)}this.watchedIsUnmounted=e}})};class C{constructor(){y(this,"evntsTree");y(this,"lstnrsRegistry");y(this,"compsRegistry");y(this,"listenAllSymbol",Symbol("*"));y(this,"emptyMap",new Map);this.evntsTree=new Map,this.lstnrsRegistry=new Map,this.compsRegistry=new Map}_getComponentFromUid(e){for(const t of this.compsRegistry.entries())if(t[0].uid===e)return t[0]}_callHandler(e,t,n,r){var d,u,p,_;const o=this._rebuildEventNameFromSymbol(n),i=((u=(d=t.handlerCallerComponentInstance)==null?void 0:d.type)==null?void 0:u.__name)||((_=(p=t.handlerCallerComponentInstance)==null?void 0:p.type)==null?void 0:_.name);t.once=()=>{this.off(t.handlerCallerComponentInstance.uid,o||"-",e)};let a;try{a=e(t)}catch(h){return console.error(`Error in handler function: ${e==null?void 0:e.name} - event: ${o} - handler caller: ${i}`),console.error(h),h}if(Object.is(t.propagationStopped,null)&&(r!=null&&r.stopPropagation))try{t.propagationStopped=typeof r.stopPropagation=="function"?r.stopPropagation(t):r.stopPropagation}catch(h){console.error(`Error in stopPropagation option for handler function: ${e==null?void 0:e.name} - event: ${o} - handler caller: ${i}`),console.error(h)}if(r!=null&&r.once)try{(typeof r.once=="function"?r.once(t):r.once)&&this.off(t.handlerCallerComponentInstance.uid,o||"-",e)}catch(h){console.error(`Error in once option for handler function: ${e==null?void 0:e.name} - event: ${o} - handler caller: ${i}`),console.error(h)}return a}async _callHandlers(e,t){const n=[];e.branchSymbols.forEach(r=>{for(const o of(this.lstnrsRegistry.get(r)||this.emptyMap).entries()){const i=t?t.uid===o[0].uid?t:void 0:o[0];if(i){e.handlerCallerComponentInstance=i;for(const a of o[1].entries()){let d=this._callHandler(a[0],e,r,a[1]);!e.eager&&d instanceof Promise&&n.push(d)}}}}),await Promise.allSettled(n)}async _internalEmitEvent(e,t){e.handlerCallerComponentInstance=t||e.emitterComponentInstance,this.compsRegistry.has(e.handlerCallerComponentInstance)&&await this._callHandlers(e,e.handlerCallerComponentInstance),!e.propagationStopped&&e.handlerCallerComponentInstance.parent&&await this._internalEmitEvent(e,e.handlerCallerComponentInstance.parent)}_getBranchSymbols(e){const t=e.split(":"),n=[this.listenAllSymbol];let r;for(;t.length>0;){r=r?r+`:${t.splice(0,1)[0]}`:`${t.splice(0,1)[0]}`;const o=this._getEventEntry(r.split(":"));if(o)n.unshift(o.symbol);else break}return n}emitEvent(e,t,n,r){const o=this,i={emitterComponentInstance:e,handlerCallerComponentInstance:e,eventName:t,branchSymbols:this._getBranchSymbols(t),eventData:n,global:(r==null?void 0:r.global)||!1,propagationStopped:null,eager:Object.is(r==null?void 0:r.eager,void 0)||Object.is(r==null?void 0:r.eager,null)?!0:!!(r!=null&&r.eager)||!1,stopPropagation:function(){if(!this.global){this.propagationStopped=!this.global;return}console.warn(`${this.eventName} - can not stop the propagation of an event emitted globally.`)},once:()=>{},transformEvent:function(a,d){if(this.global){console.warn(`Cannot transform event ${this.eventName} into ${a} as ${this.eventName} has been emitted globally.`);return}try{o._checkEventNameValidity(a),this.eventName=a,this.eventData=d||this.eventData,this.branchSymbols=o._getBranchSymbols(a)}catch(u){console.error(`Error trying to transform event ${this.eventName} into ${a}`),console.error(u)}},emitEvent:function(a,d,u,p){return o.emitEvent(this.handlerCallerComponentInstance,a,d,{global:u,eager:p})}};return i.transformEvent=i.transformEvent.bind(i),r!=null&&r.global?this._callHandlers(i):this._internalEmitEvent(i)}_addEventEntry(e,t){return t&&e!=="*"?t.children.set(e,{symbol:Symbol(e),parent:t,children:new Map}).get(e):this.evntsTree.set(e,{symbol:e==="*"?this.listenAllSymbol:Symbol(e),parent:t,children:new Map}).get(e)}_checkEventEntryValidity(e){const t=!!this.lstnrsRegistry.get(e.symbol);if(t||e.children.size===0)return t;for(const n of e.children.entries())if(this._checkEventEntryValidity(n[1]))return!0;return t}_removeEventEntry(e){let t=e.parent;if(!t){this.evntsTree.delete(e.symbol.description);return}for(e.parent=void 0,t.children.delete(e.symbol.description);t&&!this._checkEventEntryValidity(t);){const n=t.parent;n?(t.parent=void 0,n.children.delete(t.symbol.description)):this.evntsTree.delete(t.symbol.description),t=n}}_rebuildEventNameFromSymbol(e){const t=this._findEventEntryFromNodeName(e.description);if(t)return this._rebuildEventNameFromEventEntry(t)}_rebuildEventNameFromEventEntry(e){let t=e.symbol.description;for(;e.parent;)t=`${e.parent.symbol.description}:`+t,e=e.parent;return t}_findEventEntryFromNodeName(e,t){if(t){for(const n of t.children.entries()){if(n[0]===e)return n[1];const r=this._findEventEntryFromNodeName(e,n[1]);if(r)return r}return}for(const n of this.evntsTree.entries()){if(n[0]===e)return n[1];const r=this._findEventEntryFromNodeName(e,n[1]);if(r)return r}}_getEventEntry(e,t,n){const r=e.splice(0,1)[0];let o=t?t.children.get(r):this.evntsTree.get(r);if(!o){if(!n)return o;o=this._addEventEntry(r,t)}return e.length===0?o:this._getEventEntry(e,o,n)}_checkEventNameValidity(e){var r,o,i,a,d,u;const n=/(?.*\*.+)|(?:{2,})|(?:\s+:)|(?^.*:$)|(?^:.*$)|(?^[a-zA-Z:]*\s+[a-zA-Z:]*$)/gi.exec(e);if((r=n==null?void 0:n.groups)!=null&&r.listenallerror)throw new Error(`${e} - Invalid event name. Listen All event listeners must be registered with a lone '*'.`);if((o=n==null?void 0:n.groups)!=null&&o.consequentcolons)throw new Error(`${e} - Invalid event name. The name cannot have consequent colons.`);if((i=n==null?void 0:n.groups)!=null&&i.finalcolon)throw new Error(`${e} - Invalid event name. The name cannot end with a colon.`);if((a=n==null?void 0:n.groups)!=null&&a.startingcolon)throw new Error(`${e} - Invalid event name. The name cannot start with a colon.`);if((d=n==null?void 0:n.groups)!=null&&d.spacescolons)throw new Error(`${e} - Invalid event name. The name cannot have two colons separated by spaces.`);if((u=n==null?void 0:n.groups)!=null&&u.spacesfound)throw new Error(`${e} - Invalid event name. The name cannot have spaces inside.`)}_addListener(e,t,n,r){const o=this._getEventEntry(t.split(":"),void 0,!0);o&&(this._addListenerEntry(e,o.symbol,n,r),this._addCompEntrySymbol(e,o.symbol))}_addListenerEntry(e,t,n,r){const o=this.lstnrsRegistry.get(t);if(!o)this.lstnrsRegistry.set(t,new Map([[e,new Map([[n,r]])]]));else{const i=o.get(e);if(i){i.has(n)||i.set(n,r);return}o.set(e,new Map([[n,r]]))}}_addCompEntrySymbol(e,t){var n;if(this.compsRegistry.has(e)){(n=this.compsRegistry.get(e))==null||n.add(t);return}this.compsRegistry.set(e,new Set([t]))}_removeCompEntry(e){this.compsRegistry.delete(e)}_removeCompEntrySymbol(e,t){const n=this.compsRegistry.get(e);n&&(n.delete(t),n.size===0&&this._removeCompEntry(e))}_removeListenerEntry(e,t,n){const r=this.lstnrsRegistry.get(t);if(!r)return;const o=r.get(e);o&&(o.delete(n),o.size===0&&(r.delete(e),this._removeCompEntrySymbol(e,t)))}_removeListener(e,t,n){const r=this._getEventEntry(t.split(":"));if(r){this._removeListenerEntry(e,r.symbol,n);const o=this.lstnrsRegistry.get(r.symbol);o&&o.size===0&&this.lstnrsRegistry.delete(r.symbol),this._checkEventEntryValidity(r)||this._removeEventEntry(r)}}on(e,t,n,r){try{if(t=t.trim(),t.length===0){console.error("Event name can not be an empty string");return}let o=w(e);this._checkEventNameValidity(t),this._addListener(o,t,n,r),o.isUnmountedCallbacks.push(()=>{this.offAll(o)})}catch(o){console.error(o)}}off(e,t,n){const r=this._getComponentFromUid(e);r&&n?this._removeListener(r,t,n):r&&!n&&this.offAll(r)}offAll(e){const t=this.compsRegistry.get(e);t&&(t.forEach(n=>{const r=this.lstnrsRegistry.get(n);r&&r.has(e)&&r.delete(e)}),this.compsRegistry.delete(e))}}const m=function(s){return new s}(C);function E(s){s&&m.offAll(s)}function b(s,e){if(e)for(const t in s)if(!Array.isArray(s[t]))console.warn(`${e.type.__name||e.type.name} - eventKey value for ${t} must be an array`);else for(const n of s[t])m.off(e.uid,t,n.handler)}function v(s,e,t){if(E(t&&e?e:void 0),e)for(const n in s)if(Array.isArray(s[n]))for(const r of s[n])m.on(e,n,r.handler,r.options);else console.warn(`${e.type.__name||e.type.name} - eventKey value for ${n} must be an array`)}const k={beforeMount(s,e,t){var n;v(e.value,(n=e.instance)==null?void 0:n.$)},beforeUpdate(s,e,t,n){var r;v(e.value,e.arg==="update"&&((r=e.instance)==null?void 0:r.$),e.arg==="update")},beforeUnmount(s,e,t){var n;E((n=e.instance)==null?void 0:n.$)}},g=f;function I(s,e){const t=e||c.getCurrentInstance();if(t){if(s instanceof Array){const n={};for(const r of s)n[r]=function(o,i,a){return m.emitEvent(t,r,o,{global:i,eager:a})};return n}return function(n,r,o){return m.emitEvent(t,s,n,{global:r,eager:o})}}return console.warn("No current component instance. createBackboneEmitter has to be called inside a lifecycle hook or you have to pass a component instance to the function."),function(n,r,o){return console.warn("Default backbone emitter called. No event transmitted through the backbone."),Promise.resolve()}}function $(s){const e=s||c.getCurrentInstance();return e?function(t){b(t,e)}:t=>{const n=c.getCurrentInstance();if(n){b(t,n);return}console.warn(`Unable to retrieve component instance. Probably defineRemoveEventListeners has been called outside from a lifecycle hook, this means that at least this returned function must be called inside a lifecycle hook`)}}function S(s){const e=s||c.getCurrentInstance();return e?function(t,n){v(t,e,n)}:(t,n)=>{const r=c.getCurrentInstance();if(r){v(t,r,n);return}console.warn(`Unable to retrieve component instance. Probably defineAddEventListeners has been called outside from a lifecycle hook, this means that at least this returned function must be called inside a lifecycle hook`)}}function B(s,e){const t=e||c.getCurrentInstance();if(t){if(s){const n=new Map(s.map(r=>[r,function(o,i,a){return m.emitEvent(t,r,o,{global:i,eager:a})}]));return function(r,o,i,a){const d=n.get(r);return d?d(o,i,a):(console.warn(`Event ${r} not defined as backbone emitter.`),Promise.resolve())}}return function(n,r,o,i){return m.emitEvent(t,n,r,{global:o,eager:i})}}return console.warn("No current component instance. defineBackboneEmits has to be called inside a lifecycle hook."),(n,r,o,i)=>(console.warn(`Default backbone emitter called. Event ${n} NOT emitted through the backbone.`),Promise.resolve())}const R=k,N={install:s=>{if(!s){console.warn("No app parameter provided, returning...");return}s.config.globalProperties.$EventsBackBone=m,s.provide(g,I)}};function L(){return m}l.EventsBackBoneDirective=R,l.createEventsBackboneEmitter=g,l.default=N,l.defineAddEventListeners=S,l.defineBackboneEmits=B,l.defineRemoveEventListeners=$,l.useBackbone=L,Object.defineProperties(l,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});