1import{f as C,h as d,i as k,k as y}from"./chunk-NTPMZIRS.js";import{B as f,Dh as v,Eh as g,H as b,U as c,_ as p,ba as u,d as m,di as P,lh as n,y as S}from"./chunk-SMW7PHYX.js";import{a as s,b as i}from"./chunk-BMIYOSOG.js";var I=v,w=7200*1e3,D=250,K=(()=>{let o=class o{constructor(e,t,r){this.cartService=e,this.checkoutChainService=t,this.authorizeService=r,this.unresumableChain=null,this.currentUserSub=null,this.restoredPlayerID=null,this.didRestore=!1,this.unsubscribe$=new m,this.flushOnPageHide=()=>this.save(),this.flushOnHidden=()=>{document.visibilityState==="hidden"&&this.save()},!this.isNestedBrowsingContext()&&(this.currentUserSub=this.authorizeService.getStoredUserSub(),this.restore(),this.watchSignedInUser(),S(this.cartService.cartItems$,this.checkoutChainService.checkoutChain$,this.checkoutChainService.completedChainCheckouts$).pipe(f(D),c(this.unsubscribe$)).subscribe({next:()=>this.save(),error:a=>n("[checkout-state] state stream failed; snapshot no longer updating",a)}),window.addEventListener("pagehide",this.flushOnPageHide),document.addEventListener("visibilitychange",this.flushOnHidden))}ngOnDestroy(){window.removeEventListener("pagehide",this.flushOnPageHide),document.removeEventListener("visibilitychange",this.flushOnHidden),this.unsubscribe$.next(),this.unsubscribe$.complete()}consumeUnresumableChain(e){let t=this.unresumableChain;if(this.unresumableChain=null,!t)return null;let r=e>0?t.programs.filter(a=>a.programID!==e):t.programs;return t.programsKnown&&!r.length?null:{programNames:r.map(a=>a.programName).filter(a=>!!a)}}clear(){this.discardLiveCheckoutState(),this.removeSnapshot()}save(){let e=this.checkoutChainService.getPersistableState();if(!e){this.removeSnapshot();return}if(!this.currentUserSub)return;let t=this.cartService.getPersistableState(),r=this.resolvePlayerID(e.chain.links);if(!(r>0)){this.removeSnapshot();return}let a={version:d,savedAt:Date.now(),playerID:r,userSub:this.currentUserSub,cart:i(s({},t),{selectedPlayer:this.toPersistablePlayer(t.selectedPlayer),cartItems:(t.cartItems||[]).map(h=>this.toPersistableCartItem(h))}),chain:i(s({},e),{chain:i(s({},e.chain),{links:e.chain.links.map(h=>i(s({},h),{reservePlan:this.toPersistablePlan(h.reservePlan),checkoutPlan:this.toPersistablePlan(h.checkoutPlan)}))})})};try{sessionStorage.setItem(I,JSON.stringify(a))}catch(h){n("[checkout-state] failed to persist snapshot",h)}}restore(){let e=this.read();if(!e)return;let t=this.parse(e);if(!this.isInvalidChain(t)){this.checkoutChainService.beginRestore();try{if(!this.checkoutChainService.restoreState(t.chain))throw new Error("snapshot carried no chain links");this.cartService.restoreState(t.cart),this.restoredPlayerID=t.playerID,this.didRestore=!0}catch(r){n("[checkout-state] failed to restore snapshot",r),this.discardLiveCheckoutState(),this.markUnresumable([],!1,t.playerID)}finally{this.checkoutChainService.endRestore()}}}isInvalidChain(e){if(!e)return this.markUnresumable([],!1),!0;if(!e.userSub||e.userSub!==this.currentUserSub)return this.removeSnapshot(),!0;let t=this.listPendingPrograms(e);return e.version!==d||this.isExpired(e)||!(e.playerID>0)?(t.length?this.markUnresumable(t,!0,e.playerID):this.removeSnapshot(),!0):e.chain?.chain?.links?.length&&!t.length?(this.removeSnapshot(),!0):!1}markUnresumable(e,t,r=0){this.unresumableChain={programs:e,programsKnown:t,playerID:r},this.removeSnapshot()}watchSignedInUser(){this.authorizeService.getCurrentUserProfile().pipe(b(1),c(this.unsubscribe$)).subscribe({next:e=>this.verifyRestoredOwner(e?.sub??null),error:e=>{n("[checkout-state] could not resolve the signed-in user; discarding snapshot",e),this.verifyRestoredOwner(null)}}),this.authorizeService.getUser().pipe(c(this.unsubscribe$)).subscribe({next:e=>this.onSignedInUserChanged(e?.sub??null),error:e=>n("[checkout-state] user stream failed; snapshot owner may be stale",e)})}onSignedInUserChanged(e){let t=this.currentUserSub;this.currentUserSub=e,t&&t!==e&&this.clear()}verifyRestoredPlayer(e){if(!(e>0))return;let t=this.restoredPlayerID;if(t!==null){this.restoredPlayerID=null,e!==t&&(n("[checkout-state] restored chain belongs to another player; discarding"),this.discardRestoredState());return}let r=this.unresumableChain?.playerID??0;r>0&&r!==e&&(n("[checkout-state] unresumable chain belongs to another player;
1 dropping its warning"),this.unresumableChain=null)}verifyRestoredOwner(e){let t=this.currentUserSub;if(this.currentUserSub=e,!(e&&e===t)){if(this.didRestore){n("[checkout-state] restored chain failed owner re-check; discarding"),this.discardRestoredState();return}this.unresumableChain&&(n("[checkout-state] unresumable chain failed owner re-check; dropping its warning"),this.unresumableChain=null)}}discardRestoredState(){this.didRestore=!1,this.restoredPlayerID=null,this.unresumableChain=null,this.discardLiveCheckoutState(),this.removeSnapshot()}discardLiveCheckoutState(){this.checkoutChainService.cancelCheckoutChain(),this.cartService.clearAllCarts(),this.cartService.selectedPlayer=null}removeSnapshot(){g()}isNestedBrowsingContext(){return window.self!==window.top}read(){try{return sessionStorage.getItem(I)}catch(e){return null}}parse(e){try{return JSON.parse(e)}catch(t){return null}}isExpired(e){return!(e.savedAt>0)||Date.now()-e.savedAt>w}listPendingPrograms(e){let t=e?.chain?.chain?.links;return(Array.isArray(t)?t:[]).filter(r=>r.status!==C.COMPLETED).map(r=>({programID:r.programID,programName:r.programName}))}resolvePlayerID(e){for(let t of e){let r=(t.checkoutPlan??t.reservePlan)?.playerID;if(r>0)return r}return this.cartService.selectedPlayer?.playerID??0}toPersistableCartItem(e){return e&&s(i(s({},e),{player:this.toPersistablePlayer(e.player)}),e.timeSlot?{timeSlot:this.toPersistableTimeSlot(e.timeSlot)}:{})}toPersistablePlayer(e){return e&&{playerID:e.playerID,user:{firstName:e.user?.firstName,lastName:e.user?.lastName}}}toPersistablePlan(e){return e&&i(s({},e),{checkoutTimeSlots:(e.checkoutTimeSlots||[]).map(t=>this.toPersistableTimeSlot(t)),assignedSlots:(e.assignedSlots||[]).map(t=>i(s({},t),{timeSlot:this.toPersistableTimeSlot(t.timeSlot)})),pendingReservations:(e.pendingReservations||[]).map(t=>i(s({},t),{timeSlot:this.toPersistableTimeSlot(t.timeSlot)}))})}toPersistableTimeSlot(e){if(!e)return e;let t=i(s({},e),{menuItems:void 0});return e.staffAssignments?.length&&(t.staffAssignments=e.staffAssignments.map(r=>i(s({},r),{menuItems:void 0}))),t}};o.\u0275fac=function(t){return new(t||o)(u(k),u(y),u(P))},o.\u0275prov=p({token:o,factory:o.\u0275fac,providedIn:"root"});let l=o;return l})();export{K as a};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.