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]=`8b2c707e-2d58-48da-ae4d-e335b7ddefb0`,e._sentryDebugIdIdentifier=`sentry-dbid-8b2c707e-2d58-48da-ae4d-e335b7ddefb0`)}catch{}import"./rolldown-runtime-eb2nym32.js";import{Bi as e,Mi as t,Ri as n,Yi as r,Zi as i,bm as a,cm as o,dm as s,fm as c,im as l,lm as u,om as d,pg as f,pm as p,vm as m,ym as h}from"./entry.client-dr7r6s9a.js";import{t as g}from"./workspace-leaf-files-mc5sghv7.js";import{t as _}from"./BasePlugin-nelnyp3q.js";var v=a();s(),o(),l(),n();var y=t(),b=(e,...t)=>({...e,request:e.request?[...e.request,...t]:t}),x=300*1e3,S=120*1e3,C=30*1e3,w=class{httpClient;requestFactory;baseUrl=`https://userpresence.xboxlive.com`;logger=new f(`HeartbeatUserPresenceService`);activeTimer;isUserSignedIn;constructor(e,t){let n=new d(t.cvProvider);this.httpClient=new p().interceptors(t.interceptors?b(t.interceptors,n):{request:[n]}).authenticator(new u(e)).build(),this.requestFactory=c.withBaseUrl(this.baseUrl).setCommonTimeout(4e3).setCommonHeaders({[m.ContentType]:`application/json`,[m.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 h){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;heartbeatUserPresenceService;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 t=this;yield r([e(y.CommonActionType.UserSignedIn,function*(e){yield i([t.heartbeatUserPresenceService,t.heartbeatUserPresenceService.userSignedIn],e.user.xuid)}),e(y.CommonActionType.UserSignedOut,function*(e){yield i([t.heartbeatUserPresenceService,t.heartbeatUserPresenceService.userSignedOut])})])}};export{T as UserPresencePlugin};