/*!
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
@license Proprietary

This software is the confidential and proprietary property of Lenovo SSG BASD.
You shall not disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement.

--- Build Info ---
Version: 1.1.1
Build Time: 2026/2/26 17:21:00
Build Commit: 81e556ed827e2f043668506dae6fae8dbb9ef395
*/
class t{#t=!0;#e;#n;#i;#s;#o;#a;#d;#r=0;#c=5;#g=!1;#h=!1;#v=null;#l=()=>{this.debugLog("Connected to event bus"),this.#o=!0,this.#g=!1,this.debugLog("Emitting queued events",this.#s),this.#s.forEach(t=>this.emitEventToBus(t)),this.#s=[],this.stopConnectLoop(),this.#n().removeEventListener("tanstack-connect-success",this.#l)};#u=()=>{if(this.#r<this.#c)return this.#r++,void this.dispatchCustomEvent("tanstack-connect",{});this.#n().removeEventListener("tanstack-connect",this.#u),this.#h=!0,this.debugLog("Max retries reached, giving up on connection"),this.stopConnectLoop()};#E=()=>{this.#g||(this.#g=!0,this.#n().addEventListener("tanstack-connect-success",this.#l),this.#u())};constructor({pluginId:t,debug:e=!1,enabled:n=!0,reconnectEveryMs:i=300}){this.#e=t,this.#t=n,this.#n=this.getGlobalTarget,this.#i=e,this.debugLog(" Initializing event subscription for plugin",this.#e),this.#s=[],this.#o=!1,this.#h=!1,this.#a=null,this.#d=i}startConnectLoop(){null!==this.#a||this.#o||(this.debugLog(`Starting connect loop (every ${this.#d}ms)`),this.#a=setInterval(this.#u,this.#d))}stopConnectLoop(){this.#g=!1,null!==this.#a&&(clearInterval(this.#a),this.#a=null,this.#s=[],this.debugLog("Stopped connect loop"))}debugLog(...t){this.#i&&console.log(`🌴 [tanstack-devtools:${this.#e}-plugin]`,...t)}getGlobalTarget(){if("undefined"!=typeof globalThis&&globalThis.__TANSTACK_EVENT_TARGET__)return this.debugLog("Using global event target"),globalThis.__TANSTACK_EVENT_TARGET__;if("undefined"!=typeof window&&void 0!==window.addEventListener)return this.debugLog("Using window as event target"),window;const t="undefined"!=typeof EventTarget?new EventTarget:void 0;return void 0===t||void 0===t.addEventListener?(this.debugLog("No event mechanism available, running in non-web environment"),{addEventListener:()=>{},removeEventListener:()=>{},dispatchEvent:()=>!1}):(this.debugLog("Using new EventTarget as fallback"),t)}getPluginId(){return this.#e}dispatchCustomEventShim(t,e){try{const n=new Event(t,{detail:e});this.#n().dispatchEvent(n)}catch(n){this.debugLog("Failed to dispatch shim event")}}dispatchCustomEvent(t,e){try{this.#n().dispatchEvent(new CustomEvent(t,{detail:e}))}catch(n){this.dispatchCustomEventShim(t,e)}}emitEventToBus(t){this.debugLog("Emitting event to client bus",t),this.dispatchCustomEvent("tanstack-dispatch-event",t)}createEventPayload(t,e){return{type:`${this.#e}:${t}`,payload:e,pluginId:this.#e}}emit(t,e){if(this.#t){if(this.#v&&(this.debugLog("Emitting event to internal event target",t,e),this.#v.dispatchEvent(new CustomEvent(`${this.#e}:${t}`,{detail:this.createEventPayload(t,e)}))),!this.#h)return this.#o?this.emitEventToBus(this.createEventPayload(t,e)):(this.debugLog("Bus not available, will be pushed as soon as connected"),this.#s.push(this.createEventPayload(t,e)),void("undefined"==typeof CustomEvent||this.#g||(this.#E(),this.startConnectLoop())));this.debugLog("Previously failed to connect, not emitting to bus")}else this.debugLog("Event bus client is disabled, not emitting event",t,e)}on(t,e,n){const i=n?.withEventTarget??!1,s=`${this.#e}:${t}`;if(i&&(this.#v||(this.#v=new EventTarget),this.#v.addEventListener(s,t=>{e(t.detail)})),!this.#t)return this.debugLog("Event bus client is disabled, not registering event",s),()=>{};const o=t=>{this.debugLog("Received event from bus",t.detail),e(t.detail)};return this.#n().addEventListener(s,o),this.debugLog("Registered event to bus",s),()=>{i&&this.#v?.removeEventListener(s,o),this.#n().removeEventListener(s,o)}}onAll(t){if(!this.#t)return this.debugLog("Event bus client is disabled, not registering event"),()=>{};const e=e=>{const n=e.detail;t(n)};return this.#n().addEventListener("tanstack-devtools-global",e),()=>this.#n().removeEventListener("tanstack-devtools-global",e)}onAllPluginEvents(t){if(!this.#t)return this.debugLog("Event bus client is disabled, not registering event"),()=>{};const e=e=>{const n=e.detail;this.#e&&n.pluginId!==this.#e||t(n)};return this.#n().addEventListener("tanstack-devtools-global",e),()=>this.#n().removeEventListener("tanstack-devtools-global",e)}}export{t as EventClient};