try{let e=typeof window<`u`?window:typeof global<`u`?global:typeof globalThis<`u`?globalThis:typeof self<`u`?self:{},t=new e.Error().stack;t&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[t]=`dc5d5736-c8f9-4ab1-a2a6-f31153a493c8`,e._sentryDebugIdIdentifier=`sentry-dbid-dc5d5736-c8f9-4ab1-a2a6-f31153a493c8`)}catch{}import"./rolldown-runtime-nx40z1zs.js";import{Ap as e,Bp as t,Dp as n,Fs as r,Js as i,Lp as a,Np as o,Pp as s,Qp as c,Rp as l,Rs as u,Xp as d,Xs as f,em as p,ks as m,yg as h}from"./entry.client-mpghtlrl.js";import{t as g}from"./workspace-leaf-files-oergw4h0.js";import{a as _}from"./redux-boot-core-pd6f4ovn.js";var v=p();a(),o(),n(),r();var y=m(),b=(e,...t)=>({...e,request:e.request?[...e.request,...t]:t}),x=300*1e3,S=120*1e3,C=30*1e3,w=class{baseUrl=`https://userpresence.xboxlive.com`;logger=new h(`HeartbeatUserPresenceService`);constructor(n,r){let i=new e(r.cvProvider);this.httpClient=new t().interceptors(r.interceptors?b(r.interceptors,i):{request:[i]}).authenticator(new s(n)).build(),this.requestFactory=l.withBaseUrl(this.baseUrl).setCommonTimeout(4e3).setCommonHeaders({[d.ContentType]:`application/json`,[d.XXBLContractVersion]:`1`})}async userSignedIn(e){this.userSignedOut(),this.isUserSignedIn=!0,await this.startHeartbeat(e)}userSignedOut(){this.isUserSignedIn=!1,this.activeTimer&&=(clearTimeout(this.activeTimer),void 0)}async startHeartbeat(e){if(this.isUserSignedIn)if(document.visibilityState===`visible`)try{let t=await this.setUserPresence(e);this.activeTimer=setTimeout(()=>this.startHeartbeat(e),t)}catch(e){let t=(0,v.toError)(e);this.logger.error(`We should have never gotten here`,t)}else{let t=n=>{document.visibilityState===`visible`&&(document.removeEventListener(`visibilitychange`,t),this.startHeartbeat(e))};document.addEventListener(`visibilitychange`,t)}}async setUserPresence(e){let t=`users/xuid(${e})/devices/current/titles/current`,n=this.requestFactory.post(t,JSON.stringify({placement:`full`,state:`active`,activity:{}}));try{let e=await this.httpClient.execute(n);this.logger.info(`User presence set successfully`);let t=e.headers.get(`X-Heartbeat-After`);if(t){let e=parseInt(t,10);return this.logger.debug(`X-Heartbeat-After set to ${e}`),Math.max(e*1e3,C)}else return this.logger.warn(`No X-Heartbeat-After header found`),x}catch(e){if(e instanceof c){let t=e.response.headers.get(`Retry-After`);if(t){let e=parseInt(t,10);return this.logger.debug(`Retrying in ${e} seconds`),Math.max(e*1e3,C)}else return this.logger.debug(`No retry value received, using default`),S}else{let t=(0,v.toError)(e);return this.logger.warn(`Unexpected error: ${t.message}, using default retry value`),S}}}},T=class extends _{hasConsumedSignInAction=!1;constructor(e,t,n,r,i){super(e,g,i),this.heartbeatUserPresenceService=new w(t,{cvProvider:n,interceptors:r})}destroy(){this.heartbeatUserPresenceService.userSignedOut()}*handleActions(){let e=this;yield i([u(y.CommonActionType.UserSignedIn,function*(t){yield f([e.heartbeatUserPresenceService,e.heartbeatUserPresenceService.userSignedIn],t.user.xuid)}),u(y.CommonActionType.UserSignedOut,function*(t){yield f([e.heartbeatUserPresenceService,e.heartbeatUserPresenceService.userSignedOut])})])}};export{T as UserPresencePlugin};