1import{g as y}from"./info.d5329f52.js";import{i as l,L as w,q as p,t as A,h as T,bO as a,bP as d,aa as f,g as M,bQ as v,bR as b,P as D,w as m,l as R,j as x,F as I,U as k,M as g,a0 as O,a6 as C,D as F,bS as E,a2 as N}from"./index.3ec72d2e.js";import{s as P}from"./page-visibility.2f62fa7f.js";class S{constructor(e,t){if(!e.onEnd)throw new Error("onEnd handler is required");if(!t)throw new Error("ms duration is required");this.onEnd=e.onEnd,this.initialMs=t,this.startTimestamp=Date.now(),this.timer=this.create(this.onEnd,t)}create(e,t){return this.timer&&this.clear(),setTimeout(()=>e?e():this.onEnd(),t||this.initialMs)}clear(){clearTimeout(this.timer),this.timer=null}end(){this.clear(),this.onEnd()}isValid(){return this.initialMs-(Date.now()-this.startTimestamp)>0}}class _ extends S{constructor(e,t){super(e,t),this.onPause=typeof e.onPause=="function"?e.onPause:()=>{},this.onRefresh=typeof e.onRefresh=="function"?e.onRefresh:()=>{},this.onResume=typeof e.onResume=="function"?e.onResume:()=>{},this.readStorage=e.readStorage,this.remainingMs=void 0,e.refreshEvents||(e.refreshEvents=["click","keydown","scroll"]);try{this.abortController=new AbortController}catch{}if(l&&e.ee){if(e.ee){this.ee=e.ee;const s=w(this.refresh.bind(this),500,{leading:!0});this.refreshHandler=r=>{e.refreshEvents.includes(r?.[0]?.type)&&s()},e.ee.on("fn-end",this.refreshHandler)}P(s=>
1{s==="hidden"?this.pause():this.resume()},!1,!1,this.abortController?.signal)}}abort(){this.clear(),this.abortController?.abort(),this.refreshHandler&&(this.ee.removeEventListener("fn-end",this.refreshHandler),this.refreshHandler=this.ee=null)}pause(){this.onPause(),clearTimeout(this.timer),this.remainingMs=this.initialMs-(Date.now()-this.startTimestamp)}resume(){try{const t=this.readStorage(),s=typeof t=="string"?JSON.parse(t):t;e(s.expiresAt)||e(s.inactiveAt)?this.end():(this.refresh(),this.onResume())}catch{this.end()}function e(t){return Date.now()>t}}refresh(e,t){this.clear(),this.timer=this.create(e,t),this.startTimestamp=Date.now(),this.remainingMs=void 0,this.onRefresh()}}const o={value:"",inactiveAt:0,expiresAt:0,updatedAt:Date.now(),sessionReplayMode:g.OFF,sessionReplaySentFirstChunk:!1,sessionTraceMode:g.OFF,traceHarvestStarted:!1,serverTimeDiff:null,custom:{}};class j{constructor(e){const{agentIdentifier:t,key:s,storage:r}=e;if(!t||!s||!r)throw new Error("Missing required field(s):".concat(t?"":" agentID").concat(s?"":" key").concat(r?"":" storage"));this.agentIdentifier=t,this.storage=r,this.state={},this.key=s,this.ee=p.get(t),A(this.ee),this.setup(e),l&&T("storage",i=>{if(i.key===this.lookupKey){const h=typeof i.newValue=="string"?JSON.parse(i.newValue):i.newValue;this.sync(h),this.ee.emit(a.UPDATE,[d.CROSS_TAB,this.state])}})}setup({value:e=M(16),expiresMs:t=v,inactiveMs:s=b}){const r={serverTimeDiff:this.state.serverTimeDiff||o.serverTimeDiff};this.state={},this.sync({...o,...r}),this.state.value=e,this.expiresMs=t,this.inactiveMs=s;const i=this.read();t?(this.state.expiresAt=i?.expiresAt||this.getFutureTimestamp(t),this.expiresTimer=new S({onEnd:()=>{this.collectSM("expired"),this.collectSM("duration"),this.reset()}},this.state.expiresAt-Date.now())):this.state.expiresAt=1/0,s?(this.state.inactiveAt=i?.inactiveAt||this.getFutureTimestamp(s),this.inactiveTimer=new _({onEnd:()=>{this.collectSM("inactive"),this.collectSM("duration"),this.reset()},onRefresh:this.refresh.bind(this),onResume:()=>{this.ee.emit(a.RESUME)},onPause:()=>{this.initialized&&this.ee.emit(a.PAUSE),this.write(f(this.state,o))},ee:this.ee,refreshEvents:["click","keydown","scroll"],readStorage:()=>this.storage.get(this.lookupKey)},this.state.inactiveAt-Date.now())):this.state.inactiveAt=1/0,this.isNew||=!Object.keys(i).length,this.isNew?this.write(f(this.state,o),!0):this.sync(i),this.initialized=!0,this.ee.emit(a.STARTED,[this.isNew])}get lookupKey(){return"".concat(D,"_").concat(this.key)}sync(e){Object.assign(this.state,e)}read(){try{const e=this.storage.get(this.lookupKey);if(!e)return{};const t=typeof e=="string"?JSON.parse(e):e;return this.isInvalid(t)?{}:this.isExpired(t.expiresAt)?(this.collectSM("expired"),this.collectSM("duration",t,!0),this.reset()):this.isExpired(t.inactiveAt)?(this.collectSM("inactive"),this.collectSM("duration",t,!0),this.reset()):t}catch(e){return m(10,e),{}}}write(e){try{return!e||typeof e!="object"?void 0:(e.updatedAt=Date.now(),this.sync(e),this.storage.set(this.lookupKey,R(this.state)),this.ee.emit(a.UPDATE,[d.SAME_TAB,this.state]),e)}catch(t){return m(11,t),null}}reset(){try{return this.initialized&&this.ee.emit(a.RESET),this.storage.remove(this.lookupKey),this.inactiveTimer?.abort?.(),this.expiresTimer?.clear?.(),delete this.isNew,this.setup({agentIdentifier:this.agentIdentifier,key:this.key,storage:this.storage,expiresMs:this.expiresMs,inactiveMs:this.inactiveMs}),this.read()}catch{return{}}}refresh(){const e=this.read();this.write({...e,inactiveAt:this.getFutureTimestamp(this.inactiveMs)})}isExpired(e){return Date.now()>e}isInvalid(e){return!Object.keys(o).every(s=>Object.keys(e).includes(s))}collectSM(e,t,s){let r,i;e==="duration"&&(r=this.getDuration(t,s),i="Session/Duration/Ms"),e==="expired"&&(i="Session/Expired/Seen"),e==="inactive"&&(i="Session/Inactive/Seen"),i&&x(k,[i,r],void 0,I.metrics,this.ee)}getDuration(e=this.state,t){const s=e.expiresAt-this.expiresMs;return(t?Date.now():e.updatedAt)-s}getFutureTimestamp(e){return Date.now()+e}syncCustomAttribute(e,t){if(!!l)if(t===null){const s=this.read();s.custom&&(delete s.custom[e],this.write({...s}))}else{const s=this.read();this.custom={...s?.custom||{},[e]:t},this.write({...s,custom:this.custom})}}}class U{get(e){try{return localStorage.getItem(e)||void 0}catch{return""}}set(e,t){try{return t==null?this.remove(e):localStorage.setItem(e,t)}catch{}}remove(e){try{localStorage.removeItem(e)}catch{}}}let K=0;function q(n){const e=O(n);if(K++)return e.session;const t=C(n).session;e.session=new j({agentIdentifier:n,key:F,storage:new U,expiresMs:t?.expiresMs,inactiveMs:t?.inactiveMs});const s=e.session.state.custom,r=y(n);s&&(r.jsAttributes={...r.jsAttributes,...s});const i=p.get(n);return E("api-setCustomAttribute",(h,c,u)=>{e.session.syncCustomAttribute(c,u)},"session",i),E("api-setUserId",(h,c,u)=>{e.session.syncCustomAttribute(c,u)},"session",i),N(n,"session"),e.session}export{q as setupAgentSession};
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.