import{a as v,b as x}from"./chunk-RII6H6UF.js";import{J as r,O as S,Sa as j,T as l,X as f,be as I,d as u,g as c,na as A,r as a,ta as b,x as h,ya as g,z as m}from"./chunk-WL5U2YIT.js";import{e as C}from"./chunk-ADEKKGI2.js";function y(t){return new p(t)}var p,O=C(()=>{p=class{constructor(n){this.target=n}call(n){let d=this.target[n];return{withArgs:(...s)=>{if(typeof d=="function")return d.apply(this.target,s)}}}}});var q,w=C(()=>{I();A();j();x();O();I();q=(()=>{let n=class n{constructor(){this.activeClosures=new Set,this.remainingSessions=[],this.webClientSessionService=g(v),this.isClosingAllSessions=!1,this.sessionCloseRequestSubject=new c,this.sessionCloseRequest$=this.sessionCloseRequestSubject.asObservable(),this.sessionClosedSubject=new c,this.sessionClosed$=this.sessionClosedSubject.asObservable(),this.postClosureCompletedSubject=new c,this.postClosureCompleted$=this.postClosureCompletedSubject.asObservable(),this.sessionCloseRequest$.subscribe(s=>{this.sendTerminateCommandToWebSession(s)}),this.postClosureCompleted$.subscribe(()=>{this.currentSessionObserver&&this.processNextSession(this.currentSessionObserver)}),this.monitorWebSessionClosure()}closeSession(s){let e=this.getSessionClosureKey(s);return this.activeClosures.has(e)?this.postClosureCompleted$.pipe(r(i=>i.openSessionLogId===e),l(1),h(()=>{})):(this.remainingSessions=[s],this.isClosingAllSessions=!1,new u(i=>{this.currentSessionObserver=i,this.processNextSession(i)}))}closeAllSessions(s){return this.remainingSessions=[...s],this.isClosingAllSessions=!0,new u(e=>{this.currentSessionObserver=e,this.processNextSession(e)})}processNextSession(s){if(this.remainingSessions.length===0){this.cleanUpObserver(s);return}let e=this.remainingSessions.shift();if(!e){this.cleanUpObserver(s);return}let i=this.getSessionClosureKey(e);if(this.activeClosures.has(i)){this.processNextSession(s);return}if(this.activeClosures.add(i),this.isSessionAlreadyInIssueState(e)){this.processIssueStateSessionClosure(e);return}this.listenForConnectionClosed(e),this.sendTerminateCommandToWebSession(e)}notifySessionClosed(s,e){this.sessionClosedSubject.next({component:s,connectionClosedInfo:e})}sendTerminateCommandToWebSession(s){this.isWebSessionComponentRefValid(s)&&y(s.componentRef.instance).call("sendTerminateSessionCmd").withArgs(!0)}completeSessionClosure(s){let e=s.openSessionLogId;this.activeClosures.delete(e),this.webClientSessionService.removeSession(e).pipe(l(1),S(i=>(console.error("Error removing closed web session:",i),a(void 0))),f(()=>this.postClosureCompletedSubject.next(s))).subscribe()}cleanUpObserver(s){this.isClosingAllSessions&&(this.isClosingAllSessions=!1,this.activeClosures.clear()),s.next(),s.complete(),this.currentSessionObserver=void 0}listenForConnectionClosed(s){let e=s.componentRef?.instance,i=s.data.connectionId;e?.connectionClosedObserver&&e.connectionClosedObserver.pipe(r(o=>o!==null),r(o=>o.connectionId===i),l(1)).subscribe({next:o=>{if(e.hasIssueNotification){e.processOnErrorClosure(o);return}this.notifySessionClosed(e,o)},error:o=>{console.error("Error during connection close handling:",o)}})}isWebSessionComponentRefValid(s){return s&&s.componentRef}getSessionClosureKey(s){return s.sessionId}isSessionAlreadyInIssueState(s){return!!s.componentRef?.instance?.hasIssueNotification}processIssueStateSessionClosure(s){let e=s.componentRef?.instance;if(!e){this.completeSessionClosure({connectionId:s.data.connectionId,openSessionLogId:this.getSessionClosureKey(s)});return}e.processOnErrorClosure({connectionId:s.data.connectionId,openSessionLogId:this.getSessionClosureKey(s)})}isValidWebSessionSession(s,e){return s!==null&&s.data.connectionId===e.connectionId}fetchSession(s){return this.webClientSessionService.getWebSession(s).pipe(S(e=>(console.error("Error fetching session:",e),a(null))))}monitorWebSessionClosure(){this.sessionClosed$.pipe(m(({component:s,connectionClosedInfo:e})=>this.fetchSession(e.openSessionLogId).pipe(h(i=>({component:s,connectionClosedInfo:e,session:i})))),r(Boolean),r(({session:s,connectionClosedInfo:e})=>this.isValidWebSessionSession(s,e))).subscribe(({component:s,connectionClosedInfo:e,session:i})=>{s.handlePostClosureActions(i,e)})}};n.\u0275fac=function(e){return new(e||n)},n.\u0275prov=b({token:n,factory:n.\u0275fac,providedIn:"root"});let t=n;return t})()});export{y as a,O as b,q as c,w as d};
