"use strict";(("undefined"!=typeof self?self:this).wpChessCom_NYsV=("undefined"!=typeof self?self:this).wpChessCom_NYsV||[]).push([[2213],{12213:function(e,t,i){i.r(t);i.d(t,{UserActivityClient:function(){return o},createUserActivityClient:function(){return createUserActivityClient}});var r=i(42302);function getISOStringWithLocalTz(e){let t=e.getTimezoneOffset(),i=Math.abs(t),r=new Date(e.getTime()-6e4*t).toISOString();return`${r.slice(0,-1)}${t>0?"-":"+"}${String(Math.floor(i/60)).padStart(2,"0")}:${String(i%60).padStart(2,"0")}`}function fetchAndStoreDeviceId(){let e=window.localStorage.getItem("user-activity-sdk-device-id");if(e)return e;{let e=(0,r.A)();return window.localStorage.setItem("user-activity-sdk-device-id",e),e}}var s=i(72505);let bigIntRequestTransformRequest$2=e=>{if("bigint"==typeof e)return e.toString();if("object"!=typeof e)return e;if(Array.isArray(e))return e.map(bigIntRequestTransformRequest$2);for(let t in e)e[t]=bigIntRequestTransformRequest$2(e[t]);return e},bigIntRequestTransformRequest$1=e=>{if("bigint"==typeof e)return e.toString();if("object"!=typeof e)return e;if(Array.isArray(e))return e.map(bigIntRequestTransformRequest$1);for(let t in e)e[t]=bigIntRequestTransformRequest$1(e[t]);return e};function DispatchEventBatch(e,t,i,r,n){let a=r??window.PubSub?.options?.url?.base??"";return s.post(`${a}${i??"/service/user-activity"}/chesscom.user_activity.v1.UserActivityHttpBridgeService/DispatchEventBatch`,e,{headers:t,params:platformServicesParams(),withCredentials:!0,signal:n,transformRequest:[bigIntRequestTransformRequest,s.defaults.transformRequest??[]].flat()})}let platformServicesParams=()=>window.context?.environment==="dev"?{uid:window.context?.user?.uuid}:void 0,bigIntRequestTransformRequest=e=>{if("bigint"==typeof e)return e.toString();if("object"!=typeof e)return e;if(Array.isArray(e))return e.map(bigIntRequestTransformRequest);for(let t in e)e[t]=bigIntRequestTransformRequest(e[t]);return e};function deserialize(e){return JSON.parse(e,(e,t)=>t&&"BigInt"===t.type?BigInt(t.value):t)}function serialize(e){return JSON.stringify(e,(e,t)=>"bigint"==typeof t?{type:"BigInt",value:t.toString()}:t)}class n{constructor(e,t,i="user-activity-js-sdk-batch"){this.batchOptions={batchSize:10,enabled:!0,waitFor:2e3};this.currentBatch={identify:[],track:[],navigation:[],typed:[]};this.apiKey=e;this.cacheKey=i;this.requestConfig=t;this.flushCached()}flush(){this.currentTimer&&clearTimeout(this.currentTimer);this.dispatchBatchEvents()}flushCached(){let e=localStorage.getItem(this.cacheKey);if(e)try{let t=deserialize(e);Object.entries(t).forEach(([e,t])=>{e in this.currentBatch&&this.currentBatch[e].push(...t)});this.flush()}catch{console.error("An error occured while restoring cached user activity events")}localStorage.removeItem(this.cacheKey)}shouldDispatchEventNow(){let e=this.numberOfEventsInBatch>=this.batchOptions.batchSize;return!this.batchOptions.enabled||e||this.batchOptions.waitFor<=0}get numberOfEventsInBatch(){return this.currentBatch.identify.length+this.currentBatch.track.length+this.currentBatch.navigation.length}registerEvent(e,t){switch(e){case"identify":this.currentBatch.identify.push(t);break;case"navigation":this.currentBatch.navigation.push(t);break;case"track":this.currentBatch.track.push(t);break;case"typed":this.currentBatch.typed.push(t);break;default:throw Error("Invalid event type")}if(this.shouldDispatchEventNow()){this.flush();return}!this.currentTimer&&this.batchOptions.waitFor>0&&(this.currentTimer=setTimeout(()=>this.dispatchBatchEvents(),this.batchOptions.waitFor))}dispatchBatchEvents(){DispatchEventBatch({identifyEvents:this.currentBatch.identify,navigationEvents:this.currentBatch.navigation,trackEvents:this.currentBatch.track,typedEvents:this.currentBatch.typed,sentAt:new Date().toISOString()},{"x-api-key":this.apiKey,...this.requestConfig?.headers??{}},this.requestConfig?.prefix,this.requestConfig?.domain);this.resetCurrentBatch()}resetCurrentBatch(){this.currentBatch={identify:[],navigation:[],track:[],typed:[]};this.currentTimer=void 0}cache(){let{identify:e,navigation:t,track:i,typed:r}=this.currentBatch,s=[e,t,i,r].flat().length>0;if(s)try{let e=serialize(this.currentBatch);localStorage.setItem(this.cacheKey,e)}catch{console.error("An error occured while caching user activity events")}}setBatchOptions(e){this.batchOptions={...this.batchOptions,...e}}getBatchOptions(){return this.batchOptions}setRequestConfig(e){this.requestConfig=e}getRequestConfig(){return this.requestConfig}setApiKey(e){this.apiKey=e}getApiKey(){return this.apiKey}}function setCookie(e,t,i){let r="";if(i){let e=new Date;e.setTime(e.getTime()+6e4*i);let t=e.toUTCString();r=`; expires=${t}`}document.cookie=`${e}=${t||""}${r}; path=/; samesite=none; secure`}function getCookie(e){let t={};return document.cookie.split(";").forEach(e=>{let[i,r]=e.split("="),s=i.trim();s&&r&&(t[s]=r)}),t[e]||null}function getOrCreateProductSessionId(){let e=getCookie("psid");return null==e&&(e=(0,r.A)()),setCookie("psid",e,30),e}var a=i(77232);function getLanguage(){return navigator?.languages&&navigator?.languages[0]||navigator?.language||navigator?.userLanguage||""}function getScreenResolution(){if(screen.width&&screen.height)return{width:screen.width,height:screen.height,dpi:0}}class c{getLibrary(){return{name:"CHESSCOM_JS_SDK",version:"2.5.5"}}convertDeviceType(e){switch(e){case"mobile":return"MOBILE";case"tablet":return"TABLET";case"smarttv":return"TV";case"console":case"wearable":case"embedded":return"DEVICE_TYPE_UNSPECIFIED";default:return"DESKTOP"}}getAttributions(){let e=getCookie("ATTRIBUTION_V1");if(!e)return null;try{return JSON.parse(decodeURIComponent(e))}catch{return null}}getAttributionAndPsid(){let e=window.localStorage.getItem("attribution_and_psid");if(!e)return["",""];try{return JSON.parse(e)}catch{return["",""]}}setAttributionAndPsid(e,t){window.localStorage.setItem("attribution_and_psid",JSON.stringify([e,t]))}shouldIncludeAttribution(e){if(!e.campaign||"unknown"===e.campaign)return!1;let t=getOrCreateProductSessionId(),[i,r]=this.getAttributionAndPsid();return e.createDateTime&&i!==e.createDateTime?(this.setAttributionAndPsid(e.createDateTime,t),!0):!!e.createDateTime&&i===e.createDateTime&&r===t}createCampaign(){let e=this.getAttributions(),t=e&&(e.lastAttribution??e.initialAttribution);if(t&&this.shouldIncludeAttribution(t))return{content:t.content??"",medium:t.medium??"",name:t.campaign??"",source:t.source??"",term:t.term??""};let i=new URLSearchParams(document.location.search);return{content:i.get("utm_content")??"",medium:i.get("utm_medium")??"",name:i.get("utm_campaign")??"",source:i.get("utm_source")??"",term:i.get("utm_term")??""}}createContext(e,t,i){let r=new a.UAParser(navigator.userAgent).getResult();return{application:e.applicationId,client:e.client,library:this.getLibrary(),os:{name:r.os.name??"",version:r.os.version??""},campaign:this.createCampaign(),device:{id:t,manufacturer:r.device.vendor??"",model:r.device.model??"",name:"",type:this.convertDeviceType(r.device.type)},language:getLanguage(),screenResolution:getScreenResolution(),userAgent:navigator.userAgent,bucketingId:i}}}function createUserActivityClient(e){return new o(e)}class o{constructor(e){this.options=e;this.contextHelper=new c;this.sequenceNumber=0;this.userProperties={};this.userId=this.options.userId;this.legacyUserId=this.options.legacyUserId;this.deviceId=this.options.deviceId??fetchAndStoreDeviceId();this.bucketingId=this.options.bucketingId??this.deviceId;this.batchManager=new n(this.options.apiKey,this.options.requestConfig,this.options.cacheKey);window&&"addEventListener"in window&&window.addEventListener("pagehide",()=>{this.batchManager.cache()})}sendIdentifyEvent(e,t,i,...s){if("string"!=typeof e)throw Error("Event name must be a string");this.userProperties=s[0]??{};this.userId=t;this.legacyUserId=i;let n=(0,r.A)(),a={context:this.contextHelper.createContext(this.options,this.deviceId,this.bucketingId),headers:this.getEventHeaders(n),userProperties:this.userProperties,userId:t,incrementProperties:{}};return this.batchManager.registerEvent("identify",a),n}sendNavigationEvent(e,t){if("string"!=typeof e)throw Error("Event name must be a string");let i=(0,r.A)(),s={context:this.contextHelper.createContext(this.options,this.deviceId,this.bucketingId),headers:this.getEventHeaders(i),page:this.getPageInfo(t)};return this.batchManager.registerEvent("navigation",s),i}sendTrackEvent(e,...t){if("string"!=typeof e)throw Error("Event name must be a string");let i=t[0]??{};this.userProperties=t[1]??this.userProperties;let s=t[2]??{},n=(0,r.A)(),a={context:this.contextHelper.createContext(this.options,this.deviceId,this.bucketingId),headers:this.getEventHeaders(n),eventProperties:i??{},userProperties:this.userProperties,name:e,incrementProperties:s,page:this.getPageInfo()};return this.batchManager.registerEvent("track",a),n}sendEvent(e){let t=(0,r.A)(),i={headers:this.getEventHeaders(t),context:this.contextHelper.createContext(this.options,this.deviceId,this.bucketingId),event:e,page:this.getPageInfo()};return this.batchManager.registerEvent("typed",i),t}setUserProperties(e){this.userProperties=e}addUserProperties(e){this.userProperties={...this.userProperties,...e}}getUserProperties(){return this.userProperties}setUserId(e){this.userId=e}getUserId(){return this.userId}setLegacyUserId(e){this.legacyUserId=e}getLegacyUserId(){return this.legacyUserId}setDeviceId(e){this.deviceId=e}getDeviceId(){return this.deviceId}setBucketingId(e){this.bucketingId=e}getBucketingId(){return this.bucketingId}setBatchOptions(e){this.batchManager.setBatchOptions(e)}getBatchOptions(){return this.batchManager.getBatchOptions()}setRequestConfig(e){this.batchManager.setRequestConfig(e)}getRequestConfig(){return this.batchManager.getRequestConfig()}setApiKey(e){this.batchManager.setApiKey(e)}getApiKey(){return this.batchManager.getApiKey()}getProductSessionId(){return getOrCreateProductSessionId()}getEventHeaders(e){let t=this.sequenceNumber;return this.sequenceNumber+=1,{eventId:e,timestamp:new Date().toISOString(),localTime:getISOStringWithLocalTz(new Date),sequenceNumber:t,userId:this.userId,legacyUserId:this.legacyUserId,productSessionId:getOrCreateProductSessionId()}}getPageInfo(e){return{host:e?.host??document.location.host,path:e?.path??document.location.pathname,title:e?.title??document.title,hash:e?.hash??document.location.hash,referrer:e?.referrer??document.referrer,search:e?.search??document.location.search}}}}}]);