@wvdsh/sdk-js 1.3.20 → 1.3.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +19 -0
- package/dist/index.js +75 -0
- package/dist/inject.global.js +2 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -43,17 +43,24 @@ declare abstract class WavedashManager {
|
|
|
43
43
|
* e.g. a PIXI/GDevelop intro video), force-muting it before playback begins
|
|
44
44
|
* regardless of how it was created — the one path the DOM-based sources and
|
|
45
45
|
* the `muted` setter all miss.
|
|
46
|
+
*
|
|
47
|
+
* Speech synthesis (`window.speechSynthesis`): bypasses both Web Audio and HTML
|
|
48
|
+
* media entirely, so it gets its own shim — `speak()` forces the utterance's
|
|
49
|
+
* native volume to 0 while muted.
|
|
46
50
|
*/
|
|
47
51
|
declare class AudioManager extends WavedashManager {
|
|
48
52
|
private _isMuted;
|
|
49
53
|
private contexts;
|
|
50
54
|
private elements;
|
|
51
55
|
private intendedMuted;
|
|
56
|
+
private intendedUtteranceVolume;
|
|
52
57
|
private originalAudioContext;
|
|
53
58
|
private originalWebKitAudioContext;
|
|
54
59
|
private originalAudio;
|
|
55
60
|
private originalMutedDescriptor;
|
|
56
61
|
private originalPlay;
|
|
62
|
+
private originalSpeak;
|
|
63
|
+
private originalUtteranceVolumeDescriptor;
|
|
57
64
|
private mutationObserver;
|
|
58
65
|
constructor(sdk: WavedashSDK);
|
|
59
66
|
isMuted(): boolean;
|
|
@@ -78,6 +85,18 @@ declare class AudioManager extends WavedashManager {
|
|
|
78
85
|
*/
|
|
79
86
|
private trackElement;
|
|
80
87
|
private installShims;
|
|
88
|
+
/**
|
|
89
|
+
* Shim `window.speechSynthesis` so speech respects the SDK mute state.
|
|
90
|
+
*
|
|
91
|
+
* Never swallows speak(): utterances have a lifecycle the game may sequence
|
|
92
|
+
* off (onstart/onend, synth.speaking/pending checks), so every call is
|
|
93
|
+
* delegated and silenced via volume instead. Volume is sampled at speak()
|
|
94
|
+
* time, so forcing the native value to 0 right before delegating silences
|
|
95
|
+
* anything spoken while muted; in-flight speech at the mute edge is
|
|
96
|
+
* deliberately left to finish (can't be softened mid-utterance, and
|
|
97
|
+
* cancel() would discard the pending queue).
|
|
98
|
+
*/
|
|
99
|
+
private shimSpeechSynthesis;
|
|
81
100
|
private shimAudioContextClass;
|
|
82
101
|
destroy(): void;
|
|
83
102
|
}
|
package/dist/index.js
CHANGED
|
@@ -124,12 +124,16 @@ var AudioManager = class extends WavedashManager {
|
|
|
124
124
|
// HTML media elements we know about + their game-intended muted state
|
|
125
125
|
this.elements = new WeakRefSet();
|
|
126
126
|
this.intendedMuted = /* @__PURE__ */ new WeakMap();
|
|
127
|
+
// Speech synthesis utterances + their game-intended volume
|
|
128
|
+
this.intendedUtteranceVolume = /* @__PURE__ */ new WeakMap();
|
|
127
129
|
// Originals (restored on destroy)
|
|
128
130
|
this.originalAudioContext = null;
|
|
129
131
|
this.originalWebKitAudioContext = null;
|
|
130
132
|
this.originalAudio = null;
|
|
131
133
|
this.originalMutedDescriptor = null;
|
|
132
134
|
this.originalPlay = null;
|
|
135
|
+
this.originalSpeak = null;
|
|
136
|
+
this.originalUtteranceVolumeDescriptor = null;
|
|
133
137
|
this.mutationObserver = null;
|
|
134
138
|
this.handleMute = (data) => {
|
|
135
139
|
if (this._isMuted === data.isMuted) return;
|
|
@@ -275,6 +279,66 @@ var AudioManager = class extends WavedashManager {
|
|
|
275
279
|
return originalPlay.call(this);
|
|
276
280
|
};
|
|
277
281
|
})(this);
|
|
282
|
+
this.shimSpeechSynthesis();
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Shim `window.speechSynthesis` so speech respects the SDK mute state.
|
|
286
|
+
*
|
|
287
|
+
* Never swallows speak(): utterances have a lifecycle the game may sequence
|
|
288
|
+
* off (onstart/onend, synth.speaking/pending checks), so every call is
|
|
289
|
+
* delegated and silenced via volume instead. Volume is sampled at speak()
|
|
290
|
+
* time, so forcing the native value to 0 right before delegating silences
|
|
291
|
+
* anything spoken while muted; in-flight speech at the mute edge is
|
|
292
|
+
* deliberately left to finish (can't be softened mid-utterance, and
|
|
293
|
+
* cancel() would discard the pending queue).
|
|
294
|
+
*/
|
|
295
|
+
shimSpeechSynthesis() {
|
|
296
|
+
if (!window.speechSynthesis || typeof SpeechSynthesisUtterance === "undefined") {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
this.originalUtteranceVolumeDescriptor = Object.getOwnPropertyDescriptor(
|
|
300
|
+
SpeechSynthesisUtterance.prototype,
|
|
301
|
+
"volume"
|
|
302
|
+
) ?? null;
|
|
303
|
+
const volDesc = this.originalUtteranceVolumeDescriptor;
|
|
304
|
+
if (volDesc?.get && volDesc?.set) {
|
|
305
|
+
((manager) => {
|
|
306
|
+
Object.defineProperty(SpeechSynthesisUtterance.prototype, "volume", {
|
|
307
|
+
configurable: true,
|
|
308
|
+
get() {
|
|
309
|
+
const intended = manager.intendedUtteranceVolume.get(this);
|
|
310
|
+
return intended !== void 0 ? intended : volDesc.get.call(this);
|
|
311
|
+
},
|
|
312
|
+
set(value) {
|
|
313
|
+
manager.intendedUtteranceVolume.set(this, value);
|
|
314
|
+
volDesc.set.call(this, value);
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
})(this);
|
|
318
|
+
}
|
|
319
|
+
const speechSynthesis = window.speechSynthesis;
|
|
320
|
+
const originalSpeak = speechSynthesis.speak;
|
|
321
|
+
this.originalSpeak = originalSpeak;
|
|
322
|
+
((manager) => {
|
|
323
|
+
speechSynthesis.speak = function(utterance) {
|
|
324
|
+
if (manager._isMuted) {
|
|
325
|
+
if (!manager.intendedUtteranceVolume.has(utterance)) {
|
|
326
|
+
const current = volDesc?.get ? volDesc.get.call(utterance) : utterance.volume;
|
|
327
|
+
manager.intendedUtteranceVolume.set(utterance, current);
|
|
328
|
+
}
|
|
329
|
+
if (volDesc?.set) volDesc.set.call(utterance, 0);
|
|
330
|
+
else utterance.volume = 0;
|
|
331
|
+
} else {
|
|
332
|
+
const intended = manager.intendedUtteranceVolume.get(utterance);
|
|
333
|
+
if (intended !== void 0) {
|
|
334
|
+
if (volDesc?.set) volDesc.set.call(utterance, intended);
|
|
335
|
+
else utterance.volume = intended;
|
|
336
|
+
manager.intendedUtteranceVolume.delete(utterance);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return originalSpeak.call(speechSynthesis, utterance);
|
|
340
|
+
};
|
|
341
|
+
})(this);
|
|
278
342
|
}
|
|
279
343
|
shimAudioContextClass(Original) {
|
|
280
344
|
return /* @__PURE__ */ ((manager) => class extends Original {
|
|
@@ -320,6 +384,16 @@ var AudioManager = class extends WavedashManager {
|
|
|
320
384
|
if (this.originalAudio) {
|
|
321
385
|
window.Audio = this.originalAudio;
|
|
322
386
|
}
|
|
387
|
+
if (this.originalSpeak && window.speechSynthesis) {
|
|
388
|
+
window.speechSynthesis.speak = this.originalSpeak;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (this.originalUtteranceVolumeDescriptor && typeof SpeechSynthesisUtterance !== "undefined") {
|
|
392
|
+
Object.defineProperty(
|
|
393
|
+
SpeechSynthesisUtterance.prototype,
|
|
394
|
+
"volume",
|
|
395
|
+
this.originalUtteranceVolumeDescriptor
|
|
396
|
+
);
|
|
323
397
|
}
|
|
324
398
|
if (this.originalPlay) {
|
|
325
399
|
HTMLMediaElement.prototype.play = this.originalPlay;
|
|
@@ -334,6 +408,7 @@ var AudioManager = class extends WavedashManager {
|
|
|
334
408
|
this.contexts.clear();
|
|
335
409
|
this.elements.clear();
|
|
336
410
|
this.intendedMuted = /* @__PURE__ */ new WeakMap();
|
|
411
|
+
this.intendedUtteranceVolume = /* @__PURE__ */ new WeakMap();
|
|
337
412
|
super.destroy();
|
|
338
413
|
}
|
|
339
414
|
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";(()=>{var gn=Object.create;var Dt=Object.defineProperty;var mn=Object.getOwnPropertyDescriptor;var yn=Object.getOwnPropertyNames;var bn=Object.getPrototypeOf,vn=Object.prototype.hasOwnProperty;var wn=(n,t)=>()=>(t||n((t={exports:{}}).exports,t),t.exports),En=(n,t)=>{for(var e in t)Dt(n,e,{get:t[e],enumerable:!0})},Sn=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of yn(t))!vn.call(n,s)&&s!==e&&Dt(n,s,{get:()=>t[s],enumerable:!(r=mn(t,s))||r.enumerable});return n};var ur=(n,t,e)=>(e=n!=null?gn(bn(n)):{},Sn(t||!n||!n.__esModule?Dt(e,"default",{value:n,enumerable:!0}):e,n));var ir=wn((zl,zr)=>{"use strict";var Kr="Expected a function",Hr=NaN,Bs="[object Symbol]",Gs=/^\s+|\s+$/g,Vs=/^[-+]0x[0-9a-f]+$/i,Ws=/^0b[01]+$/i,Qs=/^0o[0-7]+$/i,js=parseInt,Hs=typeof global=="object"&&global&&global.Object===Object&&global,Js=typeof self=="object"&&self&&self.Object===Object&&self,Ks=Hs||Js||Function("return this")(),zs=Object.prototype,Ys=zs.toString,Zs=Math.max,Xs=Math.min,sr=function(){return Ks.Date.now()};function ei(n,t,e){var r,s,i,o,a,c,l=0,d=!1,g=!1,E=!0;if(typeof n!="function")throw new TypeError(Kr);t=Jr(t)||0,Et(e)&&(d=!!e.leading,g="maxWait"in e,i=g?Zs(Jr(e.maxWait)||0,t):i,E="trailing"in e?!!e.trailing:E);function h(T){var K=r,ye=s;return r=s=void 0,l=T,o=n.apply(ye,K),o}function m(T){return l=T,a=setTimeout(Oe,t),d?h(T):o}function F(T){var K=T-c,ye=T-l,cr=t-K;return g?Xs(cr,i-ye):cr}function ue(T){var K=T-c,ye=T-l;return c===void 0||K>=t||K<0||g&&ye>=i}function Oe(){var T=sr();if(ue(T))return ar(T);a=setTimeout(Oe,F(T))}function ar(T){return a=void 0,E&&r?h(T):(r=s=void 0,o)}function pn(){a!==void 0&&clearTimeout(a),l=0,r=c=s=a=void 0}function fn(){return a===void 0?o:ar(sr())}function Ot(){var T=sr(),K=ue(T);if(r=arguments,s=this,c=T,K){if(a===void 0)return m(c);if(g)return a=setTimeout(Oe,t),h(c)}return a===void 0&&(a=setTimeout(Oe,t)),o}return Ot.cancel=pn,Ot.flush=fn,Ot}function ti(n,t,e){var r=!0,s=!0;if(typeof n!="function")throw new TypeError(Kr);return Et(e)&&(r="leading"in e?!!e.leading:r,s="trailing"in e?!!e.trailing:s),ei(n,t,{leading:r,maxWait:t,trailing:s})}function Et(n){var t=typeof n;return!!n&&(t=="object"||t=="function")}function ri(n){return!!n&&typeof n=="object"}function ni(n){return typeof n=="symbol"||ri(n)&&Ys.call(n)==Bs}function Jr(n){if(typeof n=="number")return n;if(ni(n))return Hr;if(Et(n)){var t=typeof n.valueOf=="function"?n.valueOf():n;n=Et(t)?t+"":t}if(typeof n!="string")return n===0?n:+n;n=n.replace(Gs,"");var e=Ws.test(n);return e||Qs.test(n)?js(n.slice(2),e?2:8):Vs.test(n)?Hr:+n}zr.exports=ti});var k="1.39.1";var de={};En(de,{byteLength:()=>In,fromByteArray:()=>ie,fromByteArrayUrlSafeNoPadding:()=>Mn,toByteArray:()=>le});var $=[],O=[],Cn=Uint8Array,Ut="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for(se=0,lr=Ut.length;se<lr;++se)$[se]=Ut[se],O[Ut.charCodeAt(se)]=se;var se,lr;O[45]=62;O[95]=63;function dr(n){var t=n.length;if(t%4>0)throw new Error("Invalid string. Length must be a multiple of 4");var e=n.indexOf("=");e===-1&&(e=t);var r=e===t?0:4-e%4;return[e,r]}function In(n){var t=dr(n),e=t[0],r=t[1];return(e+r)*3/4-r}function Pn(n,t,e){return(t+e)*3/4-e}function le(n){var t,e=dr(n),r=e[0],s=e[1],i=new Cn(Pn(n,r,s)),o=0,a=s>0?r-4:r,c;for(c=0;c<a;c+=4)t=O[n.charCodeAt(c)]<<18|O[n.charCodeAt(c+1)]<<12|O[n.charCodeAt(c+2)]<<6|O[n.charCodeAt(c+3)],i[o++]=t>>16&255,i[o++]=t>>8&255,i[o++]=t&255;return s===2&&(t=O[n.charCodeAt(c)]<<2|O[n.charCodeAt(c+1)]>>4,i[o++]=t&255),s===1&&(t=O[n.charCodeAt(c)]<<10|O[n.charCodeAt(c+1)]<<4|O[n.charCodeAt(c+2)]>>2,i[o++]=t>>8&255,i[o++]=t&255),i}function Tn(n){return $[n>>18&63]+$[n>>12&63]+$[n>>6&63]+$[n&63]}function An(n,t,e){for(var r,s=[],i=t;i<e;i+=3)r=(n[i]<<16&16711680)+(n[i+1]<<8&65280)+(n[i+2]&255),s.push(Tn(r));return s.join("")}function ie(n){for(var t,e=n.length,r=e%3,s=[],i=16383,o=0,a=e-r;o<a;o+=i)s.push(An(n,o,o+i>a?a:o+i));return r===1?(t=n[e-1],s.push($[t>>2]+$[t<<4&63]+"==")):r===2&&(t=(n[e-2]<<8)+n[e-1],s.push($[t>>10]+$[t>>4&63]+$[t<<2&63]+"=")),s.join("")}function Mn(n){return ie(n).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function R(n){if(n===void 0)return{};if(!De(n))throw new Error(`The arguments to a Convex function must be an object. Received: ${n}`);return n}function be(n){if(typeof n>"u")throw new Error("Client created with undefined deployment address. If you used an environment variable, check that it's set.");if(typeof n!="string")throw new Error(`Invalid deployment address: found ${n}".`);if(!(n.startsWith("http:")||n.startsWith("https:")))throw new Error(`Invalid deployment address: Must start with "https://" or "http://". Found "${n}".`);try{new URL(n)}catch{throw new Error(`Invalid deployment address: "${n}" is not a valid URL. If you believe this URL is correct, use the \`skipConvexDeploymentUrlCheck\` option to bypass this.`)}if(n.endsWith(".convex.site"))throw new Error(`Invalid deployment address: "${n}" ends with .convex.site, which is used for HTTP Actions. Convex deployment URLs typically end with .convex.cloud? If you believe this URL is correct, use the \`skipConvexDeploymentUrlCheck\` option to bypass this.`)}function De(n){let t=typeof n=="object",e=Object.getPrototypeOf(n),r=e===null||e===Object.prototype||e?.constructor?.name==="Object";return t&&r}var fr=!0,he=BigInt("-9223372036854775808"),qt=BigInt("9223372036854775807"),Ft=BigInt("0"),xn=BigInt("8"),Rn=BigInt("256");function gr(n){return Number.isNaN(n)||!Number.isFinite(n)||Object.is(n,-0)}function _n(n){n<Ft&&(n-=he+he);let t=n.toString(16);t.length%2===1&&(t="0"+t);let e=new Uint8Array(new ArrayBuffer(8)),r=0;for(let s of t.match(/.{2}/g).reverse())e.set([parseInt(s,16)],r++),n>>=xn;return ie(e)}function Ln(n){let t=le(n);if(t.byteLength!==8)throw new Error(`Received ${t.byteLength} bytes, expected 8 for $integer`);let e=Ft,r=Ft;for(let s of t)e+=BigInt(s)*Rn**r,r++;return e>qt&&(e+=he+he),e}function kn(n){if(n<he||qt<n)throw new Error(`BigInt ${n} does not fit into a 64-bit signed integer.`);let t=new ArrayBuffer(8);return new DataView(t).setBigInt64(0,n,!0),ie(new Uint8Array(t))}function On(n){let t=le(n);if(t.byteLength!==8)throw new Error(`Received ${t.byteLength} bytes, expected 8 for $integer`);return new DataView(t.buffer).getBigInt64(0,!0)}var Dn=DataView.prototype.setBigInt64?kn:_n,Un=DataView.prototype.getBigInt64?On:Ln,hr=1024;function $t(n){if(n.length>hr)throw new Error(`Field name ${n} exceeds maximum field name length ${hr}.`);if(n.startsWith("$"))throw new Error(`Field name ${n} starts with a '$', which is reserved.`);for(let t=0;t<n.length;t+=1){let e=n.charCodeAt(t);if(e<32||e>=127)throw new Error(`Field name ${n} has invalid character '${n[t]}': Field names can only contain non-control ASCII characters`)}}function x(n){if(n===null||typeof n=="boolean"||typeof n=="number"||typeof n=="string")return n;if(Array.isArray(n))return n.map(r=>x(r));if(typeof n!="object")throw new Error(`Unexpected type of ${n}`);let t=Object.entries(n);if(t.length===1){let r=t[0][0];if(r==="$bytes"){if(typeof n.$bytes!="string")throw new Error(`Malformed $bytes field on ${n}`);return le(n.$bytes).buffer}if(r==="$integer"){if(typeof n.$integer!="string")throw new Error(`Malformed $integer field on ${n}`);return Un(n.$integer)}if(r==="$float"){if(typeof n.$float!="string")throw new Error(`Malformed $float field on ${n}`);let s=le(n.$float);if(s.byteLength!==8)throw new Error(`Received ${s.byteLength} bytes, expected 8 for $float`);let o=new DataView(s.buffer).getFloat64(0,fr);if(!gr(o))throw new Error(`Float ${o} should be encoded as a number`);return o}if(r==="$set")throw new Error("Received a Set which is no longer supported as a Convex type.");if(r==="$map")throw new Error("Received a Map which is no longer supported as a Convex type.")}let e={};for(let[r,s]of Object.entries(n))$t(r),e[r]=x(s);return e}var pr=16384;function oe(n){let t=JSON.stringify(n,(e,r)=>r===void 0?"undefined":typeof r=="bigint"?`${r.toString()}n`:r);if(t.length>pr){let e="[...truncated]",r=pr-e.length,s=t.codePointAt(r-1);return s!==void 0&&s>65535&&(r-=1),t.substring(0,r)+e}return t}function Ue(n,t,e,r){if(n===void 0){let o=e&&` (present at path ${e} in original object ${oe(t)})`;throw new Error(`undefined is not a valid Convex value${o}. To learn about Convex's supported types, see https://docs.convex.dev/using/types.`)}if(n===null)return n;if(typeof n=="bigint"){if(n<he||qt<n)throw new Error(`BigInt ${n} does not fit into a 64-bit signed integer.`);return{$integer:Dn(n)}}if(typeof n=="number")if(gr(n)){let o=new ArrayBuffer(8);return new DataView(o).setFloat64(0,n,fr),{$float:ie(new Uint8Array(o))}}else return n;if(typeof n=="boolean"||typeof n=="string")return n;if(n instanceof ArrayBuffer)return{$bytes:ie(new Uint8Array(n))};if(Array.isArray(n))return n.map((o,a)=>Ue(o,t,e+`[${a}]`,!1));if(n instanceof Set)throw new Error(Nt(e,"Set",[...n],t));if(n instanceof Map)throw new Error(Nt(e,"Map",[...n],t));if(!De(n)){let o=n?.constructor?.name,a=o?`${o} `:"";throw new Error(Nt(e,a,n,t))}let s={},i=Object.entries(n);i.sort(([o,a],[c,l])=>o===c?0:o<c?-1:1);for(let[o,a]of i)a!==void 0?($t(o),s[o]=Ue(a,t,e+`.${o}`,!1)):r&&($t(o),s[o]=Nn(a,t,e+`.${o}`));return s}function Nt(n,t,e,r){return n?`${t}${oe(e)} is not a supported Convex type (present at path ${n} in original object ${oe(r)}). To learn about Convex's supported types, see https://docs.convex.dev/using/types.`:`${t}${oe(e)} is not a supported Convex type.`}function Nn(n,t,e){if(n===void 0)return{$undefined:null};if(t===void 0)throw new Error(`Programming error. Current value is ${oe(n)} but original value is undefined`);return Ue(n,t,e,!1)}function A(n){return Ue(n,n,"",!1)}var Fn=Object.defineProperty,$n=(n,t,e)=>t in n?Fn(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,S=(n,t,e)=>$n(n,typeof t!="symbol"?t+"":t,e),qn="https://docs.convex.dev/error#undefined-validator";function ve(n,t){let e=t!==void 0?` for field "${t}"`:"";throw new Error(`A validator is undefined${e} in ${n}. This is often caused by circular imports. See ${qn} for details.`)}var _=class{constructor({isOptional:t}){S(this,"type"),S(this,"fieldPaths"),S(this,"isOptional"),S(this,"isConvexValidator"),this.isOptional=t,this.isConvexValidator=!0}},Ne=class n extends _{constructor({isOptional:t,tableName:e}){if(super({isOptional:t}),S(this,"tableName"),S(this,"kind","id"),typeof e!="string")throw new Error("v.id(tableName) requires a string");this.tableName=e}get json(){return{type:"id",tableName:this.tableName}}asOptional(){return new n({isOptional:"optional",tableName:this.tableName})}},we=class n extends _{constructor(){super(...arguments),S(this,"kind","float64")}get json(){return{type:"number"}}asOptional(){return new n({isOptional:"optional"})}},Ee=class n extends _{constructor(){super(...arguments),S(this,"kind","int64")}get json(){return{type:"bigint"}}asOptional(){return new n({isOptional:"optional"})}},Fe=class n extends _{constructor(){super(...arguments),S(this,"kind","boolean")}get json(){return{type:this.kind}}asOptional(){return new n({isOptional:"optional"})}},$e=class n extends _{constructor(){super(...arguments),S(this,"kind","bytes")}get json(){return{type:this.kind}}asOptional(){return new n({isOptional:"optional"})}},qe=class n extends _{constructor(){super(...arguments),S(this,"kind","string")}get json(){return{type:this.kind}}asOptional(){return new n({isOptional:"optional"})}},Be=class n extends _{constructor(){super(...arguments),S(this,"kind","null")}get json(){return{type:this.kind}}asOptional(){return new n({isOptional:"optional"})}},Ge=class n extends _{constructor(){super(...arguments),S(this,"kind","any")}get json(){return{type:this.kind}}asOptional(){return new n({isOptional:"optional"})}},Ve=class n extends _{constructor({isOptional:t,fields:e}){super({isOptional:t}),S(this,"fields"),S(this,"kind","object"),globalThis.Object.entries(e).forEach(([r,s])=>{if(s===void 0&&ve("v.object()",r),!s.isConvexValidator)throw new Error("v.object() entries must be validators")}),this.fields=e}get json(){return{type:this.kind,value:globalThis.Object.fromEntries(globalThis.Object.entries(this.fields).map(([t,e])=>[t,{fieldType:e.json,optional:e.isOptional==="optional"}]))}}asOptional(){return new n({isOptional:"optional",fields:this.fields})}omit(...t){let e={...this.fields};for(let r of t)delete e[r];return new n({isOptional:this.isOptional,fields:e})}pick(...t){let e={};for(let r of t)e[r]=this.fields[r];return new n({isOptional:this.isOptional,fields:e})}partial(){let t={};for(let[e,r]of globalThis.Object.entries(this.fields))t[e]=r.asOptional();return new n({isOptional:this.isOptional,fields:t})}extend(t){return new n({isOptional:this.isOptional,fields:{...this.fields,...t}})}},We=class n extends _{constructor({isOptional:t,value:e}){if(super({isOptional:t}),S(this,"value"),S(this,"kind","literal"),typeof e!="string"&&typeof e!="boolean"&&typeof e!="number"&&typeof e!="bigint")throw new Error("v.literal(value) must be a string, number, or boolean");this.value=e}get json(){return{type:this.kind,value:A(this.value)}}asOptional(){return new n({isOptional:"optional",value:this.value})}},Qe=class n extends _{constructor({isOptional:t,element:e}){super({isOptional:t}),S(this,"element"),S(this,"kind","array"),e===void 0&&ve("v.array()"),this.element=e}get json(){return{type:this.kind,value:this.element.json}}asOptional(){return new n({isOptional:"optional",element:this.element})}},je=class n extends _{constructor({isOptional:t,key:e,value:r}){if(super({isOptional:t}),S(this,"key"),S(this,"value"),S(this,"kind","record"),e===void 0&&ve("v.record()","key"),r===void 0&&ve("v.record()","value"),e.isOptional==="optional")throw new Error("Record validator cannot have optional keys");if(r.isOptional==="optional")throw new Error("Record validator cannot have optional values");if(!e.isConvexValidator||!r.isConvexValidator)throw new Error("Key and value of v.record() but be validators");this.key=e,this.value=r}get json(){return{type:this.kind,keys:this.key.json,values:{fieldType:this.value.json,optional:!1}}}asOptional(){return new n({isOptional:"optional",key:this.key,value:this.value})}},He=class n extends _{constructor({isOptional:t,members:e}){super({isOptional:t}),S(this,"members"),S(this,"kind","union"),e.forEach((r,s)=>{if(r===void 0&&ve("v.union()",`member at index ${s}`),!r.isConvexValidator)throw new Error("All members of v.union() must be validators")}),this.members=e}get json(){return{type:this.kind,value:this.members.map(t=>t.json)}}asOptional(){return new n({isOptional:"optional",members:this.members})}};function Bt(n){return!!n.isConvexValidator}var p={id:n=>new Ne({isOptional:"required",tableName:n}),null:()=>new Be({isOptional:"required"}),number:()=>new we({isOptional:"required"}),float64:()=>new we({isOptional:"required"}),bigint:()=>new Ee({isOptional:"required"}),int64:()=>new Ee({isOptional:"required"}),boolean:()=>new Fe({isOptional:"required"}),string:()=>new qe({isOptional:"required"}),bytes:()=>new $e({isOptional:"required"}),literal:n=>new We({isOptional:"required",value:n}),array:n=>new Qe({isOptional:"required",element:n}),object:n=>new Ve({isOptional:"required",fields:n}),record:(n,t)=>new je({isOptional:"required",key:n,value:t}),union:(...n)=>new He({isOptional:"required",members:n}),any:()=>new Ge({isOptional:"required"}),optional:n=>n.asOptional(),nullable:n=>p.union(n,p.null())};var Bn=Object.defineProperty,Gn=(n,t,e)=>t in n?Bn(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,Gt=(n,t,e)=>Gn(n,typeof t!="symbol"?t+"":t,e),mr,yr,Vn=Symbol.for("ConvexError"),j=class extends(yr=Error,mr=Vn,yr){constructor(t){super(typeof t=="string"?t:oe(t)),Gt(this,"name","ConvexError"),Gt(this,"data"),Gt(this,mr,!0),this.data=t}};var br=()=>Array.from({length:4},()=>0),wi=br(),Ei=br();var Qn=Object.defineProperty,jn=(n,t,e)=>t in n?Qn(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,vr=(n,t,e)=>jn(n,typeof t!="symbol"?t+"":t,e),Hn="color:rgb(0, 145, 255)";function wr(n){switch(n){case"query":return"Q";case"mutation":return"M";case"action":return"A";case"any":return"?"}}var Je=class{constructor(t){vr(this,"_onLogLineFuncs"),vr(this,"_verbose"),this._onLogLineFuncs={},this._verbose=t.verbose}addLogLineListener(t){let e=Math.random().toString(36).substring(2,15);for(let r=0;r<10&&this._onLogLineFuncs[e]!==void 0;r++)e=Math.random().toString(36).substring(2,15);return this._onLogLineFuncs[e]=t,()=>{delete this._onLogLineFuncs[e]}}logVerbose(...t){if(this._verbose)for(let e of Object.values(this._onLogLineFuncs))e("debug",`${new Date().toISOString()}`,...t)}log(...t){for(let e of Object.values(this._onLogLineFuncs))e("info",...t)}warn(...t){for(let e of Object.values(this._onLogLineFuncs))e("warn",...t)}error(...t){for(let e of Object.values(this._onLogLineFuncs))e("error",...t)}};function Vt(n){let t=new Je(n);return t.addLogLineListener((e,...r)=>{switch(e){case"debug":console.debug(...r);break;case"info":console.log(...r);break;case"warn":console.warn(...r);break;case"error":console.error(...r);break;default:console.log(...r)}}),t}function Wt(n){return new Je(n)}function ae(n,t,e,r,s){let i=wr(e);if(typeof s=="object"&&(s=`ConvexError ${JSON.stringify(s.errorData,null,2)}`),t==="info"){let o=s.match(/^\[.*?\] /);if(o===null){n.error(`[CONVEX ${i}(${r})] Could not parse console.log`);return}let a=s.slice(1,o[0].length-2),c=s.slice(o[0].length);n.log(`%c[CONVEX ${i}(${r})] [${a}]`,Hn,c)}else n.error(`[CONVEX ${i}(${r})] ${s}`)}function Er(n,t){let e=`[CONVEX FATAL ERROR] ${t}`;return n.error(e),new Error(e)}function z(n,t,e){return`[CONVEX ${wr(n)}(${t})] ${e.errorMessage}
|
|
2
|
+
Called by client`}function Se(n,t){return t.data=n.errorData,t}function q(n){let t=n.split(":"),e,r;return t.length===1?(e=t[0],r="default"):(e=t.slice(0,t.length-1).join(":"),r=t[t.length-1]),e.endsWith(".js")&&(e=e.slice(0,-3)),`${e}:${r}`}function B(n,t){return JSON.stringify({udfPath:q(n),args:A(t)})}function Qt(n,t,e){let{initialNumItems:r,id:s}=e;return JSON.stringify({type:"paginated",udfPath:q(n),args:A(t),options:A({initialNumItems:r,id:s})})}function Sr(n){return JSON.parse(n).type==="paginated"}var Jn=Object.defineProperty,Kn=(n,t,e)=>t in n?Jn(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,G=(n,t,e)=>Kn(n,typeof t!="symbol"?t+"":t,e),Ke=class{constructor(){G(this,"nextQueryId"),G(this,"querySetVersion"),G(this,"querySet"),G(this,"queryIdToToken"),G(this,"identityVersion"),G(this,"auth"),G(this,"outstandingQueriesOlderThanRestart"),G(this,"outstandingAuthOlderThanRestart"),G(this,"paused"),G(this,"pendingQuerySetModifications"),this.nextQueryId=0,this.querySetVersion=0,this.identityVersion=0,this.querySet=new Map,this.queryIdToToken=new Map,this.outstandingQueriesOlderThanRestart=new Set,this.outstandingAuthOlderThanRestart=!1,this.paused=!1,this.pendingQuerySetModifications=new Map}hasSyncedPastLastReconnect(){return this.outstandingQueriesOlderThanRestart.size===0&&!this.outstandingAuthOlderThanRestart}markAuthCompletion(){this.outstandingAuthOlderThanRestart=!1}subscribe(t,e,r,s){let i=q(t),o=B(i,e),a=this.querySet.get(o);if(a!==void 0)return a.numSubscribers+=1,{queryToken:o,modification:null,unsubscribe:()=>this.removeSubscriber(o)};{let c=this.nextQueryId++,l={id:c,canonicalizedUdfPath:i,args:e,numSubscribers:1,journal:r,componentPath:s};this.querySet.set(o,l),this.queryIdToToken.set(c,o);let d=this.querySetVersion,g=this.querySetVersion+1,E={type:"Add",queryId:c,udfPath:i,args:[A(e)],journal:r,componentPath:s};return this.paused?this.pendingQuerySetModifications.set(c,E):this.querySetVersion=g,{queryToken:o,modification:{type:"ModifyQuerySet",baseVersion:d,newVersion:g,modifications:[E]},unsubscribe:()=>this.removeSubscriber(o)}}}transition(t){for(let e of t.modifications)switch(e.type){case"QueryUpdated":case"QueryFailed":{this.outstandingQueriesOlderThanRestart.delete(e.queryId);let r=e.journal;if(r!==void 0){let s=this.queryIdToToken.get(e.queryId);s!==void 0&&(this.querySet.get(s).journal=r)}break}case"QueryRemoved":{this.outstandingQueriesOlderThanRestart.delete(e.queryId);break}default:throw new Error(`Invalid modification ${e.type}`)}}queryId(t,e){let r=q(t),s=B(r,e),i=this.querySet.get(s);return i!==void 0?i.id:null}isCurrentOrNewerAuthVersion(t){return t>=this.identityVersion}getAuth(){return this.auth}setAuth(t){this.auth={tokenType:"User",value:t};let e=this.identityVersion;return this.paused||(this.identityVersion=e+1),{type:"Authenticate",baseVersion:e,...this.auth}}setAdminAuth(t,e){let r={tokenType:"Admin",value:t,impersonating:e};this.auth=r;let s=this.identityVersion;return this.paused||(this.identityVersion=s+1),{type:"Authenticate",baseVersion:s,...r}}clearAuth(){this.auth=void 0,this.markAuthCompletion();let t=this.identityVersion;return this.paused||(this.identityVersion=t+1),{type:"Authenticate",tokenType:"None",baseVersion:t}}hasAuth(){return!!this.auth}isNewAuth(t){return this.auth?.value!==t}queryPath(t){let e=this.queryIdToToken.get(t);return e?this.querySet.get(e).canonicalizedUdfPath:null}queryArgs(t){let e=this.queryIdToToken.get(t);return e?this.querySet.get(e).args:null}queryToken(t){return this.queryIdToToken.get(t)??null}queryJournal(t){return this.querySet.get(t)?.journal}restart(){this.unpause(),this.outstandingQueriesOlderThanRestart.clear();let t=[];for(let s of this.querySet.values()){let i={type:"Add",queryId:s.id,udfPath:s.canonicalizedUdfPath,args:[A(s.args)],journal:s.journal,componentPath:s.componentPath};t.push(i),this.outstandingQueriesOlderThanRestart.add(s.id)}this.querySetVersion=1;let e={type:"ModifyQuerySet",baseVersion:0,newVersion:1,modifications:t};if(!this.auth)return this.identityVersion=0,[e,void 0];this.outstandingAuthOlderThanRestart=!0;let r={type:"Authenticate",baseVersion:0,...this.auth};return this.identityVersion=1,[e,r]}pause(){this.paused=!0}resume(){let t=this.pendingQuerySetModifications.size>0?{type:"ModifyQuerySet",baseVersion:this.querySetVersion,newVersion:++this.querySetVersion,modifications:Array.from(this.pendingQuerySetModifications.values())}:void 0,e=this.auth!==void 0?{type:"Authenticate",baseVersion:this.identityVersion++,...this.auth}:void 0;return this.unpause(),[t,e]}unpause(){this.paused=!1,this.pendingQuerySetModifications.clear()}removeSubscriber(t){let e=this.querySet.get(t);if(e.numSubscribers>1)return e.numSubscribers-=1,null;{this.querySet.delete(t),this.queryIdToToken.delete(e.id),this.outstandingQueriesOlderThanRestart.delete(e.id);let r=this.querySetVersion,s=this.querySetVersion+1,i={type:"Remove",queryId:e.id};return this.paused?this.pendingQuerySetModifications.has(e.id)?this.pendingQuerySetModifications.delete(e.id):this.pendingQuerySetModifications.set(e.id,i):this.querySetVersion=s,{type:"ModifyQuerySet",baseVersion:r,newVersion:s,modifications:[i]}}}};var zn=Object.defineProperty,Yn=(n,t,e)=>t in n?zn(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,ze=(n,t,e)=>Yn(n,typeof t!="symbol"?t+"":t,e),Ye=class{constructor(t,e){this.logger=t,this.markConnectionStateDirty=e,ze(this,"inflightRequests"),ze(this,"requestsOlderThanRestart"),ze(this,"inflightMutationsCount",0),ze(this,"inflightActionsCount",0),this.inflightRequests=new Map,this.requestsOlderThanRestart=new Set}request(t,e){let r=new Promise(s=>{let i=e?"Requested":"NotSent";this.inflightRequests.set(t.requestId,{message:t,status:{status:i,requestedAt:new Date,onResult:s}}),t.type==="Mutation"?this.inflightMutationsCount++:t.type==="Action"&&this.inflightActionsCount++});return this.markConnectionStateDirty(),r}onResponse(t){let e=this.inflightRequests.get(t.requestId);if(e===void 0||e.status.status==="Completed")return null;let r=e.message.type==="Mutation"?"mutation":"action",s=e.message.udfPath;for(let c of t.logLines)ae(this.logger,"info",r,s,c);let i=e.status,o,a;if(t.success)o={success:!0,logLines:t.logLines,value:x(t.result)},a=()=>i.onResult(o);else{let c=t.result,{errorData:l}=t;ae(this.logger,"error",r,s,c),o={success:!1,errorMessage:c,errorData:l!==void 0?x(l):void 0,logLines:t.logLines},a=()=>i.onResult(o)}return t.type==="ActionResponse"||!t.success?(a(),this.inflightRequests.delete(t.requestId),this.requestsOlderThanRestart.delete(t.requestId),e.message.type==="Action"?this.inflightActionsCount--:e.message.type==="Mutation"&&this.inflightMutationsCount--,this.markConnectionStateDirty(),{requestId:t.requestId,result:o}):(e.status={status:"Completed",result:o,ts:t.ts,onResolve:a},null)}removeCompleted(t){let e=new Map;for(let[r,s]of this.inflightRequests.entries()){let i=s.status;i.status==="Completed"&&i.ts.lessThanOrEqual(t)&&(i.onResolve(),e.set(r,i.result),s.message.type==="Mutation"?this.inflightMutationsCount--:s.message.type==="Action"&&this.inflightActionsCount--,this.inflightRequests.delete(r),this.requestsOlderThanRestart.delete(r))}return e.size>0&&this.markConnectionStateDirty(),e}restart(){this.requestsOlderThanRestart=new Set(this.inflightRequests.keys());let t=[];for(let[e,r]of this.inflightRequests){if(r.status.status==="NotSent"){r.status.status="Requested",t.push(r.message);continue}if(r.message.type==="Mutation")t.push(r.message);else if(r.message.type==="Action"){if(this.inflightRequests.delete(e),this.requestsOlderThanRestart.delete(e),this.inflightActionsCount--,r.status.status==="Completed")throw new Error("Action should never be in 'Completed' state");r.status.onResult({success:!1,errorMessage:"Connection lost while action was in flight",logLines:[]})}}return this.markConnectionStateDirty(),t}resume(){let t=[];for(let[,e]of this.inflightRequests)if(e.status.status==="NotSent"){e.status.status="Requested",t.push(e.message);continue}return t}hasIncompleteRequests(){for(let t of this.inflightRequests.values())if(t.status.status==="Requested")return!0;return!1}hasInflightRequests(){return this.inflightRequests.size>0}hasSyncedPastLastReconnect(){return this.requestsOlderThanRestart.size===0}timeOfOldestInflightRequest(){if(this.inflightRequests.size===0)return null;let t=Date.now();for(let e of this.inflightRequests.values())e.status.status!=="Completed"&&e.status.requestedAt.getTime()<t&&(t=e.status.requestedAt.getTime());return new Date(t)}inflightMutations(){return this.inflightMutationsCount}inflightActions(){return this.inflightActionsCount}};var pe=Symbol.for("functionName");var Cr=Symbol.for("toReferencePath");function Zn(n){return n[Cr]??null}function Xn(n){return n.startsWith("function://")}function Y(n){let t;if(typeof n=="string")Xn(n)?t={functionHandle:n}:t={name:n};else if(n[pe])t={name:n[pe]};else{let e=Zn(n);if(!e)throw new Error(`${n} is not a functionReference`);t={reference:e}}return t}function L(n){let t=Y(n);if(t.name===void 0)throw t.functionHandle!==void 0?new Error(`Expected function reference like "api.file.func" or "internal.file.func", but received function handle ${t.functionHandle}`):t.reference!==void 0?new Error(`Expected function reference in the current component like "api.file.func" or "internal.file.func", but received reference ${t.reference}`):new Error(`Expected function reference like "api.file.func" or "internal.file.func", but received ${JSON.stringify(t)}`);if(typeof n=="string")return n;let e=n[pe];if(!e)throw new Error(`${n} is not a functionReference`);return e}function Ir(n=[]){let t={get(e,r){if(typeof r=="string"){let s=[...n,r];return Ir(s)}else if(r===pe){if(n.length<2){let o=["api",...n].join(".");throw new Error(`API path is expected to be of the form \`api.moduleName.functionName\`. Found: \`${o}\``)}let s=n.slice(0,-1).join("/"),i=n[n.length-1];return i==="default"?s:s+":"+i}else return r===Symbol.toStringTag?"FunctionReference":void 0}};return new Proxy({},t)}var jt=Ir();var es=Object.defineProperty,ts=(n,t,e)=>t in n?es(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,Ze=(n,t,e)=>ts(n,typeof t!="symbol"?t+"":t,e),Xe=class n{constructor(t){Ze(this,"queryResults"),Ze(this,"modifiedQueries"),this.queryResults=t,this.modifiedQueries=[]}getQuery(t,...e){let r=R(e[0]),s=L(t),i=this.queryResults.get(B(s,r));if(i!==void 0)return n.queryValue(i.result)}getAllQueries(t){let e=[],r=L(t);for(let s of this.queryResults.values())s.udfPath===q(r)&&e.push({args:s.args,value:n.queryValue(s.result)});return e}setQuery(t,e,r){let s=R(e),i=L(t),o=B(i,s),a;r===void 0?a=void 0:a={success:!0,value:r,logLines:[]};let c={udfPath:i,args:s,result:a};this.queryResults.set(o,c),this.modifiedQueries.push(o)}static queryValue(t){if(t!==void 0)return t.success?t.value:void 0}},et=class{constructor(){Ze(this,"queryResults"),Ze(this,"optimisticUpdates"),this.queryResults=new Map,this.optimisticUpdates=[]}ingestQueryResultsFromServer(t,e){this.optimisticUpdates=this.optimisticUpdates.filter(o=>!e.has(o.mutationId));let r=this.queryResults;this.queryResults=new Map(t);let s=new Xe(this.queryResults);for(let o of this.optimisticUpdates)o.update(s);let i=[];for(let[o,a]of this.queryResults){let c=r.get(o);(c===void 0||c.result!==a.result)&&i.push(o)}return i}applyOptimisticUpdate(t,e){this.optimisticUpdates.push({update:t,mutationId:e});let r=new Xe(this.queryResults);return t(r),r.modifiedQueries}rawQueryResult(t){let e=this.queryResults.get(t);if(e!==void 0)return e.result}queryResult(t){let e=this.queryResults.get(t);if(e===void 0)return;let r=e.result;if(r!==void 0){if(r.success)return r.value;throw r.errorData!==void 0?Se(r,new j(z("query",e.udfPath,r))):new Error(z("query",e.udfPath,r))}}hasQueryResult(t){return this.queryResults.get(t)!==void 0}queryLogs(t){return this.queryResults.get(t)?.result?.logLines}};var rs=Object.defineProperty,ns=(n,t,e)=>t in n?rs(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,Ht=(n,t,e)=>ns(n,typeof t!="symbol"?t+"":t,e),V=class n{constructor(t,e){Ht(this,"low"),Ht(this,"high"),Ht(this,"__isUnsignedLong__"),this.low=t|0,this.high=e|0,this.__isUnsignedLong__=!0}static isLong(t){return(t&&t.__isUnsignedLong__)===!0}static fromBytesLE(t){return new n(t[0]|t[1]<<8|t[2]<<16|t[3]<<24,t[4]|t[5]<<8|t[6]<<16|t[7]<<24)}toBytesLE(){let t=this.high,e=this.low;return[e&255,e>>>8&255,e>>>16&255,e>>>24,t&255,t>>>8&255,t>>>16&255,t>>>24]}static fromNumber(t){return isNaN(t)||t<0?Pr:t>=ss?is:new n(t%Ce|0,t/Ce|0)}toString(){return(BigInt(this.high)*BigInt(Ce)+BigInt(this.low)).toString()}equals(t){return n.isLong(t)||(t=n.fromValue(t)),this.high>>>31===1&&t.high>>>31===1?!1:this.high===t.high&&this.low===t.low}notEquals(t){return!this.equals(t)}comp(t){return n.isLong(t)||(t=n.fromValue(t)),this.equals(t)?0:t.high>>>0>this.high>>>0||t.high===this.high&&t.low>>>0>this.low>>>0?-1:1}lessThanOrEqual(t){return this.comp(t)<=0}static fromValue(t){return typeof t=="number"?n.fromNumber(t):new n(t.low,t.high)}},Pr=new V(0,0),Tr=65536,Ce=Tr*Tr,ss=Ce*Ce,is=new V(-1,-1);var os=Object.defineProperty,as=(n,t,e)=>t in n?os(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,tt=(n,t,e)=>as(n,typeof t!="symbol"?t+"":t,e),Ie=class{constructor(t,e){tt(this,"version"),tt(this,"remoteQuerySet"),tt(this,"queryPath"),tt(this,"logger"),this.version={querySet:0,ts:V.fromNumber(0),identity:0},this.remoteQuerySet=new Map,this.queryPath=t,this.logger=e}transition(t){let e=t.startVersion;if(this.version.querySet!==e.querySet||this.version.ts.notEquals(e.ts)||this.version.identity!==e.identity)throw new Error(`Invalid start version: ${e.ts.toString()}:${e.querySet}:${e.identity}, transitioning from ${this.version.ts.toString()}:${this.version.querySet}:${this.version.identity}`);for(let r of t.modifications)switch(r.type){case"QueryUpdated":{let s=this.queryPath(r.queryId);if(s)for(let o of r.logLines)ae(this.logger,"info","query",s,o);let i=x(r.value??null);this.remoteQuerySet.set(r.queryId,{success:!0,value:i,logLines:r.logLines});break}case"QueryFailed":{let s=this.queryPath(r.queryId);if(s)for(let o of r.logLines)ae(this.logger,"info","query",s,o);let{errorData:i}=r;this.remoteQuerySet.set(r.queryId,{success:!1,errorMessage:r.errorMessage,errorData:i!==void 0?x(i):void 0,logLines:r.logLines});break}case"QueryRemoved":{this.remoteQuerySet.delete(r.queryId);break}default:throw new Error(`Invalid modification ${r.type}`)}this.version=t.endVersion}remoteQueryResults(){return this.remoteQuerySet}timestamp(){return this.version.ts}};function Jt(n){let t=de.toByteArray(n);return V.fromBytesLE(Array.from(t))}function cs(n){let t=new Uint8Array(n.toBytesLE());return de.fromByteArray(t)}function Kt(n){switch(n.type){case"FatalError":case"AuthError":case"ActionResponse":case"TransitionChunk":case"Ping":return{...n};case"MutationResponse":return n.success?{...n,ts:Jt(n.ts)}:{...n};case"Transition":return{...n,startVersion:{...n.startVersion,ts:Jt(n.startVersion.ts)},endVersion:{...n.endVersion,ts:Jt(n.endVersion.ts)}};default:}}function Ar(n){switch(n.type){case"Authenticate":case"ModifyQuerySet":case"Mutation":case"Action":case"Event":return{...n};case"Connect":return n.maxObservedTimestamp!==void 0?{...n,maxObservedTimestamp:cs(n.maxObservedTimestamp)}:{...n,maxObservedTimestamp:void 0};default:}}var us=Object.defineProperty,ls=(n,t,e)=>t in n?us(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,I=(n,t,e)=>ls(n,typeof t!="symbol"?t+"":t,e),ds=1e3,hs=1001,ps=1005,fs=4040,rt;function fe(){return rt===void 0&&(rt=Date.now()),typeof performance>"u"||!performance.now?Date.now():Math.round(rt+performance.now())}function Mr(){return`t=${Math.round((fe()-rt)/100)/10}s`}var xr={InternalServerError:{timeout:1e3},SubscriptionsWorkerFullError:{timeout:3e3},TooManyConcurrentRequests:{timeout:3e3},CommitterFullError:{timeout:3e3},AwsTooManyRequestsException:{timeout:3e3},ExecuteFullError:{timeout:3e3},SystemTimeoutError:{timeout:3e3},ExpiredInQueue:{timeout:3e3},VectorIndexesUnavailable:{timeout:1e3},SearchIndexesUnavailable:{timeout:1e3},TableSummariesUnavailable:{timeout:1e3},VectorIndexTooLarge:{timeout:3e3},SearchIndexTooLarge:{timeout:3e3},TooManyWritesInTimePeriod:{timeout:3e3}};function gs(n){if(n===void 0)return"Unknown";for(let t of Object.keys(xr))if(n.startsWith(t))return t;return"Unknown"}var nt=class{constructor(t,e,r,s,i,o){this.markConnectionStateDirty=i,this.debug=o,I(this,"socket"),I(this,"connectionCount"),I(this,"_hasEverConnected",!1),I(this,"lastCloseReason"),I(this,"transitionChunkBuffer",null),I(this,"defaultInitialBackoff"),I(this,"maxBackoff"),I(this,"retries"),I(this,"serverInactivityThreshold"),I(this,"reconnectDueToServerInactivityTimeout"),I(this,"scheduledReconnect",null),I(this,"networkOnlineHandler",null),I(this,"pendingNetworkRecoveryInfo",null),I(this,"uri"),I(this,"onOpen"),I(this,"onResume"),I(this,"onMessage"),I(this,"webSocketConstructor"),I(this,"logger"),I(this,"onServerDisconnectError"),this.webSocketConstructor=r,this.socket={state:"disconnected"},this.connectionCount=0,this.lastCloseReason="InitialConnect",this.defaultInitialBackoff=1e3,this.maxBackoff=16e3,this.retries=0,this.serverInactivityThreshold=6e4,this.reconnectDueToServerInactivityTimeout=null,this.uri=t,this.onOpen=e.onOpen,this.onResume=e.onResume,this.onMessage=e.onMessage,this.onServerDisconnectError=e.onServerDisconnectError,this.logger=s,this.setupNetworkListener(),this.connect()}setSocketState(t){this.socket=t,this._logVerbose(`socket state changed: ${this.socket.state}, paused: ${"paused"in this.socket?this.socket.paused:void 0}`),this.markConnectionStateDirty()}setupNetworkListener(){typeof window>"u"||typeof window.addEventListener!="function"||this.networkOnlineHandler===null&&(this.networkOnlineHandler=()=>{this._logVerbose("network online event detected"),this.tryReconnectImmediately()},window.addEventListener("online",this.networkOnlineHandler),this._logVerbose("network online event listener registered"))}cleanupNetworkListener(){this.networkOnlineHandler&&typeof window<"u"&&typeof window.removeEventListener=="function"&&(window.removeEventListener("online",this.networkOnlineHandler),this.networkOnlineHandler=null,this._logVerbose("network online event listener removed"))}assembleTransition(t){if(t.partNumber<0||t.partNumber>=t.totalParts||t.totalParts===0||this.transitionChunkBuffer&&(this.transitionChunkBuffer.totalParts!==t.totalParts||this.transitionChunkBuffer.transitionId!==t.transitionId))throw this.transitionChunkBuffer=null,new Error("Invalid TransitionChunk");if(this.transitionChunkBuffer===null&&(this.transitionChunkBuffer={chunks:[],totalParts:t.totalParts,transitionId:t.transitionId}),t.partNumber!==this.transitionChunkBuffer.chunks.length){let e=this.transitionChunkBuffer.chunks.length;throw this.transitionChunkBuffer=null,new Error(`TransitionChunk received out of order: expected part ${e}, got ${t.partNumber}`)}if(this.transitionChunkBuffer.chunks.push(t.chunk),this.transitionChunkBuffer.chunks.length===t.totalParts){let e=this.transitionChunkBuffer.chunks.join("");this.transitionChunkBuffer=null;let r=Kt(JSON.parse(e));if(r.type!=="Transition")throw new Error(`Expected Transition, got ${r.type} after assembling chunks`);return r}return null}connect(){if(this.socket.state==="terminated")return;if(this.socket.state!=="disconnected"&&this.socket.state!=="stopped")throw new Error("Didn't start connection from disconnected state: "+this.socket.state);let t=new this.webSocketConstructor(this.uri);this._logVerbose("constructed WebSocket"),this.setSocketState({state:"connecting",ws:t,paused:"no"}),this.resetServerInactivityTimeout(),t.onopen=()=>{if(this.logger.logVerbose("begin ws.onopen"),this.socket.state!=="connecting")throw new Error("onopen called with socket not in connecting state");if(this.setSocketState({state:"ready",ws:t,paused:this.socket.paused==="yes"?"uninitialized":"no"}),this.resetServerInactivityTimeout(),this.socket.paused==="no"&&(this._hasEverConnected=!0,this.onOpen({connectionCount:this.connectionCount,lastCloseReason:this.lastCloseReason,clientTs:fe()})),this.lastCloseReason!=="InitialConnect"&&(this.lastCloseReason?this.logger.log("WebSocket reconnected at",Mr(),"after disconnect due to",this.lastCloseReason):this.logger.log("WebSocket reconnected at",Mr())),this.connectionCount+=1,this.lastCloseReason=null,this.pendingNetworkRecoveryInfo!==null){let{timeSavedMs:e}=this.pendingNetworkRecoveryInfo;this.pendingNetworkRecoveryInfo=null,this.sendMessage({type:"Event",eventType:"NetworkRecoveryReconnect",event:{timeSavedMs:e}}),this.logger.log(`Network recovery reconnect saved ~${Math.round(e/1e3)}s of waiting`)}},t.onerror=e=>{this.transitionChunkBuffer=null;let r=e.message;r&&this.logger.log(`WebSocket error message: ${r}`)},t.onmessage=e=>{this.resetServerInactivityTimeout();let r=e.data.length,s=Kt(JSON.parse(e.data));if(this._logVerbose(`received ws message with type ${s.type}`),s.type==="Ping")return;if(s.type==="TransitionChunk"){let o=this.assembleTransition(s);if(!o)return;s=o,this._logVerbose(`assembled full ws message of type ${s.type}`)}this.transitionChunkBuffer!==null&&(this.transitionChunkBuffer=null,this.logger.log(`Received unexpected ${s.type} while buffering TransitionChunks`)),s.type==="Transition"&&this.reportLargeTransition({messageLength:r,transition:s}),this.onMessage(s).hasSyncedPastLastReconnect&&(this.retries=0,this.markConnectionStateDirty())},t.onclose=e=>{if(this._logVerbose("begin ws.onclose"),this.transitionChunkBuffer=null,this.lastCloseReason===null&&(this.lastCloseReason=e.reason||`closed with code ${e.code}`),e.code!==ds&&e.code!==hs&&e.code!==ps&&e.code!==fs){let s=`WebSocket closed with code ${e.code}`;e.reason&&(s+=`: ${e.reason}`),this.logger.log(s),this.onServerDisconnectError&&e.reason&&this.onServerDisconnectError(s)}let r=gs(e.reason);this.scheduleReconnect(r)}}socketState(){return this.socket.state}sendMessage(t){let e={type:t.type,...t.type==="Authenticate"&&t.tokenType==="User"?{value:`...${t.value.slice(-7)}`}:{}};if(this.socket.state==="ready"&&this.socket.paused==="no"){let r=Ar(t),s=JSON.stringify(r),i=!1;try{this.socket.ws.send(s),i=!0}catch(o){this.logger.log(`Failed to send message on WebSocket, reconnecting: ${o}`),this.closeAndReconnect("FailedToSendMessage")}return this._logVerbose(`${i?"sent":"failed to send"} message with type ${t.type}: ${JSON.stringify(e)}`),!0}return this._logVerbose(`message not sent (socket state: ${this.socket.state}, paused: ${"paused"in this.socket?this.socket.paused:void 0}): ${JSON.stringify(e)}`),!1}resetServerInactivityTimeout(){this.socket.state!=="terminated"&&(this.reconnectDueToServerInactivityTimeout!==null&&(clearTimeout(this.reconnectDueToServerInactivityTimeout),this.reconnectDueToServerInactivityTimeout=null),this.reconnectDueToServerInactivityTimeout=setTimeout(()=>{this.closeAndReconnect("InactiveServer")},this.serverInactivityThreshold))}scheduleReconnect(t){this.scheduledReconnect&&(clearTimeout(this.scheduledReconnect.timeout),this.scheduledReconnect=null),this.socket={state:"disconnected"};let e=this.nextBackoff(t);this.markConnectionStateDirty(),this.logger.log(`Attempting reconnect in ${Math.round(e)}ms`);let r=fe(),s=setTimeout(()=>{this.scheduledReconnect?.timeout===s&&(this.scheduledReconnect=null,this.connect())},e);this.scheduledReconnect={timeout:s,scheduledAt:r,backoffMs:e}}closeAndReconnect(t){switch(this._logVerbose(`begin closeAndReconnect with reason ${t}`),this.socket.state){case"disconnected":case"terminated":case"stopped":return;case"connecting":case"ready":{this.lastCloseReason=t,this.close(),this.scheduleReconnect("client");return}default:this.socket}}close(){switch(this.transitionChunkBuffer=null,this.socket.state){case"disconnected":case"terminated":case"stopped":return Promise.resolve();case"connecting":{let t=this.socket.ws;return t.onmessage=e=>{this._logVerbose("Ignoring message received after close")},new Promise(e=>{t.onclose=()=>{this._logVerbose("Closed after connecting"),e()},t.onopen=()=>{this._logVerbose("Opened after connecting"),t.close()}})}case"ready":{this._logVerbose("ws.close called");let t=this.socket.ws;t.onmessage=r=>{this._logVerbose("Ignoring message received after close")};let e=new Promise(r=>{t.onclose=()=>{r()}});return t.close(),e}default:return this.socket,Promise.resolve()}}terminate(){switch(this.reconnectDueToServerInactivityTimeout&&clearTimeout(this.reconnectDueToServerInactivityTimeout),this.scheduledReconnect&&(clearTimeout(this.scheduledReconnect.timeout),this.scheduledReconnect=null),this.cleanupNetworkListener(),this.socket.state){case"terminated":case"stopped":case"disconnected":case"connecting":case"ready":{let t=this.close();return this.setSocketState({state:"terminated"}),t}default:throw this.socket,new Error(`Invalid websocket state: ${this.socket.state}`)}}stop(){switch(this.socket.state){case"terminated":return Promise.resolve();case"connecting":case"stopped":case"disconnected":case"ready":{this.cleanupNetworkListener();let t=this.close();return this.socket={state:"stopped"},t}default:return this.socket,Promise.resolve()}}tryRestart(){switch(this.socket.state){case"stopped":break;case"terminated":case"connecting":case"ready":case"disconnected":this.logger.logVerbose("Restart called without stopping first");return;default:this.socket}this.setupNetworkListener(),this.connect()}pause(){switch(this.socket.state){case"disconnected":case"stopped":case"terminated":return;case"connecting":case"ready":{this.socket={...this.socket,paused:"yes"};return}default:{this.socket;return}}}tryReconnectImmediately(){if(this._logVerbose("tryReconnectImmediately called"),this.socket.state!=="disconnected"){this._logVerbose(`tryReconnectImmediately called but socket state is ${this.socket.state}, no action taken`);return}let t=null;if(this.scheduledReconnect){let e=fe()-this.scheduledReconnect.scheduledAt;t=Math.max(0,this.scheduledReconnect.backoffMs-e),this._logVerbose(`would have waited ${Math.round(t)}ms more (backoff was ${Math.round(this.scheduledReconnect.backoffMs)}ms, elapsed ${Math.round(e)}ms)`),clearTimeout(this.scheduledReconnect.timeout),this.scheduledReconnect=null,this._logVerbose("canceled scheduled reconnect")}this.logger.log("Network recovery detected, reconnecting immediately"),this.pendingNetworkRecoveryInfo=t!==null?{timeSavedMs:t}:null,this.connect()}resume(){switch(this.socket.state){case"connecting":this.socket={...this.socket,paused:"no"};return;case"ready":this.socket.paused==="uninitialized"?(this.socket={...this.socket,paused:"no"},this._hasEverConnected=!0,this.onOpen({connectionCount:this.connectionCount,lastCloseReason:this.lastCloseReason,clientTs:fe()})):this.socket.paused==="yes"&&(this.socket={...this.socket,paused:"no"},this.onResume());return;case"terminated":case"stopped":case"disconnected":return;default:this.socket}this.connect()}connectionState(){return{isConnected:this.socket.state==="ready",hasEverConnected:this._hasEverConnected,connectionCount:this.connectionCount,connectionRetries:this.retries}}_logVerbose(t){this.logger.logVerbose(t)}nextBackoff(t){let r=(t==="client"?100:t==="Unknown"?this.defaultInitialBackoff:xr[t].timeout)*Math.pow(2,this.retries);this.retries+=1;let s=Math.min(r,this.maxBackoff),i=s*(Math.random()-.5);return s+i}reportLargeTransition({transition:t,messageLength:e}){if(t.clientClockSkew===void 0||t.serverTs===void 0)return;let r=fe()-t.clientClockSkew-t.serverTs/1e6,s=`${Math.round(r)}ms`,i=`${Math.round(e/1e4)/100}MB`,o=e/(r/1e3),a=`${Math.round(o/1e4)/100}MB per second`;this._logVerbose(`received ${i} transition in ${s} at ${a}`),e>2e7?this.logger.log(`received query results totaling more that 20MB (${i}) which will take a long time to download on slower connections`):r>2e4&&this.logger.log(`received query results totaling ${i} which took more than 20s to arrive (${s})`),this.debug&&this.sendMessage({type:"Event",eventType:"ClientReceivedTransition",event:{transitionTransitTime:r,messageLength:e}})}};function Rr(){return ms()}function ms(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,n=>{let t=Math.random()*16|0;return(n==="x"?t:t&3|8).toString(16)})}var ce=class extends Error{};ce.prototype.name="InvalidTokenError";function ys(n){return decodeURIComponent(atob(n).replace(/(.)/g,(t,e)=>{let r=e.charCodeAt(0).toString(16).toUpperCase();return r.length<2&&(r="0"+r),"%"+r}))}function bs(n){let t=n.replace(/-/g,"+").replace(/_/g,"/");switch(t.length%4){case 0:break;case 2:t+="==";break;case 3:t+="=";break;default:throw new Error("base64 string is not of the correct length")}try{return ys(t)}catch{return atob(t)}}function st(n,t){if(typeof n!="string")throw new ce("Invalid token specified: must be a string");t||(t={});let e=t.header===!0?0:1,r=n.split(".")[e];if(typeof r!="string")throw new ce(`Invalid token specified: missing part #${e+1}`);let s;try{s=bs(r)}catch(i){throw new ce(`Invalid token specified: invalid base64 for part #${e+1} (${i.message})`)}try{return JSON.parse(s)}catch(i){throw new ce(`Invalid token specified: invalid json for part #${e+1} (${i.message})`)}}var vs=Object.defineProperty,ws=(n,t,e)=>t in n?vs(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,D=(n,t,e)=>ws(n,typeof t!="symbol"?t+"":t,e),Es=480*60*60*1e3,_r=2,it=class{constructor(t,e,r){D(this,"authState",{state:"noAuth"}),D(this,"configVersion",0),D(this,"syncState"),D(this,"authenticate"),D(this,"stopSocket"),D(this,"tryRestartSocket"),D(this,"pauseSocket"),D(this,"resumeSocket"),D(this,"clearAuth"),D(this,"logger"),D(this,"refreshTokenLeewaySeconds"),D(this,"tokenConfirmationAttempts",0),this.syncState=t,this.authenticate=e.authenticate,this.stopSocket=e.stopSocket,this.tryRestartSocket=e.tryRestartSocket,this.pauseSocket=e.pauseSocket,this.resumeSocket=e.resumeSocket,this.clearAuth=e.clearAuth,this.logger=r.logger,this.refreshTokenLeewaySeconds=r.refreshTokenLeewaySeconds}async setConfig(t,e){this.resetAuthState(),this._logVerbose("pausing WS for auth token fetch"),this.pauseSocket();let r=await this.fetchTokenAndGuardAgainstRace(t,{forceRefreshToken:!1});r.isFromOutdatedConfig||(r.value?(this.setAuthState({state:"waitingForServerConfirmationOfCachedToken",config:{fetchToken:t,onAuthChange:e},hasRetried:!1}),this.authenticate(r.value)):(this.setAuthState({state:"initialRefetch",config:{fetchToken:t,onAuthChange:e}}),await this.refetchToken()),this._logVerbose("resuming WS after auth token fetch"),this.resumeSocket())}onTransition(t){if(this.syncState.isCurrentOrNewerAuthVersion(t.endVersion.identity)&&!(t.endVersion.identity<=t.startVersion.identity)){if(this._logVerbose(`auth state is ${this.authState.state} when handling transition`),this.syncState.markAuthCompletion(),this.authState.state==="waitingForServerConfirmationOfCachedToken"){this._logVerbose("server confirmed auth token is valid"),this.refetchToken(),this.authState.config.onAuthChange(!0);return}this.authState.state==="waitingForServerConfirmationOfFreshToken"&&(this._logVerbose("server confirmed new auth token is valid"),this.scheduleTokenRefetch(this.authState.token),this.tokenConfirmationAttempts=0,this.authState.hadAuth||this.authState.config.onAuthChange(!0))}}onAuthError(t){if(t.authUpdateAttempted===!1&&(this.authState.state==="waitingForServerConfirmationOfFreshToken"||this.authState.state==="waitingForServerConfirmationOfCachedToken")){this._logVerbose("ignoring non-auth token expired error");return}let{baseVersion:e}=t;if(!this.syncState.isCurrentOrNewerAuthVersion(e+1)){this._logVerbose("ignoring auth error for previous auth attempt");return}this.tryToReauthenticate(t)}async tryToReauthenticate(t){if(this._logVerbose(`attempting to reauthenticate: ${t.error}`),this.authState.state==="noAuth"||this.authState.state==="waitingForServerConfirmationOfFreshToken"&&this.tokenConfirmationAttempts>=_r){this.logger.error(`Failed to authenticate: "${t.error}", check your server auth config`),this.syncState.hasAuth()&&this.syncState.clearAuth(),this.authState.state!=="noAuth"&&this.setAndReportAuthFailed(this.authState.config.onAuthChange);return}this.authState.state==="waitingForServerConfirmationOfFreshToken"&&(this.tokenConfirmationAttempts++,this._logVerbose(`retrying reauthentication, ${_r-this.tokenConfirmationAttempts} attempts remaining`)),await this.stopSocket();let e=await this.fetchTokenAndGuardAgainstRace(this.authState.config.fetchToken,{forceRefreshToken:!0});e.isFromOutdatedConfig||(e.value&&this.syncState.isNewAuth(e.value)?(this.authenticate(e.value),this.setAuthState({state:"waitingForServerConfirmationOfFreshToken",config:this.authState.config,token:e.value,hadAuth:this.authState.state==="notRefetching"||this.authState.state==="waitingForScheduledRefetch"})):(this._logVerbose("reauthentication failed, could not fetch a new token"),this.syncState.hasAuth()&&this.syncState.clearAuth(),this.setAndReportAuthFailed(this.authState.config.onAuthChange)),this.tryRestartSocket())}async refetchToken(){if(this.authState.state==="noAuth")return;this._logVerbose("refetching auth token");let t=await this.fetchTokenAndGuardAgainstRace(this.authState.config.fetchToken,{forceRefreshToken:!0});t.isFromOutdatedConfig||(t.value?this.syncState.isNewAuth(t.value)?(this.setAuthState({state:"waitingForServerConfirmationOfFreshToken",hadAuth:this.syncState.hasAuth(),token:t.value,config:this.authState.config}),this.authenticate(t.value)):this.setAuthState({state:"notRefetching",config:this.authState.config}):(this._logVerbose("refetching token failed"),this.syncState.hasAuth()&&this.clearAuth(),this.setAndReportAuthFailed(this.authState.config.onAuthChange)),this._logVerbose("restarting WS after auth token fetch (if currently stopped)"),this.tryRestartSocket())}scheduleTokenRefetch(t){if(this.authState.state==="noAuth")return;let e=this.decodeToken(t);if(!e){this.logger.error("Auth token is not a valid JWT, cannot refetch the token");return}let{iat:r,exp:s}=e;if(!r||!s){this.logger.error("Auth token does not have required fields, cannot refetch the token");return}let i=s-r;if(i<=2){this.logger.error("Auth token does not live long enough, cannot refetch the token");return}let o=Math.min(Es,(i-this.refreshTokenLeewaySeconds)*1e3);o<=0&&(this.logger.warn(`Refetching auth token immediately, configured leeway ${this.refreshTokenLeewaySeconds}s is larger than the token's lifetime ${i}s`),o=0);let a=setTimeout(()=>{this._logVerbose("running scheduled token refetch"),this.refetchToken()},o);this.setAuthState({state:"waitingForScheduledRefetch",refetchTokenTimeoutId:a,config:this.authState.config}),this._logVerbose(`scheduled preemptive auth token refetching in ${o}ms`)}async fetchTokenAndGuardAgainstRace(t,e){let r=++this.configVersion;this._logVerbose(`fetching token with config version ${r}`);let s=await t(e);return this.configVersion!==r?(this._logVerbose(`stale config version, expected ${r}, got ${this.configVersion}`),{isFromOutdatedConfig:!0}):{isFromOutdatedConfig:!1,value:s}}stop(){this.resetAuthState(),this.configVersion++,this._logVerbose(`config version bumped to ${this.configVersion}`)}setAndReportAuthFailed(t){t(!1),this.resetAuthState()}resetAuthState(){this.setAuthState({state:"noAuth"})}setAuthState(t){let e=t.state==="waitingForServerConfirmationOfFreshToken"?{hadAuth:t.hadAuth,state:t.state,token:`...${t.token.slice(-7)}`}:{state:t.state};switch(this._logVerbose(`setting auth state to ${JSON.stringify(e)}`),t.state){case"waitingForScheduledRefetch":case"notRefetching":case"noAuth":this.tokenConfirmationAttempts=0;break;case"waitingForServerConfirmationOfFreshToken":case"waitingForServerConfirmationOfCachedToken":case"initialRefetch":break;default:}this.authState.state==="waitingForScheduledRefetch"&&clearTimeout(this.authState.refetchTokenTimeoutId),this.authState=t}decodeToken(t){try{return st(t)}catch(e){return this._logVerbose(`Error decoding token: ${e instanceof Error?e.message:"Unknown error"}`),null}}_logVerbose(t){this.logger.logVerbose(`${t} [v${this.configVersion}]`)}};var Ss=["convexClientConstructed","convexWebSocketOpen","convexFirstMessageReceived"];function Lr(n,t){let e={sessionId:t};typeof performance>"u"||!performance.mark||performance.mark(n,{detail:e})}function Cs(n){let t=n.name.slice(6);return t=t.charAt(0).toLowerCase()+t.slice(1),{name:t,startTime:n.startTime}}function kr(n){if(typeof performance>"u"||!performance.getEntriesByName)return[];let t=[];for(let e of Ss){let r=performance.getEntriesByName(e).filter(s=>s.entryType==="mark").filter(s=>s.detail.sessionId===n);t.push(...r)}return t.map(Cs)}var Is=Object.defineProperty,Ps=(n,t,e)=>t in n?Is(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,P=(n,t,e)=>Ps(n,typeof t!="symbol"?t+"":t,e),Pe=class{constructor(t,e,r){if(P(this,"address"),P(this,"state"),P(this,"requestManager"),P(this,"webSocketManager"),P(this,"authenticationManager"),P(this,"remoteQuerySet"),P(this,"optimisticQueryResults"),P(this,"_transitionHandlerCounter",0),P(this,"_nextRequestId"),P(this,"_onTransitionFns",new Map),P(this,"_sessionId"),P(this,"firstMessageReceived",!1),P(this,"debug"),P(this,"logger"),P(this,"maxObservedTimestamp"),P(this,"connectionStateSubscribers",new Map),P(this,"nextConnectionStateSubscriberId",0),P(this,"_lastPublishedConnectionState"),P(this,"markConnectionStateDirty",()=>{Promise.resolve().then(()=>{let h=this.connectionState();if(JSON.stringify(h)!==JSON.stringify(this._lastPublishedConnectionState)){this._lastPublishedConnectionState=h;for(let m of this.connectionStateSubscribers.values())m(h)}})}),P(this,"mark",h=>{this.debug&&Lr(h,this.sessionId)}),typeof t=="object")throw new Error("Passing a ClientConfig object is no longer supported. Pass the URL of the Convex deployment as a string directly.");r?.skipConvexDeploymentUrlCheck!==!0&&be(t),r={...r};let s=r.authRefreshTokenLeewaySeconds??10,i=r.webSocketConstructor;if(!i&&typeof WebSocket>"u")throw new Error("No WebSocket global variable defined! To use Convex in an environment without WebSocket try the HTTP client: https://docs.convex.dev/api/classes/browser.ConvexHttpClient");i=i||WebSocket,this.debug=r.reportDebugInfoToConvex??!1,this.address=t,this.logger=r.logger===!1?Wt({verbose:r.verbose??!1}):r.logger!==!0&&r.logger?r.logger:Vt({verbose:r.verbose??!1});let o=t.search("://");if(o===-1)throw new Error("Provided address was not an absolute URL.");let a=t.substring(o+3),c=t.substring(0,o),l;if(c==="http")l="ws";else if(c==="https")l="wss";else throw new Error(`Unknown parent protocol ${c}`);let d=`${l}://${a}/api/${k}/sync`;this.state=new Ke,this.remoteQuerySet=new Ie(h=>this.state.queryPath(h),this.logger),this.requestManager=new Ye(this.logger,this.markConnectionStateDirty);let g=()=>{this.webSocketManager.pause(),this.state.pause()};this.authenticationManager=new it(this.state,{authenticate:h=>{let m=this.state.setAuth(h);return this.webSocketManager.sendMessage(m),m.baseVersion},stopSocket:()=>this.webSocketManager.stop(),tryRestartSocket:()=>this.webSocketManager.tryRestart(),pauseSocket:g,resumeSocket:()=>this.webSocketManager.resume(),clearAuth:()=>{this.clearAuth()}},{logger:this.logger,refreshTokenLeewaySeconds:s}),this.optimisticQueryResults=new et,this.addOnTransitionHandler(h=>{e(h.queries.map(m=>m.token))}),this._nextRequestId=0,this._sessionId=Rr();let{unsavedChangesWarning:E}=r;if(typeof window>"u"||typeof window.addEventListener>"u"){if(E===!0)throw new Error("unsavedChangesWarning requested, but window.addEventListener not found! Remove {unsavedChangesWarning: true} from Convex client options.")}else E!==!1&&window.addEventListener("beforeunload",h=>{if(this.requestManager.hasIncompleteRequests()){h.preventDefault();let m="Are you sure you want to leave? Your changes may not be saved.";return(h||window.event).returnValue=m,m}});this.webSocketManager=new nt(d,{onOpen:h=>{this.mark("convexWebSocketOpen"),this.webSocketManager.sendMessage({...h,type:"Connect",sessionId:this._sessionId,maxObservedTimestamp:this.maxObservedTimestamp}),this.remoteQuerySet=new Ie(ue=>this.state.queryPath(ue),this.logger);let[m,F]=this.state.restart();F&&this.webSocketManager.sendMessage(F),this.webSocketManager.sendMessage(m);for(let ue of this.requestManager.restart())this.webSocketManager.sendMessage(ue)},onResume:()=>{let[h,m]=this.state.resume();m&&this.webSocketManager.sendMessage(m),h&&this.webSocketManager.sendMessage(h);for(let F of this.requestManager.resume())this.webSocketManager.sendMessage(F)},onMessage:h=>{switch(this.firstMessageReceived||(this.firstMessageReceived=!0,this.mark("convexFirstMessageReceived"),this.reportMarks()),h.type){case"Transition":{this.observedTimestamp(h.endVersion.ts),this.authenticationManager.onTransition(h),this.remoteQuerySet.transition(h),this.state.transition(h);let m=this.requestManager.removeCompleted(this.remoteQuerySet.timestamp());this.notifyOnQueryResultChanges(m);break}case"MutationResponse":{h.success&&this.observedTimestamp(h.ts);let m=this.requestManager.onResponse(h);m!==null&&this.notifyOnQueryResultChanges(new Map([[m.requestId,m.result]]));break}case"ActionResponse":{this.requestManager.onResponse(h);break}case"AuthError":{this.authenticationManager.onAuthError(h);break}case"FatalError":{let m=Er(this.logger,h.error);throw this.webSocketManager.terminate(),m}default:}return{hasSyncedPastLastReconnect:this.hasSyncedPastLastReconnect()}},onServerDisconnectError:r.onServerDisconnectError},i,this.logger,this.markConnectionStateDirty,this.debug),this.mark("convexClientConstructed"),r.expectAuth&&g()}hasSyncedPastLastReconnect(){return this.requestManager.hasSyncedPastLastReconnect()&&this.state.hasSyncedPastLastReconnect()}observedTimestamp(t){(this.maxObservedTimestamp===void 0||this.maxObservedTimestamp.lessThanOrEqual(t))&&(this.maxObservedTimestamp=t)}getMaxObservedTimestamp(){return this.maxObservedTimestamp}notifyOnQueryResultChanges(t){let e=this.remoteQuerySet.remoteQueryResults(),r=new Map;for(let[i,o]of e){let a=this.state.queryToken(i);if(a!==null){let c={result:o,udfPath:this.state.queryPath(i),args:this.state.queryArgs(i)};r.set(a,c)}}let s=this.optimisticQueryResults.ingestQueryResultsFromServer(r,new Set(t.keys()));this.handleTransition({queries:s.map(i=>{let o=this.optimisticQueryResults.rawQueryResult(i);return{token:i,modification:{kind:"Updated",result:o}}}),reflectedMutations:Array.from(t).map(([i,o])=>({requestId:i,result:o})),timestamp:this.remoteQuerySet.timestamp()})}handleTransition(t){for(let e of this._onTransitionFns.values())e(t)}addOnTransitionHandler(t){let e=this._transitionHandlerCounter++;return this._onTransitionFns.set(e,t),()=>this._onTransitionFns.delete(e)}getCurrentAuthClaims(){let t=this.state.getAuth(),e={};if(t&&t.tokenType==="User")try{e=t?st(t.value):{}}catch{e={}}else return;return{token:t.value,decoded:e}}setAuth(t,e){this.authenticationManager.setConfig(t,e)}hasAuth(){return this.state.hasAuth()}setAdminAuth(t,e){let r=this.state.setAdminAuth(t,e);this.webSocketManager.sendMessage(r)}clearAuth(){let t=this.state.clearAuth();this.webSocketManager.sendMessage(t)}subscribe(t,e,r){let s=R(e),{modification:i,queryToken:o,unsubscribe:a}=this.state.subscribe(t,s,r?.journal,r?.componentPath);return i!==null&&this.webSocketManager.sendMessage(i),{queryToken:o,unsubscribe:()=>{let c=a();c&&this.webSocketManager.sendMessage(c)}}}localQueryResult(t,e){let r=R(e),s=B(t,r);return this.optimisticQueryResults.queryResult(s)}localQueryResultByToken(t){return this.optimisticQueryResults.queryResult(t)}hasLocalQueryResultByToken(t){return this.optimisticQueryResults.hasQueryResult(t)}localQueryLogs(t,e){let r=R(e),s=B(t,r);return this.optimisticQueryResults.queryLogs(s)}queryJournal(t,e){let r=R(e),s=B(t,r);return this.state.queryJournal(s)}connectionState(){let t=this.webSocketManager.connectionState();return{hasInflightRequests:this.requestManager.hasInflightRequests(),isWebSocketConnected:t.isConnected,hasEverConnected:t.hasEverConnected,connectionCount:t.connectionCount,connectionRetries:t.connectionRetries,timeOfOldestInflightRequest:this.requestManager.timeOfOldestInflightRequest(),inflightMutations:this.requestManager.inflightMutations(),inflightActions:this.requestManager.inflightActions()}}subscribeToConnectionState(t){let e=this.nextConnectionStateSubscriberId++;return this.connectionStateSubscribers.set(e,t),()=>{this.connectionStateSubscribers.delete(e)}}async mutation(t,e,r){let s=await this.mutationInternal(t,e,r);if(!s.success)throw s.errorData!==void 0?Se(s,new j(z("mutation",t,s))):new Error(z("mutation",t,s));return s.value}async mutationInternal(t,e,r,s){let{mutationPromise:i}=this.enqueueMutation(t,e,r,s);return i}enqueueMutation(t,e,r,s){let i=R(e);this.tryReportLongDisconnect();let o=this.nextRequestId;if(this._nextRequestId++,r!==void 0){let d=r.optimisticUpdate;if(d!==void 0){let g=m=>{d(m,i)instanceof Promise&&this.logger.warn("Optimistic update handler returned a Promise. Optimistic updates should be synchronous.")},h=this.optimisticQueryResults.applyOptimisticUpdate(g,o).map(m=>{let F=this.localQueryResultByToken(m);return{token:m,modification:{kind:"Updated",result:F===void 0?void 0:{success:!0,value:F,logLines:[]}}}});this.handleTransition({queries:h,reflectedMutations:[],timestamp:this.remoteQuerySet.timestamp()})}}let a={type:"Mutation",requestId:o,udfPath:t,componentPath:s,args:[A(i)]},c=this.webSocketManager.sendMessage(a),l=this.requestManager.request(a,c);return{requestId:o,mutationPromise:l}}async action(t,e){let r=await this.actionInternal(t,e);if(!r.success)throw r.errorData!==void 0?Se(r,new j(z("action",t,r))):new Error(z("action",t,r));return r.value}async actionInternal(t,e,r){let s=R(e),i=this.nextRequestId;this._nextRequestId++,this.tryReportLongDisconnect();let o={type:"Action",requestId:i,udfPath:t,componentPath:r,args:[A(s)]},a=this.webSocketManager.sendMessage(o);return this.requestManager.request(o,a)}async close(){return this.authenticationManager.stop(),this.webSocketManager.terminate()}get url(){return this.address}get nextRequestId(){return this._nextRequestId}get sessionId(){return this._sessionId}reportMarks(){if(this.debug){let t=kr(this.sessionId);this.webSocketManager.sendMessage({type:"Event",eventType:"ClientConnect",event:t})}}tryReportLongDisconnect(){if(!this.debug)return;let t=this.connectionState().timeOfOldestInflightRequest;if(t===null||Date.now()-t.getTime()<=60*1e3)return;let e=`${this.address}/api/debug_event`;fetch(e,{method:"POST",headers:{"Content-Type":"application/json","Convex-Client":`npm-${k}`},body:JSON.stringify({event:"LongWebsocketDisconnect"})}).then(r=>{r.ok||this.logger.warn("Analytics request failed with response:",r.body)}).catch(r=>{this.logger.warn("Analytics response failed with error:",r)})}};function ot(n){if(typeof n!="object"||n===null||!Array.isArray(n.page)||typeof n.isDone!="boolean"||typeof n.continueCursor!="string")throw new Error(`Not a valid paginated query result: ${n?.toString()}`);return n}var Ts=Object.defineProperty,As=(n,t,e)=>t in n?Ts(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,Or=(n,t,e)=>As(n,typeof t!="symbol"?t+"":t,e),at=class{constructor(t,e){this.client=t,this.onTransition=e,Or(this,"paginatedQuerySet",new Map),Or(this,"lastTransitionTs"),this.lastTransitionTs=V.fromNumber(0),this.client.addOnTransitionHandler(r=>this.onBaseTransition(r))}subscribe(t,e,r){let s=q(t),i=Qt(s,e,r),o=()=>this.removePaginatedQuerySubscriber(i),a=this.paginatedQuerySet.get(i);return a?(a.numSubscribers+=1,{paginatedQueryToken:i,unsubscribe:o}):(this.paginatedQuerySet.set(i,{token:i,canonicalizedUdfPath:s,args:e,numSubscribers:1,options:{initialNumItems:r.initialNumItems},nextPageKey:0,pageKeys:[],pageKeyToQuery:new Map,ongoingSplits:new Map,skip:!1,id:r.id}),this.addPageToPaginatedQuery(i,null,r.initialNumItems),{paginatedQueryToken:i,unsubscribe:o})}localQueryResult(t,e,r){let s=q(t),i=Qt(s,e,r);return this.localQueryResultByToken(i)}localQueryResultByToken(t){let e=this.paginatedQuerySet.get(t);if(!e)return;let r=this.activePageQueryTokens(e);if(r.length===0)return{results:[],status:"LoadingFirstPage",loadMore:c=>this.loadMoreOfPaginatedQuery(t,c)};let s=[],i=!1,o=!1;for(let c of r){let l=this.client.localQueryResultByToken(c);if(l===void 0){i=!0,o=!1;continue}let d=ot(l);s=s.concat(d.page),o=!!d.isDone}let a;return i?a=s.length===0?"LoadingFirstPage":"LoadingMore":o?a="Exhausted":a="CanLoadMore",{results:s,status:a,loadMore:c=>this.loadMoreOfPaginatedQuery(t,c)}}onBaseTransition(t){let e=t.queries.map(o=>o.token),r=this.queriesContainingTokens(e),s=[];r.length>0&&(this.processPaginatedQuerySplits(r,o=>this.client.localQueryResultByToken(o)),s=r.map(o=>({token:o,modification:{kind:"Updated",result:this.localQueryResultByToken(o)}})));let i={...t,paginatedQueries:s};this.onTransition(i)}loadMoreOfPaginatedQuery(t,e){this.mustGetPaginatedQuery(t);let r=this.queryTokenForLastPageOfPaginatedQuery(t),s=this.client.localQueryResultByToken(r);if(!s)return!1;let i=ot(s);if(i.isDone)return!1;this.addPageToPaginatedQuery(t,i.continueCursor,e);let o={timestamp:this.lastTransitionTs,reflectedMutations:[],queries:[],paginatedQueries:[{token:t,modification:{kind:"Updated",result:this.localQueryResultByToken(t)}}]};return this.onTransition(o),!0}queriesContainingTokens(t){if(t.length===0)return[];let e=[],r=new Set(t);for(let[s,i]of this.paginatedQuerySet)for(let o of this.allQueryTokens(i))if(r.has(o)){e.push(s);break}return e}processPaginatedQuerySplits(t,e){for(let r of t){let s=this.mustGetPaginatedQuery(r),{ongoingSplits:i,pageKeyToQuery:o,pageKeys:a}=s;for(let[c,[l,d]]of i)e(o.get(l).queryToken)!==void 0&&e(o.get(d).queryToken)!==void 0&&this.completePaginatedQuerySplit(s,c,l,d);for(let c of a){if(i.has(c))continue;let l=o.get(c).queryToken,d=e(l);if(!d)continue;let g=ot(d);g.splitCursor&&(g.pageStatus==="SplitRecommended"||g.pageStatus==="SplitRequired"||g.page.length>s.options.initialNumItems*2)&&this.splitPaginatedQueryPage(s,c,g.splitCursor,g.continueCursor)}}}splitPaginatedQueryPage(t,e,r,s){let i=t.nextPageKey++,o=t.nextPageKey++,a={cursor:s,numItems:t.options.initialNumItems,id:t.id},c=this.client.subscribe(t.canonicalizedUdfPath,{...t.args,paginationOpts:{...a,cursor:null,endCursor:r}});t.pageKeyToQuery.set(i,c);let l=this.client.subscribe(t.canonicalizedUdfPath,{...t.args,paginationOpts:{...a,cursor:r,endCursor:s}});t.pageKeyToQuery.set(o,l),t.ongoingSplits.set(e,[i,o])}addPageToPaginatedQuery(t,e,r){let s=this.mustGetPaginatedQuery(t),i=s.nextPageKey++,o={cursor:e,numItems:r,id:s.id},a={...s.args,paginationOpts:o},c=this.client.subscribe(s.canonicalizedUdfPath,a);return s.pageKeys.push(i),s.pageKeyToQuery.set(i,c),c}removePaginatedQuerySubscriber(t){let e=this.paginatedQuerySet.get(t);if(e&&(e.numSubscribers-=1,!(e.numSubscribers>0))){for(let r of e.pageKeyToQuery.values())r.unsubscribe();this.paginatedQuerySet.delete(t)}}completePaginatedQuerySplit(t,e,r,s){let i=t.pageKeyToQuery.get(e);t.pageKeyToQuery.delete(e);let o=t.pageKeys.indexOf(e);t.pageKeys.splice(o,1,r,s),t.ongoingSplits.delete(e),i.unsubscribe()}activePageQueryTokens(t){return t.pageKeys.map(e=>t.pageKeyToQuery.get(e).queryToken)}allQueryTokens(t){return Array.from(t.pageKeyToQuery.values()).map(e=>e.queryToken)}queryTokenForLastPageOfPaginatedQuery(t){let e=this.mustGetPaginatedQuery(t),r=e.pageKeys[e.pageKeys.length-1];if(r===void 0)throw new Error(`No pages for paginated query ${t}`);return e.pageKeyToQuery.get(r).queryToken}mustGetPaginatedQuery(t){let e=this.paginatedQuerySet.get(t);if(!e)throw new Error("paginated query no longer exists for token "+t);return e}};var Ms=Object.defineProperty,xs=(n,t,e)=>t in n?Ms(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,ge=(n,t,e)=>xs(n,typeof t!="symbol"?t+"":t,e),Dr;var Te=class{constructor(t,e={}){ge(this,"listeners"),ge(this,"_client"),ge(this,"_paginatedClient"),ge(this,"callNewListenersWithCurrentValuesTimer"),ge(this,"_closed"),ge(this,"_disabled"),e.skipConvexDeploymentUrlCheck!==!0&&be(t);let{disabled:r,...s}=e;this._closed=!1,this._disabled=!!r,Dr&&!("webSocketConstructor"in s)&&typeof WebSocket>"u"&&(s.webSocketConstructor=Dr),typeof window>"u"&&!("unsavedChangesWarning"in s)&&(s.unsavedChangesWarning=!1),this.disabled||(this._client=new Pe(t,()=>{},s),this._paginatedClient=new at(this._client,i=>this._transition(i))),this.listeners=new Set}get closed(){return this._closed}get client(){if(this._client)return this._client;throw new Error("ConvexClient is disabled")}get paginatedClient(){if(this._paginatedClient)return this._paginatedClient;throw new Error("ConvexClient is disabled")}get disabled(){return this._disabled}onUpdate(t,e,r,s){if(this.disabled)return this.createDisabledUnsubscribe();let{queryToken:i,unsubscribe:o}=this.client.subscribe(L(t),e),a={queryToken:i,callback:r,onError:s,unsubscribe:o,hasEverRun:!1,query:t,args:e,paginationOptions:void 0};this.listeners.add(a),this.queryResultReady(i)&&this.callNewListenersWithCurrentValuesTimer===void 0&&(this.callNewListenersWithCurrentValuesTimer=setTimeout(()=>this.callNewListenersWithCurrentValues(),0));let c={unsubscribe:()=>{this.closed||(this.listeners.delete(a),o())},getCurrentValue:()=>this.client.localQueryResultByToken(i),getQueryLogs:()=>this.client.localQueryLogs(i)},l=c.unsubscribe;return Object.assign(l,c),l}onPaginatedUpdate_experimental(t,e,r,s,i){if(this.disabled)return this.createDisabledUnsubscribe();let o={initialNumItems:r.initialNumItems,id:-1},{paginatedQueryToken:a,unsubscribe:c}=this.paginatedClient.subscribe(L(t),e,o),l={queryToken:a,callback:s,onError:i,unsubscribe:c,hasEverRun:!1,query:t,args:e,paginationOptions:o};this.listeners.add(l),this.paginatedClient.localQueryResultByToken(a)&&this.callNewListenersWithCurrentValuesTimer===void 0&&(this.callNewListenersWithCurrentValuesTimer=setTimeout(()=>this.callNewListenersWithCurrentValues(),0));let d={unsubscribe:()=>{this.closed||(this.listeners.delete(l),c())},getCurrentValue:()=>this.paginatedClient.localQueryResult(L(t),e,o),getQueryLogs:()=>[]},g=d.unsubscribe;return Object.assign(g,d),g}callNewListenersWithCurrentValues(){this.callNewListenersWithCurrentValuesTimer=void 0,this._transition({queries:[],paginatedQueries:[]},!0)}queryResultReady(t){return this.client.hasLocalQueryResultByToken(t)}createDisabledUnsubscribe(){let t=(()=>{});return Object.assign(t,{unsubscribe:t,getCurrentValue:()=>{},getQueryLogs:()=>{}}),t}async close(){if(!this.disabled)return this.listeners.clear(),this._closed=!0,this._paginatedClient&&(this._paginatedClient=void 0),this.client.close()}getAuth(){if(!this.disabled)return this.client.getCurrentAuthClaims()}setAuth(t,e){this.disabled||this.client.setAuth(t,e??(()=>{}))}setAdminAuth(t,e){if(this.closed)throw new Error("ConvexClient has already been closed.");this.disabled||this.client.setAdminAuth(t,e)}_transition({queries:t,paginatedQueries:e},r=!1){let s=[...t.map(i=>i.token),...e.map(i=>i.token)];for(let i of this.listeners){let{callback:o,queryToken:a,onError:c,hasEverRun:l}=i,d=Sr(a),g=d?!!this.paginatedClient.localQueryResultByToken(a):this.client.hasLocalQueryResultByToken(a);if(s.includes(a)||r&&!l&&g){i.hasEverRun=!0;let E;try{d?E=this.paginatedClient.localQueryResultByToken(a):E=this.client.localQueryResultByToken(a)}catch(h){if(!(h instanceof Error))throw h;c?c(h,"Second argument to onUpdate onError is reserved for later use"):Promise.reject(h);continue}o(E,"Second argument to onUpdate callback is reserved for later use")}}}async mutation(t,e,r){if(this.disabled)throw new Error("ConvexClient is disabled");return await this.client.mutation(L(t),e,r)}async action(t,e){if(this.disabled)throw new Error("ConvexClient is disabled");return await this.client.action(L(t),e)}async query(t,e){if(this.disabled)throw new Error("ConvexClient is disabled");let r=this.client.localQueryResult(L(t),e);return r!==void 0?Promise.resolve(r):new Promise((s,i)=>{let{unsubscribe:o}=this.onUpdate(t,e,a=>{o(),s(a)},a=>{o(),i(a)})})}connectionState(){if(this.disabled)throw new Error("ConvexClient is disabled");return this.client.connectionState()}subscribeToConnectionState(t){return this.disabled?()=>{}:this.client.subscribeToConnectionState(t)}};var Yt={GODOT:"GODOT",UNITY:"UNITY",UNREAL:"UNREAL",JSDOS:"JSDOS",RUFFLE:"RUFFLE",RENPY:"RENPY",CUSTOM:"CUSTOM"},Ur=24,aa={MAX_PLAYERS:100,EXPIRY_HOURS:Ur,EXPIRY_MS:Ur*60*60*1e3},ct={PUBLIC:0,FRIENDS_ONLY:1,PRIVATE:2},ut=500,lt={ASC:0,DESC:1},dt={NUMERIC:0,TIME_SECONDS:1,TIME_MILLISECONDS:2,TIME_GAME_TICKS:3},Ae={SCREENSHOT:0,VIDEO:1,COMMUNITY:2,GAME_MANAGED:3,OTHER:4},Me={PUBLIC:0,PRIVATE:2};var ca={DEFAULT_LIMIT_BYTES:1024*1024*1024,UPLOADS_PER_MINUTE:30,UPLOADS_PER_HOUR:300},H={OFFER:"offer",ANSWER:"answer",ICE_CANDIDATE:"ice-candidate"};var w={PROGRESS_UPDATE:"ProgressUpdate",LOADING_COMPLETE:"LoadingComplete",TOGGLE_OVERLAY:"ToggleOverlay",TAKE_FOCUS:"TakeFocus",LOBBY_JOINED:"LobbyJoined",LOBBY_LEFT:"LobbyLeft",GET_LOBBY_INVITE_LINK:"GetLobbyInviteLink",GET_DEVICE_FINGERPRINT:"GetDeviceFingerprint",SET_FULLSCREEN:"SetFullscreen",TOGGLE_FULLSCREEN:"ToggleFullscreen",FULLSCREEN_CHANGED:"FullscreenChanged",SET_MUTE:"SetMute",TOGGLE_MUTE:"ToggleMute",MUTE_CHANGED:"MuteChanged",OVERLAY_CHANGED:"OverlayChanged",GAMEPLAY_JWT_READY:"GameplayJwtReady",END_SESSION:"EndSession",TRIGGER_PAYWALL:"TriggerPaywall"},Zt={EMBED_CONFIGURE:"embed.configure",EMBED_CONFIGURE_ACK:"embed.configure:ack",EMBED_CREDS_REQUEST:"embed.creds-request",EMBED_CREDS_RESPONSE:"embed.creds-response"};var xe={RedirectUrl:"rdurl",EntrypointParams:"entrypointparams",Entrypoint:"entrypoint",Engine:"engine",EngineVersion:"engineversion",PlayKey:"pk",SdkConfig:"sdkconfig",Caller:"caller"},Nr={Wavedash:"wavedash"},zt=3e4,Re={CLIENT_INTERVAL_MS:zt,CLIENT_REESTABLISH_THRESHOLD_MS:zt*2.5,CLIENT_GRACE_MS:zt/6};var ru=p.object({numItems:p.number(),cursor:p.union(p.string(),p.null()),endCursor:p.optional(p.union(p.string(),p.null())),id:p.optional(p.number()),maximumRowsRead:p.optional(p.number()),maximumBytesRead:p.optional(p.number())});var ks=Symbol("var.requestId"),Os=Symbol("var.ip"),Ds=Symbol("var.userAgent"),Us=Symbol("var.now"),Ns={[ks]:"requestId",[Os]:"ip",[Ds]:"userAgent",[Us]:"now"};var Fs=Object.defineProperty,$s=(n,t,e)=>t in n?Fs(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,Q=(n,t,e)=>$s(n,typeof t!="symbol"?t+"":t,e),ht=class{constructor(t){Q(this,"indexes"),Q(this,"stagedDbIndexes"),Q(this,"searchIndexes"),Q(this,"stagedSearchIndexes"),Q(this,"vectorIndexes"),Q(this,"stagedVectorIndexes"),Q(this,"validator"),this.indexes=[],this.stagedDbIndexes=[],this.searchIndexes=[],this.stagedSearchIndexes=[],this.vectorIndexes=[],this.stagedVectorIndexes=[],this.validator=t}" indexes"(){return this.indexes}index(t,e){return Array.isArray(e)?this.indexes.push({indexDescriptor:t,fields:e}):e.staged?this.stagedDbIndexes.push({indexDescriptor:t,fields:e.fields}):this.indexes.push({indexDescriptor:t,fields:e.fields}),this}searchIndex(t,e){return e.staged?this.stagedSearchIndexes.push({indexDescriptor:t,searchField:e.searchField,filterFields:e.filterFields||[]}):this.searchIndexes.push({indexDescriptor:t,searchField:e.searchField,filterFields:e.filterFields||[]}),this}vectorIndex(t,e){return e.staged?this.stagedVectorIndexes.push({indexDescriptor:t,vectorField:e.vectorField,dimensions:e.dimensions,filterFields:e.filterFields||[]}):this.vectorIndexes.push({indexDescriptor:t,vectorField:e.vectorField,dimensions:e.dimensions,filterFields:e.filterFields||[]}),this}self(){return this}export(){let t=this.validator.json;if(typeof t!="object")throw new Error("Invalid validator: please make sure that the parameter of `defineTable` is valid (see https://docs.convex.dev/database/schemas)");return{indexes:this.indexes,stagedDbIndexes:this.stagedDbIndexes,searchIndexes:this.searchIndexes,stagedSearchIndexes:this.stagedSearchIndexes,vectorIndexes:this.vectorIndexes,stagedVectorIndexes:this.stagedVectorIndexes,documentType:t}}};function er(n){return Bt(n)?new ht(n):new ht(p.object(n))}var tr=class{constructor(t,e){Q(this,"tables"),Q(this,"strictTableNameTypes"),Q(this,"schemaValidation"),this.tables=t,this.schemaValidation=e?.schemaValidation===void 0?!0:e.schemaValidation}export(){return JSON.stringify({tables:Object.entries(this.tables).map(([t,e])=>{let{indexes:r,stagedDbIndexes:s,searchIndexes:i,stagedSearchIndexes:o,vectorIndexes:a,stagedVectorIndexes:c,documentType:l}=e.export();return{tableName:t,indexes:r,stagedDbIndexes:s,searchIndexes:i,stagedSearchIndexes:o,vectorIndexes:a,stagedVectorIndexes:c,documentType:l}}),schemaValidation:this.schemaValidation})}};function Fr(n,t){return new tr(n,t)}var Mu=Fr({_scheduled_functions:er({name:p.string(),args:p.array(p.any()),scheduledTime:p.float64(),completedTime:p.optional(p.float64()),state:p.union(p.object({kind:p.literal("pending")}),p.object({kind:p.literal("inProgress")}),p.object({kind:p.literal("success")}),p.object({kind:p.literal("failed"),error:p.string()}),p.object({kind:p.literal("canceled")}))}),_storage:er({sha256:p.string(),size:p.float64(),contentType:p.optional(p.string())})});var f=jt;var me={KICKED:"KICKED",ERROR:"ERROR"},Le={JOINED:"JOINED",LEFT:"LEFT"},$r={QUEUE_FULL:"QUEUE_FULL",PAYLOAD_TOO_LARGE:"PAYLOAD_TOO_LARGE",INVALID_PAYLOAD_SIZE:"INVALID_PAYLOAD_SIZE",INVALID_CHANNEL:"INVALID_CHANNEL",MALFORMED:"MALFORMED",PEER_NOT_READY:"PEER_NOT_READY"},pt={SMALL:64,MEDIUM:128,LARGE:256};var b={LOBBY_MESSAGE:"LobbyMessage",LOBBY_JOINED:"LobbyJoined",LOBBY_KICKED:"LobbyKicked",LOBBY_USERS_UPDATED:"LobbyUsersUpdated",LOBBY_DATA_UPDATED:"LobbyDataUpdated",LOBBY_INVITE:"LobbyInvite",P2P_CONNECTION_ESTABLISHED:"P2PConnectionEstablished",P2P_CONNECTION_FAILED:"P2PConnectionFailed",P2P_PEER_DISCONNECTED:"P2PPeerDisconnected",P2P_PEER_RECONNECTING:"P2PPeerReconnecting",P2P_PEER_RECONNECTED:"P2PPeerReconnected",P2P_PACKET_DROPPED:"P2PPacketDropped",STATS_STORED:"StatsStored",BACKEND_CONNECTED:"BackendConnected",BACKEND_DISCONNECTED:"BackendDisconnected",BACKEND_RECONNECTING:"BackendReconnecting",FULLSCREEN_CHANGED:"FullscreenChanged",MUTE_CHANGED:"MuteChanged"};var v=class{constructor(t){this.sdk=t}destroy(){}};var rr=class{constructor(){this.set=new Set}add(t){for(let e of this.set)if(e.deref()===t)return;this.set.add(new WeakRef(t))}forEach(t){for(let e of this.set){let r=e.deref();r===void 0?this.set.delete(e):t(r)}}clear(){this.set.clear()}},ft=class extends v{constructor(e){super(e);this._isMuted=!1;this.contexts=new Map;this.elements=new rr;this.intendedMuted=new WeakMap;this.intendedUtteranceVolume=new WeakMap;this.originalAudioContext=null;this.originalWebKitAudioContext=null;this.originalAudio=null;this.originalMutedDescriptor=null;this.originalPlay=null;this.originalSpeak=null;this.originalUtteranceVolumeDescriptor=null;this.mutationObserver=null;this.handleMute=e=>{if(this._isMuted===e.isMuted)return;this._isMuted=e.isMuted;let r=this._isMuted?0:1;this.contexts.forEach((i,o)=>{let a=o.currentTime;i.gain.cancelScheduledValues(a),i.gain.setValueAtTime(i.gain.value,a),i.gain.linearRampToValueAtTime(r,a+.05)});let s=this.originalMutedDescriptor?.set;s&&this.elements.forEach(i=>{let o=this.intendedMuted.get(i)??!1;s.call(i,this._isMuted?!0:o)}),this.sdk.gameEventManager.notifyGame(b.MUTE_CHANGED,{isMuted:this._isMuted})};this.installShims(),this.sdk.iframeMessenger.addEventListener(w.MUTE_CHANGED,this.handleMute)}isMuted(){return this._isMuted}async requestMute(e){return(await this.sdk.iframeMessenger.requestFromParent(w.SET_MUTE,{muted:e})).success}async toggleMute(){return(await this.sdk.iframeMessenger.requestFromParent(w.TOGGLE_MUTE)).success}trackElement(e){if(this.intendedMuted.has(e))return;let r=this.originalMutedDescriptor?.get,s=this.originalMutedDescriptor?.set,i=r?r.call(e):e.muted;this.intendedMuted.set(e,i),this.elements.add(e),this._isMuted&&!i&&s&&s.call(e,!0)}installShims(){if(typeof window>"u")return;window.AudioContext&&(this.originalAudioContext=window.AudioContext,window.AudioContext=this.shimAudioContextClass(window.AudioContext));let e=window;if(e.webkitAudioContext&&(this.originalWebKitAudioContext=e.webkitAudioContext,e.webkitAudioContext=this.shimAudioContextClass(e.webkitAudioContext)),window.Audio){let i=window.Audio;this.originalAudio=i,(o=>{let a=function(c){let l=new i(c);return o.trackElement(l),l};a.prototype=i.prototype,window.Audio=a})(this)}typeof document<"u"&&(document.querySelectorAll("audio, video").forEach(i=>{this.trackElement(i)}),this.mutationObserver=new MutationObserver(i=>{for(let o of i)o.addedNodes.forEach(a=>{a instanceof HTMLMediaElement?this.trackElement(a):a instanceof HTMLElement&&a.querySelectorAll("audio, video").forEach(c=>{this.trackElement(c)})})}),this.mutationObserver.observe(document.documentElement,{childList:!0,subtree:!0})),this.originalMutedDescriptor=Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype,"muted")??null;let r=this.originalMutedDescriptor;r?.get&&r?.set&&(i=>{Object.defineProperty(HTMLMediaElement.prototype,"muted",{configurable:!0,get(){let o=i.intendedMuted.get(this);return o!==void 0?o:r.get.call(this)},set(o){i.intendedMuted.set(this,o),i.elements.add(this),r.set.call(this,i._isMuted?!0:o)}})})(this);let s=HTMLMediaElement.prototype.play;this.originalPlay=s,(i=>{HTMLMediaElement.prototype.play=function(){return i.trackElement(this),s.call(this)}})(this),this.shimSpeechSynthesis()}shimSpeechSynthesis(){if(!window.speechSynthesis||typeof SpeechSynthesisUtterance>"u")return;this.originalUtteranceVolumeDescriptor=Object.getOwnPropertyDescriptor(SpeechSynthesisUtterance.prototype,"volume")??null;let e=this.originalUtteranceVolumeDescriptor;e?.get&&e?.set&&(i=>{Object.defineProperty(SpeechSynthesisUtterance.prototype,"volume",{configurable:!0,get(){let o=i.intendedUtteranceVolume.get(this);return o!==void 0?o:e.get.call(this)},set(o){i.intendedUtteranceVolume.set(this,o),e.set.call(this,o)}})})(this);let r=window.speechSynthesis,s=r.speak;this.originalSpeak=s,(i=>{r.speak=function(o){if(i._isMuted){if(!i.intendedUtteranceVolume.has(o)){let a=e?.get?e.get.call(o):o.volume;i.intendedUtteranceVolume.set(o,a)}e?.set?e.set.call(o,0):o.volume=0}else{let a=i.intendedUtteranceVolume.get(o);a!==void 0&&(e?.set?e.set.call(o,a):o.volume=a,i.intendedUtteranceVolume.delete(o))}return s.call(r,o)}})(this)}shimAudioContextClass(e){return(r=>class extends e{constructor(s){super(s);let i=this.createGain();i.connect(this.destination),i.gain.setValueAtTime(r._isMuted?0:1,this.currentTime),Object.defineProperty(this,"destination",{configurable:!0,get(){return i}}),r.contexts.set(this,i)}close(){return r.contexts.delete(this),super.close()}})(this)}destroy(){if(this.sdk.iframeMessenger.removeEventListener(w.MUTE_CHANGED,this.handleMute),this.mutationObserver&&(this.mutationObserver.disconnect(),this.mutationObserver=null),typeof window<"u"){this.originalAudioContext&&(window.AudioContext=this.originalAudioContext);let e=window;this.originalWebKitAudioContext&&e.webkitAudioContext&&(e.webkitAudioContext=this.originalWebKitAudioContext),this.originalAudio&&(window.Audio=this.originalAudio),this.originalSpeak&&window.speechSynthesis&&(window.speechSynthesis.speak=this.originalSpeak)}this.originalUtteranceVolumeDescriptor&&typeof SpeechSynthesisUtterance<"u"&&Object.defineProperty(SpeechSynthesisUtterance.prototype,"volume",this.originalUtteranceVolumeDescriptor),this.originalPlay&&(HTMLMediaElement.prototype.play=this.originalPlay),this.originalMutedDescriptor&&Object.defineProperty(HTMLMediaElement.prototype,"muted",this.originalMutedDescriptor),this.contexts.clear(),this.elements.clear(),this.intendedMuted=new WeakMap,this.intendedUtteranceVolume=new WeakMap,super.destroy()}};var qr="/userfs",Z="FILE_DATA";async function Br(n,t){return new Promise((e,r)=>{let s=indexedDB.open(qr);s.onerror=()=>r(s.error),s.onupgradeneeded=i=>{let o=i.target.result;o.objectStoreNames.contains(Z)||o.createObjectStore(Z)},s.onsuccess=()=>{let i=s.result,o=i.transaction(Z,"readwrite"),a=o.objectStore(Z),c={contents:t,timestamp:Date.now(),mode:33206},l=a.put(c,n);l.onsuccess=()=>e(),l.onerror=()=>r(l.error),o.oncomplete=()=>i.close()}})}async function Gr(n){return new Promise((t,e)=>{let r=indexedDB.open(qr);r.onerror=()=>e(r.error),r.onupgradeneeded=s=>{let i=s.target.result;i.objectStoreNames.contains(Z)||i.createObjectStore(Z)},r.onsuccess=()=>{let s=r.result,i=s.transaction(Z,"readonly"),a=i.objectStore(Z).get(n);a.onsuccess=()=>t(a.result),a.onerror=()=>e(a.error),i.oncomplete=()=>s.close()}})}function Vr(n){if(n==null)throw new Error("File not found in IndexedDB");if("contents"in n&&n.contents!=null){let t=n.contents instanceof Uint8Array||n.contents instanceof Int8Array?n.contents:new Uint8Array(n.contents);return new Blob([t],{type:"application/octet-stream"})}if(n instanceof Blob)return n;if(n instanceof Uint8Array||n instanceof Int8Array)return new Blob([n],{type:"application/octet-stream"});if(n instanceof ArrayBuffer)return new Blob([n],{type:"application/octet-stream"});if("data"in n&&n.data instanceof ArrayBuffer)return new Blob([n.data],{type:"application/octet-stream"});if("blob"in n&&n.blob instanceof Blob)return n.blob;throw new Error("Unrecognized value shape from IndexedDB")}var X={DEBUG:0,INFO:1,WARN:2,ERROR:3},nr=class{constructor(t=X.WARN){this.logLevel=t}setLogLevel(t){this.logLevel=t}debug(t,...e){this.logLevel<=X.DEBUG&&console.log(`[WavedashJS] ${t}`,...e)}info(t,...e){this.logLevel<=X.INFO&&console.log(`[WavedashJS] ${t}`,...e)}warn(t,...e){this.logLevel<=X.WARN&&console.warn(`[WavedashJS] ${t}`,...e)}error(t,...e){this.logLevel<=X.ERROR&&console.error(`[WavedashJS] ${t}`,...e)}},u=new nr;var Wr="userfs",Qr="/idbfs/wavedash",gt=class extends v{constructor(t){super(t)}toRemoteKey(t){let e=this.sdk.engineInstance?.unityPersistentDataPath,r=e?t.replace(e,Qr):t,s=r.startsWith("/")?r.slice(1):r;return`${this.sdk.gameCloudId}/${Wr}/${this.sdk.wavedashUser.id}/${s}`}toLocalPath(t){let e=`${this.sdk.gameCloudId}/${Wr}/${this.sdk.wavedashUser.id}/`,r=t.startsWith(e)?"/"+t.slice(e.length):t,s=this.sdk.engineInstance?.unityPersistentDataPath;return s?r.replace(Qr,s):r}async uploadRemoteFile(t){let e=await this.sdk.convexClient.mutation(f.sdk.remoteFileStorage.getUploadUrl,{path:this.toRemoteKey(t)});if(!await this.upload(e,t))throw new Error(`Failed to upload file: ${t}`);return t}async deleteRemoteFile(t){let e=this.getRemoteStorageUrl(t),r=await this.sdk.ensureGameplayJwt(),s=await fetch(e,{method:"DELETE",headers:{Authorization:`Bearer ${r}`}});if(!s.ok){let i=`Failed to delete remote file ${t}: ${s.status} (${s.statusText})`;throw u.error(i),new Error(i)}return t}async downloadRemoteFile(t){let e=this.getRemoteStorageUrl(t);return await this.download(e,t),t}async remoteFileExists(t){let e=this.getRemoteStorageUrl(t),r=await this.sdk.ensureGameplayJwt(),s=await fetch(e,{method:"HEAD",headers:{Authorization:`Bearer ${r}`}});if(s.status===404)return!1;if(s.ok)return!0;throw new Error(`${s.status} (${s.statusText})`)}async listRemoteDirectory(t){let e=this.getRemoteStorageUrl(t)+"?list=true",r=await this.sdk.ensureGameplayJwt(),s=await fetch(e,{method:"GET",headers:{Authorization:`Bearer ${r}`}});if(s.status===404)return[];if(!s.ok)throw new Error(`${s.status} (${s.statusText})`);return(await s.json()).files.filter(o=>!o.key.endsWith("/")).map(o=>({...o,key:this.toLocalPath(o.key)}))}async downloadRemoteDirectory(t){let r=(await this.listRemoteDirectory(t)).map(async o=>{let a=this.getRemoteStorageUrl(o.key);try{return await this.download(a,o.key),{fileName:o.name,success:!0}}catch(c){let l=c instanceof Error?c.message:String(c);return u.error(`Failed to download ${o.name}: ${l}`),{fileName:o.name,success:!1}}}),i=(await Promise.all(r)).filter(o=>!o.success);if(i.length>0)throw new Error(`Failed to download ${i.length} files: ${i.map(o=>o.fileName).join(", ")}`);return t}async writeLocalFile(t,e){if(u.debug(`Writing local file: ${t}`),this.sdk.engineInstance?.FS)return u.error(`${this.sdk.engineInstance.type} engine detected, use engine's builtin file access to save files.`),!1;try{return await Br(t,e),!0}catch(r){return u.error(`Failed to write local file: ${r}`),!1}}async readLocalFile(t){if(u.debug(`Reading local file: ${t}`),this.sdk.engineInstance?.FS)return u.error(`${this.sdk.engineInstance.type} engine detected, use engine's builtin file access to read files.`),null;try{let e=await this.readLocalFileBlob(t);if(!e)return null;let r=await e.arrayBuffer();return new Uint8Array(r)}catch(e){return u.error(`Failed to read local file: ${e}`),null}}async upload(t,e){if(u.debug(`Uploading ${e} to: ${t}`),this.sdk.engineInstance&&!this.sdk.engineInstance.FS)return u.error("Engine instance is missing the Emscripten FS API"),!1;let r=!1;return this.sdk.engineInstance?r=await this.uploadFromFS(t,e):r=await this.uploadFromIndexedDb(t,e),r}async download(t,e){if(u.debug(`Downloading ${e} from: ${t}`),this.sdk.engineInstance&&!this.sdk.engineInstance.FS)throw new Error("Engine instance is missing the Emscripten FS API");let r=await this.sdk.ensureGameplayJwt(),s=await fetch(t,{method:"GET",headers:{Authorization:`Bearer ${r}`}});if(!s.ok)throw new Error(`${s.status} (${s.statusText})`);let o=await(await s.blob()).arrayBuffer(),a=new Uint8Array(o);if(this.sdk.engineInstance){let c=e.substring(0,e.lastIndexOf("/"));if(c)try{this.sdk.engineInstance.FS.mkdirTree(c)}catch{}try{this.sdk.engineInstance.FS.writeFile(e,a)}catch(l){let d=l instanceof Error?l.message:String(l);throw new Error(`Failed to save file ${e} to engine FS: ${d}`)}}else if(!await this.writeLocalFile(e,a))throw new Error(`Failed to save file ${e} to local IndexedDB storage`);u.debug(`Successfully saved to: ${e}`)}getRemoteStorageOrigin(){if(this.remoteStorageOrigin)return this.remoteStorageOrigin;if(this.sdk.config?.remoteStorageOrigin)return this.remoteStorageOrigin=this.sdk.config.remoteStorageOrigin,this.remoteStorageOrigin;if(this.sdk.ugcHost)return this.remoteStorageOrigin=this.sdk.ugcHost.startsWith("http")?this.sdk.ugcHost:`https://${this.sdk.ugcHost}`,this.remoteStorageOrigin;if(typeof window<"u"&&window.location){let e=window.location.hostname.split(".");return this.remoteStorageOrigin=`${window.location.protocol}//ugc.`+e.slice(2).join("."),this.remoteStorageOrigin}throw new Error("Remote storage origin cannot be determined.")}getRemoteStorageUrl(t){return`${this.getRemoteStorageOrigin()}/${this.toRemoteKey(t)}`}async uploadFromIndexedDb(t,e){try{let r=await this.readLocalFileBlob(e);return r?(await fetch(t,{method:"PUT",body:r})).ok:(u.error(`File not found in IndexedDB: ${e}`),!1)}catch(r){return u.error(`Error uploading from IndexedDB: ${r}`),!1}}async uploadFromFS(t,e){try{if(!this.sdk.engineInstance.FS.analyzePath(e).exists)throw new Error(`File not found in FS: ${e}`);let s=this.sdk.engineInstance.FS.readFile(e),i=new Blob([s],{type:"application/octet-stream"});return(await fetch(t,{method:"PUT",body:i})).ok}catch(r){let s=r instanceof Error?r.message:String(r);return u.error(`Error uploading from FS: ${s}`),!1}}async readLocalFileBlob(t){if(u.debug(`Reading local file (blob): ${t}`),this.sdk.engineInstance?.FS)return u.error(`${this.sdk.engineInstance.type} engine detected, use engine's builtin file access to read files.`),null;try{let e=await Gr(t);return e?Vr(e):null}catch(e){return u.error(`Failed to read local file blob: ${e}`),null}}};function jr(n,t,e){if(n.startsWith("http://")||n.startsWith("https://"))return n;let r=e?Object.entries(e).filter(([,s])=>s!==void 0).map(([s,i])=>s==="background"?`${s}=${encodeURIComponent(i)}`:`${s}=${i}`).join(","):"";return`https://${t}/cdn-cgi/image/${r}/${n}`}var mt=class extends v{constructor(e){super(e);this.userCache=new Map;this.leaderboardPageUserCache=new Map}getUserAvatarUrl(e,r=pt.MEDIUM){let s=this.userCache.get(e)??this.leaderboardPageUserCache.get(e);return s?.avatarR2Key?jr(s.avatarR2Key,this.sdk.uploadsHost,{width:r,height:r,fit:"cover",quality:"high",sharpen:1}):null}getUsername(e){return this.userCache.get(e)?.username??this.leaderboardPageUserCache.get(e)?.username??null}async listFriends(){let e=await this.sdk.convexClient.query(f.sdk.friends.listFriends,{});return this.cacheUsers(e),e}cacheUsers(e){for(let r of e)this.userCache.set(r.userId,{username:r.username,avatarR2Key:r.avatarUrl??r.userAvatarUrl})}cacheLeaderboardPage(e){this.leaderboardPageUserCache.clear();for(let r of e)this.leaderboardPageUserCache.set(r.userId,{username:r.username,avatarR2Key:r.userAvatarUrl})}};var yt=class extends v{constructor(e){super(e);this._isFullscreen=!1;this.listeners=new Set;this.sdk.iframeMessenger.addEventListener(w.FULLSCREEN_CHANGED,r=>{this.sdk.gameEventManager.notifyGame(b.FULLSCREEN_CHANGED,{isFullscreen:r.isFullscreen}),this.setState(r.isFullscreen)}),this.installCompatShims()}isFullscreen(){return this._isFullscreen}async requestFullscreen(e){return(await this.sdk.iframeMessenger.requestFromParent(w.SET_FULLSCREEN,{fullscreen:e})).success}async toggleFullscreen(){return(await this.sdk.iframeMessenger.requestFromParent(w.TOGGLE_FULLSCREEN)).success}subscribe(e){return this.listeners.add(e),()=>this.listeners.delete(e)}setState(e){if(this._isFullscreen!==e){this._isFullscreen=e;for(let r of this.listeners)r(e)}}installCompatShims(){if(typeof document>"u")return;let e=()=>this._isFullscreen?document.body:null;Object.defineProperty(document,"fullscreenElement",{configurable:!0,get:e}),Object.defineProperty(document,"webkitFullscreenElement",{configurable:!0,get:e});let r=async()=>{if(this._isFullscreen)return;if(!await this.requestFullscreen(!0))throw new Error("Fullscreen request was denied")},s=async()=>{if(!this._isFullscreen)return;if(!await this.requestFullscreen(!1))throw new Error("Exit fullscreen request was denied")};Element.prototype.requestFullscreen=function(){return r()},Element.prototype.webkitRequestFullscreen=function(){return r()},Document.prototype.exitFullscreen=function(){return s()},Document.prototype.webkitExitFullscreen=function(){return s()},this.subscribe(()=>{document.dispatchEvent(new Event("fullscreenchange",{bubbles:!0}))})}};var bt=class extends v{constructor(e){super(e);this.eventQueue=[]}notifyGame(e,r){if(!this.sdk.eventsReady){this.eventQueue.push({event:e,payload:r}),u.debug(`Queued event: ${e}`);return}this.sdk.engineInstance?this.sendGameEvent(e,r):this.sdk.dispatchEvent(new CustomEvent(e,{detail:r}))}sendGameEvent(e,r){let s=typeof r=="object"?JSON.stringify(r):r;this.sdk.engineInstance?.SendMessage?this.sdk.engineInstance.SendMessage(this.sdk.engineCallbackReceiver,e,s):u.error("Engine instance not set. Dropping event:",e)}flushEventQueue(){let e=this.eventQueue;this.eventQueue=[];for(let r of e)this.notifyGame(r.event,r.payload)}};var ee={passive:!0,capture:!0},vt=class extends v{constructor(e){super(e);this.deviceFingerprint=void 0;this.testConnectionInterval=null;this.heartbeatInterval=null;this.gamepadPollInterval=null;this.inactivityTimeout=null;this.isConnected=!1;this.sentDisconnectedEvent=!1;this.disconnectedAt=null;this.lastHeartbeatTime=0;this.lastInputResetAt=0;this.heartbeatInFlight=!1;this.isFirstTick=!0;this.TEST_CONNECTION_INTERVAL_MS=1e3;this.DISCONNECTED_TIMEOUT_MS=9e4;this.INACTIVITY_TIMEOUT_MS=1800*1e3;this.INPUT_THROTTLE_MS=1e3;this.GAMEPAD_POLL_INTERVAL_MS=1e3;this.GAMEPAD_AXIS_DEADZONE=.2;this.cachedPresenceData={};this.handleVisibilityChange=()=>{document.visibilityState==="visible"?this.start():this.stop()};this.handleUserInput=()=>{let e=Date.now();e-this.lastInputResetAt<this.INPUT_THROTTLE_MS||(this.lastInputResetAt=e,this.start())};this.isConnected=this.sdk.convexClient.client.connectionState().isWebSocketConnected,document.addEventListener("visibilitychange",this.handleVisibilityChange),window.addEventListener("keydown",this.handleUserInput,ee),window.addEventListener("pointerdown",this.handleUserInput,ee),window.addEventListener("pointermove",this.handleUserInput,ee),window.addEventListener("wheel",this.handleUserInput,ee),this.gamepadPollInterval=setInterval(()=>{this.pollGamepads()},this.GAMEPAD_POLL_INTERVAL_MS),this.deviceFingerprintReady=this.sdk.iframeMessenger.requestFromParent(w.GET_DEVICE_FINGERPRINT).then(r=>{this.deviceFingerprint=r}).catch(()=>{})}start(){this.sdk.gameLoaded&&document.visibilityState==="visible"&&(this.inactivityTimeout!==null&&clearTimeout(this.inactivityTimeout),this.inactivityTimeout=setTimeout(()=>{this.stop()},this.INACTIVITY_TIMEOUT_MS),this.heartbeatInterval===null&&(this.isFirstTick?this.deviceFingerprintReady.then(()=>{!this.sdk.gameLoaded||!this.isFirstTick||this.tickHeartbeat()}):this.tickHeartbeat(),this.heartbeatInterval=setInterval(()=>{this.tickHeartbeat()},Re.CLIENT_INTERVAL_MS),this.testConnectionInterval=setInterval(()=>{this.testConnection()},this.TEST_CONNECTION_INTERVAL_MS)))}stop(){this.inactivityTimeout!==null&&(clearTimeout(this.inactivityTimeout),this.inactivityTimeout=null),this.heartbeatInterval!==null&&(clearInterval(this.heartbeatInterval),this.heartbeatInterval=null),this.testConnectionInterval!==null&&(clearInterval(this.testConnectionInterval),this.testConnectionInterval=null)}async updateUserPresence(e){try{return this.cachedPresenceData=e,await this.sdk.convexClient.mutation(f.sdk.presence.heartbeat,{data:e,deviceFingerprint:this.deviceFingerprint}),!0}catch(r){return u.error(`Error updating presence: ${r}`),!1}}isCurrentlyConnected(){return this.isConnected}destroy(){this.stop(),this.gamepadPollInterval!==null&&(clearInterval(this.gamepadPollInterval),this.gamepadPollInterval=null),document.removeEventListener("visibilitychange",this.handleVisibilityChange),window.removeEventListener("keydown",this.handleUserInput,ee),window.removeEventListener("pointerdown",this.handleUserInput,ee),window.removeEventListener("pointermove",this.handleUserInput,ee),window.removeEventListener("wheel",this.handleUserInput,ee)}tickHeartbeat(){let e=Date.now()-this.lastHeartbeatTime,r=this.isFirstTick||e>=Re.CLIENT_REESTABLISH_THRESHOLD_MS;this.isFirstTick=!1,r?this.sendHeartbeat(!0):e>=Re.CLIENT_INTERVAL_MS-Re.CLIENT_GRACE_MS&&this.sendHeartbeat(!1)}sendHeartbeat(e){!e&&this.heartbeatInFlight||(this.heartbeatInFlight=!0,this.sdk.convexClient.mutation(f.sdk.presence.heartbeat,{...e?{data:this.cachedPresenceData,deviceFingerprint:this.deviceFingerprint}:{}}).then(r=>{r&&(this.lastHeartbeatTime=Date.now())}).catch(r=>{u.error(`Heartbeat failed: ${r}`)}).finally(()=>{this.heartbeatInFlight=!1}))}pollGamepads(){if(typeof navigator>"u"||!navigator.getGamepads)return;let e=navigator.getGamepads();for(let r of e)if(r){if(r.buttons.some(s=>s.pressed)){this.start();return}if(r.axes.some(s=>Math.abs(s)>this.GAMEPAD_AXIS_DEADZONE)){this.start();return}}}testConnection(){try{let e=this.isConnected,r=this.sdk.convexClient.client.connectionState();this.isConnected=navigator.onLine&&r.isWebSocketConnected;let s={isConnected:this.isConnected,hasEverConnected:r.hasEverConnected,connectionCount:r.connectionCount,connectionRetries:r.connectionRetries};this.isConnected&&!e?(this.disconnectedAt=null,this.sentDisconnectedEvent=!1,this.sdk.gameEventManager.notifyGame(b.BACKEND_CONNECTED,s)):!this.isConnected&&e?(this.disconnectedAt=Date.now(),u.warn("Backend disconnected - attempting to reconnect..."),this.sdk.gameEventManager.notifyGame(b.BACKEND_RECONNECTING,s)):!this.isConnected&&!e?this.disconnectedAt&&!this.sentDisconnectedEvent&&Date.now()-this.disconnectedAt>this.DISCONNECTED_TIMEOUT_MS&&(this.sdk.gameEventManager.notifyGame(b.BACKEND_DISCONNECTED,s),this.sentDisconnectedEvent=!0):this.isConnected&&e&&(this.disconnectedAt=null,this.sentDisconnectedEvent=!1)}catch(e){u.error("Error testing connection:",e)}}};var wt=class extends v{constructor(e){super(e);this.leaderboardCache=new Map}async getLeaderboard(e){let r=await this.sdk.convexClient.query(f.sdk.leaderboards.getLeaderboard,{name:e});return this.leaderboardCache.set(r.id,r),r}async getOrCreateLeaderboard(e,r,s){let i=await this.sdk.convexClient.mutation(f.sdk.leaderboards.getOrCreateLeaderboard,{name:e,sortOrder:r,displayType:s});return this.leaderboardCache.set(i.id,i),i}getLeaderboardEntryCount(e){let r=this.leaderboardCache.get(e);return r?r.totalEntries:-1}async getMyLeaderboardEntries(e){let r=await this.sdk.convexClient.query(f.sdk.leaderboards.getMyLeaderboardEntry,{leaderboardId:e});r&&r.totalEntries&&this.updateCachedTotalEntries(e,r.totalEntries);let s=r.entry?{...r.entry,userId:this.sdk.wavedashUser.id,username:this.sdk.wavedashUser.username,userAvatarUrl:this.sdk.wavedashUser.avatarUrl}:null;return s?[s]:[]}async listLeaderboardEntriesAroundUser(e,r,s,i=!1){let o=await this.sdk.convexClient.query(f.sdk.leaderboards.listEntriesAroundUser,{leaderboardId:e,countAhead:r,countBehind:s,friendsOnly:i});return o&&o.totalEntries&&this.updateCachedTotalEntries(e,o.totalEntries),this.sdk.friendsManager.cacheLeaderboardPage(o.entries),o.entries}async listLeaderboardEntries(e,r,s,i=!1){let o=await this.sdk.convexClient.query(f.sdk.leaderboards.listEntries,{leaderboardId:e,offset:r,limit:s,friendsOnly:i});return o&&o.totalEntries&&this.updateCachedTotalEntries(e,o.totalEntries),this.sdk.friendsManager.cacheLeaderboardPage(o.entries),o.entries}async uploadLeaderboardScore(e,r,s,i){let o=await this.sdk.convexClient.mutation(f.sdk.leaderboards.upsertLeaderboardEntry,{leaderboardId:e,score:r,keepBest:s,ugcId:i});return o&&o.totalEntries&&this.updateCachedTotalEntries(e,o.totalEntries),{...o.entry,submittedScore:o.submission.score,submittedRank:o.submission.globalRank,userId:this.sdk.wavedashUser.id,username:this.sdk.wavedashUser.username,userAvatarUrl:this.sdk.wavedashUser.avatarUrl}}updateCachedTotalEntries(e,r){let s=this.leaderboardCache.get(e);s&&typeof r=="number"&&this.leaderboardCache.set(e,{...s,totalEntries:r})}};var Yr=ur(ir(),1);var Ct=class Ct extends v{constructor(e){super(e);this.unsubscribeLobbyMessages=null;this.unsubscribeLobbyUsers=null;this.unsubscribeLobbyData=null;this.lobbyId=null;this.lobbyUsers=[];this.lobbyHostId=null;this.lobbyMetadata={};this.pendingMetadataUpdates={};this.recentMessageIds=[];this.maybeBeingDeletedLobbyIds=new Set;this.resetMaybeBeingDeletedLobbyIdTimeouts=new Map;this.inFlightMetadataUpdate=null;this.cachedLobbies={};this.unsubscribeLobbyInvites=null;this.seenInviteIds=new Set;this.p2pUpdateQueue=Promise.resolve();this.throttledSetMetadata=(0,Yr.default)(()=>this.setMetadata(),Ct.METADATA_UPDATE_THROTTLE_MS,{leading:!1,trailing:!0});this.processUserUpdates=e=>{this.sdk.friendsManager.cacheUsers(e);let r=this.lobbyUsers,s=new Set(r.map(o=>o.userId)),i=new Set(e.map(o=>o.userId));this.lobbyUsers=e;for(let o of e)o.isHost&&(this.lobbyHostId=o.userId),s.has(o.userId)||this.sdk.gameEventManager.notifyGame(b.LOBBY_USERS_UPDATED,{...o,changeType:Le.JOINED});for(let o of r)i.has(o.userId)||(o.userId===this.sdk.getUserId()&&u.warn("USER WAS KICKED FROM LOBBY! Received notification for myself leaving."),this.sdk.gameEventManager.notifyGame(b.LOBBY_USERS_UPDATED,{...o,isHost:!1,changeType:Le.LEFT}));this.lobbyId&&(this.p2pUpdateQueue=this.p2pUpdateQueue.then(()=>this.updateP2PConnections(e)).catch(o=>{u.error("Error in queued P2P update:",o)}))};this.processMessageUpdates=e=>{for(let r of e)this.recentMessageIds.includes(r.messageId)||(this.recentMessageIds.push(r.messageId),this.sdk.gameEventManager.notifyGame(b.LOBBY_MESSAGE,r));this.recentMessageIds=e.map(r=>r.messageId)};this.processInviteUpdates=e=>{for(let r of e)this.seenInviteIds.has(r.notificationId)||(this.seenInviteIds.add(r.notificationId),this.sdk.gameEventManager.notifyGame(b.LOBBY_INVITE,r));this.seenInviteIds=new Set(e.map(r=>r.notificationId))};this.unsubscribeLobbyInvites=this.sdk.convexClient.onUpdate(f.sdk.gameLobby.getLobbyInvites,{},this.processInviteUpdates,r=>{u.error(`Lobby invites subscription error: ${r}`)})}async createLobby(e,r){let s=await this.sdk.convexClient.mutation(f.sdk.gameLobby.createAndJoinLobby,{visibility:e,maxPlayers:r});return this.handleLobbyJoin(s),s.lobbyId}async joinLobby(e){let r=await this.sdk.convexClient.mutation(f.sdk.gameLobby.joinLobby,{lobbyId:e});return this.handleLobbyJoin(r),!0}getLobbyUsers(e){return this.lobbyId!==e?(u.error("Must be a member of the lobby to access user list"),[]):this.lobbyUsers}getHostId(e){return this.lobbyId!==e?(u.error("Must be a member of the lobby to access the host ID"),null):this.lobbyHostId}getLobbyData(e,r){return this.lobbyId===e?this.lobbyMetadata[r]??null:this.cachedLobbies[e]?this.cachedLobbies[e].metadata[r]??null:null}deleteLobbyData(e,r){return this.setLobbyData(e,r,null)}setLobbyData(e,r,s){return this.lobbyId!==e||this.lobbyHostId!==this.sdk.getUserId()?!1:(this.lobbyMetadata[r]===s||(s===null?delete this.lobbyMetadata[r]:this.lobbyMetadata[r]=s,this.pendingMetadataUpdates[r]=s,this.throttledSetMetadata()),!0)}getLobbyMaxPlayers(e){return this.cachedLobbies[e]?this.cachedLobbies[e].maxPlayers:0}getNumLobbyUsers(e){return this.lobbyId===e?this.lobbyUsers.length:this.cachedLobbies[e]?this.cachedLobbies[e].playerCount:0}async leaveLobby(e){return this.cleanupLobbyState(),await this.sdk.convexClient.mutation(f.sdk.gameLobby.leaveLobby,{lobbyId:e}),this.sdk.iframeMessenger.postToParent(w.LOBBY_LEFT,{lobbyId:e}),e}async listAvailableLobbies(e=!1){let r=e?{friendsOnly:e}:void 0,i=(await this.sdk.convexClient.query(f.sdk.gameLobby.listAvailable,{filters:r})).filter(o=>!this.maybeBeingDeletedLobbyIds.has(o.lobbyId));for(let o of i)this.cachedLobbies[o.lobbyId]=o;return i}sendLobbyMessage(e,r){let s={lobbyId:e,message:r};if(r.length===0)return u.error("Message cannot be empty"),!1;if(r.length>ut)return u.error(`Message cannot be longer than ${ut} characters`),!1;try{this.sdk.convexClient.mutation(f.sdk.gameLobby.sendMessage,s)}catch(i){return u.error(`Error sending lobby message: ${i}`),!1}return!0}async inviteUserToLobby(e,r){return await this.sdk.convexClient.mutation(f.sdk.gameLobby.inviteToLobby,{lobbyId:e,targetUserId:r}),!0}async getLobbyInviteLink(e=!1){if(!this.lobbyId)throw new Error("User is not in a lobby");let r=await this.sdk.iframeMessenger.requestFromParent(w.GET_LOBBY_INVITE_LINK,{lobbyId:this.lobbyId,copyToClipboard:e});if(!r)throw new Error("Parent could not generate invite link");return r}handleLobbyJoin(e){this.cleanupLobbyState(),this.lobbyId=e.lobbyId,this.lobbyHostId=e.hostId,this.lobbyUsers=e.users,this.lobbyMetadata=e.metadata,this.sdk.friendsManager.cacheUsers(e.users);let r=s=>{u.error(`Lobby subscription error: ${s.message}`),s.message.includes("not a member")?this.handleLobbyKicked(me.KICKED):this.handleLobbyKicked(me.ERROR)};this.unsubscribeLobbyMessages=this.sdk.convexClient.onUpdate(f.sdk.gameLobby.lobbyMessages,{lobbyId:e.lobbyId},this.processMessageUpdates,r),this.unsubscribeLobbyData=this.sdk.convexClient.onUpdate(f.sdk.gameLobby.getLobbyMetadata,{lobbyId:e.lobbyId},s=>{this.lobbyMetadata=s,this.sdk.gameEventManager.notifyGame(b.LOBBY_DATA_UPDATED,s)},r),this.unsubscribeLobbyUsers=this.sdk.convexClient.onUpdate(f.sdk.gameLobby.lobbyUsers,{lobbyId:e.lobbyId},this.processUserUpdates,r),e.users.length>1&&(this.p2pUpdateQueue=this.updateP2PConnections(e.users).catch(s=>{u.error("Error initializing P2P on join:",s)})),this.sdk.iframeMessenger.postToParent(w.LOBBY_JOINED,{lobbyId:e.lobbyId}),this.sdk.gameEventManager.notifyGame(b.LOBBY_JOINED,{lobbyId:e.lobbyId,hostId:e.hostId,users:e.users,metadata:e.metadata}),u.debug("Subscribed to lobby:",e.lobbyId)}handleLobbyKicked(e=me.KICKED){let r=this.lobbyId;r&&(u.warn(`User was removed from lobby: ${r} (reason: ${e})`),this.cleanupLobbyState(),this.sdk.iframeMessenger.postToParent(w.LOBBY_LEFT,{lobbyId:r}),this.sdk.gameEventManager.notifyGame(b.LOBBY_KICKED,{lobbyId:r,reason:e}))}cleanupLobbyState(){let e=this.lobbyId;if(this.lobbyId=null,this.throttledSetMetadata.cancel(),this.pendingMetadataUpdates={},this.unsubscribeLobbyMessages&&(this.unsubscribeLobbyMessages(),this.unsubscribeLobbyMessages=null),this.unsubscribeLobbyUsers&&(this.unsubscribeLobbyUsers(),this.unsubscribeLobbyUsers=null),this.unsubscribeLobbyData&&(this.unsubscribeLobbyData(),this.unsubscribeLobbyData=null),this.sdk.p2pManager.disconnectP2P(),e&&this.lobbyUsers.length===1){let r=this.resetMaybeBeingDeletedLobbyIdTimeouts.get(e);r&&clearTimeout(r),this.maybeBeingDeletedLobbyIds.add(e);let s=setTimeout(()=>{this.maybeBeingDeletedLobbyIds.delete(e),this.resetMaybeBeingDeletedLobbyIdTimeouts.delete(e)},500);this.resetMaybeBeingDeletedLobbyIdTimeouts.set(e,s)}this.lobbyUsers=[],this.lobbyHostId=null,this.lobbyMetadata={},this.recentMessageIds=[],this.p2pUpdateQueue=Promise.resolve()}unsubscribeFromCurrentLobby(){this.cleanupLobbyState()}destroy(){this.cleanupLobbyState(),this.unsubscribeLobbyInvites&&(this.unsubscribeLobbyInvites(),this.unsubscribeLobbyInvites=null);for(let e of this.resetMaybeBeingDeletedLobbyIdTimeouts.values())clearTimeout(e);this.resetMaybeBeingDeletedLobbyIdTimeouts.clear(),this.maybeBeingDeletedLobbyIds.clear(),this.seenInviteIds.clear(),this.cachedLobbies={}}setMetadata(){if(this.inFlightMetadataUpdate!==null||this.lobbyId===null||Object.keys(this.pendingMetadataUpdates).length===0)return;let e=this.pendingMetadataUpdates;this.pendingMetadataUpdates={},this.inFlightMetadataUpdate=this.sdk.convexClient.mutation(f.sdk.gameLobby.setLobbyMetadata,{lobbyId:this.lobbyId,updates:e}).catch(r=>{u.error("Error updating lobby metadata:",r)}).finally(()=>{this.inFlightMetadataUpdate=null,Object.keys(this.pendingMetadataUpdates).length>0&&this.throttledSetMetadata()})}async updateP2PConnections(e){if(this.lobbyId)try{if(e.length<=1){this.sdk.p2pManager.disconnectP2P(),u.debug("Only one user in lobby, P2P connections disconnected");return}let r=e.map(s=>({id:s.userId,username:s.username,avatarUrl:s.userAvatarUrl}));await this.sdk.p2pManager.initializeP2PForCurrentLobby(this.lobbyId,r),u.debug(`P2P connections updated for lobby ${this.lobbyId} with ${r.length} users`)}catch(r){u.error("Error updating P2P connections:",r)}}};Ct.METADATA_UPDATE_THROTTLE_MS=150;var St=Ct;function Zr(){if(typeof document>"u")return;let n=document.getElementsByClassName("game-focus-target");if(n.length>0){n[0].focus();return}document.querySelector("canvas, input, button, [tabindex]:not([tabindex='-1'])")?.focus()}var tn=typeof Element<"u"&&typeof document<"u",Xr=tn?Element.prototype.requestPointerLock:void 0,en=0;function It(){if(!tn||!Xr)return()=>{};++en===1&&(Element.prototype.requestPointerLock=function(){return Promise.resolve()}),document.exitPointerLock();let n=!1;return()=>{n||(n=!0,--en===0&&(Element.prototype.requestPointerLock=Xr))}}var Pt=class extends v{constructor(e){super(e);this.handleKeyDown=e=>{e.key==="Tab"&&e.shiftKey&&(e.preventDefault(),this.toggleOverlay())};this.sdk.iframeMessenger.addEventListener(w.TAKE_FOCUS,Zr),this.sdk.iframeMessenger.addEventListener(w.OVERLAY_CHANGED,({isOpen:r})=>this.setOpen(r)),typeof window<"u"&&window.addEventListener("keydown",this.handleKeyDown)}setOpen(e){e?this.restorePointerLock??(this.restorePointerLock=It()):(this.restorePointerLock?.(),this.restorePointerLock=void 0)}toggleOverlay(){this.sdk.iframeMessenger.postToParent(w.TOGGLE_OVERLAY,{})}destroy(){this.restorePointerLock?.(),this.restorePointerLock=void 0,typeof window<"u"&&window.removeEventListener("keydown",this.handleKeyDown)}};var rn={enableReliableChannel:!0,enableUnreliableChannel:!0,messageSize:2048,maxIncomingMessages:1024},J=class J extends v{constructor(e){super(e);this.currentConnection=null;this.peerConnections=new Map;this.reliableChannels=new Map;this.unreliableChannels=new Map;this.pendingIceCandidates=new Map;this.iceRestartAttempts=new Map;this.iceRestartInProgress=new Set;this.MAX_ICE_RESTART_ATTEMPTS=3;this.reconnectingPeers=new Set;this.establishedPeers=new Set;this.packetDropTrackers=new Map;this.PACKET_DROP_WINDOW_MS=500;this.turnCredentials=null;this.turnCredentialsInitPromise=null;this.unsubscribeFromSignalingMessages=null;this.processedSignalingMessages=new Set;this.pendingProcessedMessageIds=new Set;this.initializationInProgress=null;this.initializationLobbyId=null;this.signalingSubscriptionReady=null;this.signalingSubscriptionReadyResolver=null;this.channelQueues=new Map;this.MESSAGE_SLOT_HEADER_SIZE=4;this.MAX_CHANNELS=8;this.USERID_SIZE=32;this.CHANNEL_SIZE=4;this.DATALENGTH_SIZE=4;this.CHANNEL_OFFSET=this.USERID_SIZE;this.DATALENGTH_OFFSET=this.USERID_SIZE+this.CHANNEL_SIZE;this.PAYLOAD_OFFSET=this.USERID_SIZE+this.CHANNEL_SIZE+this.DATALENGTH_SIZE;this.WIRE_CHANNEL_SIZE=1;this.WIRE_CHANNEL_OFFSET=0;this.WIRE_PAYLOAD_OFFSET=this.WIRE_CHANNEL_SIZE;this.textEncoder=new TextEncoder;this.textDecoder=new TextDecoder;this.initialized=!1;this.config={...rn}}destroy(){this.disconnectP2P()}ensureInitialized(){this.initialized||this.init()}init(e){if(this.initialized&&!e)return;this.config={...rn,...e};let r=this.MESSAGE_SLOT_HEADER_SIZE+this.PAYLOAD_OFFSET+1,s=this.config.messageSize,i=this.config.maxIncomingMessages;if(s<r)throw new Error(`P2P messageSize must be at least ${r} bytes (got ${s})`);if(s>J.MAX_MESSAGE_SIZE&&console.warn(`P2P messageSize ${s} exceeds max ${J.MAX_MESSAGE_SIZE}, clamping to ${J.MAX_MESSAGE_SIZE}`),i<1)throw new Error(`P2P maxIncomingMessages must be at least 1 (got ${i})`);this.MESSAGE_SIZE=Math.min(s,J.MAX_MESSAGE_SIZE),this.QUEUE_SIZE=i,this.MAX_PAYLOAD_SIZE=this.MESSAGE_SIZE-this.MESSAGE_SLOT_HEADER_SIZE-this.PAYLOAD_OFFSET,this.outgoingMessageBuffer=new Uint8Array(this.MAX_PAYLOAD_SIZE);let o=this.MESSAGE_SIZE*this.QUEUE_SIZE*this.MAX_CHANNELS;o>J.MEMORY_WARNING_THRESHOLD_BYTES&&console.warn(`P2P ring buffer memory could reach ${(o/1024/1024).toFixed(1)}MB if all ${this.MAX_CHANNELS} channels are used (messageSize=${this.MESSAGE_SIZE} x maxIncomingMessages=${this.QUEUE_SIZE} per channel). Consider reducing maxIncomingMessages if memory is a concern.`),this.initialized=!0}async initializeP2PForCurrentLobby(e,r){if(this.ensureInitialized(),this.currentConnection&&this.currentConnection.lobbyId===e)return this.updateP2PConnection(r);if(this.initializationInProgress&&this.initializationLobbyId===e&&(u.debug("P2P initialization already in progress, waiting..."),await this.initializationInProgress,this.currentConnection))return this.updateP2PConnection(r);this.initializationLobbyId=e,this.initializationInProgress=this.doInitializeP2P(e,r);try{return await this.initializationInProgress}finally{this.initializationInProgress=null,this.initializationLobbyId=null}}async doInitializeP2P(e,r){let s={lobbyId:e,peers:{}};return r.forEach(i=>{i.id!==this.sdk.getUserId()&&(s.peers[i.id]={userId:i.id,username:i.username})}),this.currentConnection=s,await this.establishWebRTCConnections(s),s}async getIceServers(){return this.turnCredentials&&this.turnCredentials.expiresAt>Date.now()+36e5?this.turnCredentials.iceServers:this.turnCredentialsInitPromise?(await this.turnCredentialsInitPromise,this.turnCredentials?.iceServers??null):(this.turnCredentialsInitPromise=(async()=>{try{this.turnCredentials=await this.sdk.convexClient.action(f.sdk.turnCredentials.getOrCreate,{})}finally{this.turnCredentialsInitPromise=null}})(),await this.turnCredentialsInitPromise,this.turnCredentials?.iceServers??null)}async updateP2PConnection(e){if(!this.currentConnection)throw new Error("No existing P2P connection to update");u.debug("Updating P2P connection with new member list"),new Set(Object.keys(this.currentConnection.peers)).add(this.sdk.getUserId());let s=new Set(e.map(o=>o.id)),i=[];for(let o of e){if(o.id===this.sdk.getUserId())continue;let a=this.currentConnection.peers[o.id];a?!a.username&&o.username&&(a.username=o.username):(u.debug(`Adding new peer: ${o.username} (${o.id})`),this.currentConnection.peers[o.id]={userId:o.id,username:o.username},i.push(o.id))}if(i.length>0){let o=this.sdk.getUserId(),a=i.map(l=>{let d=o<l;return u.debug(`Creating connection to new peer ${l}, shouldCreateChannels: ${d}`),this.createPeerConnection(l,this.currentConnection,d)});await Promise.all(a);let c=i.filter(l=>o<l);if(c.length>0){let l=c.map(d=>(u.debug(`Initiating offer to new peer ${d} (lower userId rule)`),this.createOfferToPeer(d)));await Promise.all(l),u.debug(`Initiated ${l.length} offers to new peers`)}}for(let o of Object.keys(this.currentConnection.peers))if(!s.has(o)){let a=this.currentConnection.peers[o];u.debug(`Peer left: ${a.username} (${o})`);let c=this.peerConnections.get(o);c&&(c.close(),this.peerConnections.delete(o)),this.reliableChannels.delete(o),this.unreliableChannels.delete(o),this.pendingIceCandidates.delete(o),this.iceRestartAttempts.delete(o),this.iceRestartInProgress.delete(o),this.reconnectingPeers.delete(o),this.establishedPeers.delete(o),delete this.currentConnection.peers[o]}return this.currentConnection}async establishWebRTCConnections(e){this.subscribeToSignalingMessages(e),this.signalingSubscriptionReady&&(await this.signalingSubscriptionReady,u.debug("Signaling subscription confirmed ready")),await this.establishPeerConnections(e)}subscribeToSignalingMessages(e){this.signalingSubscriptionReady=new Promise(s=>{this.signalingSubscriptionReadyResolver=s});let r=!1;this.unsubscribeFromSignalingMessages=this.sdk.convexClient.onUpdate(f.sdk.p2pSignaling.getSignalingMessages,{lobbyId:e.lobbyId},s=>{r||(r=!0,this.signalingSubscriptionReadyResolver?.(),this.signalingSubscriptionReadyResolver=null),s&&this.processSignalingMessages(s,e)})}stopSignalingMessageSubscription(){this.unsubscribeFromSignalingMessages!==null&&(this.unsubscribeFromSignalingMessages(),this.unsubscribeFromSignalingMessages=null)}async processSignalingMessages(e,r){if(e.length===0)return;let s=[],i=[];for(let o of e)!this.processedSignalingMessages.has(o._id)&&!this.pendingProcessedMessageIds.has(o._id)&&i.push(o),s.push(o._id);for(let o of i){this.pendingProcessedMessageIds.add(o._id);try{await this.handleSignalingMessage(o,r),this.processedSignalingMessages.add(o._id)}catch(a){u.error("Error handling signaling message:",a)}}if(s.length>0)try{await this.sdk.convexClient.mutation(f.sdk.p2pSignaling.markSignalingMessagesProcessed,{messageIds:s});for(let o of s)this.pendingProcessedMessageIds.delete(o)}catch(o){u.error("Failed to mark signaling messages as processed:",o);for(let a of s)this.pendingProcessedMessageIds.delete(a)}}async handleSignalingMessage(e,r){if(e.fromUserId===this.sdk.getUserId())return;let s=e.fromUserId;if(!this.peerConnections.has(s))if(e.messageType===H.OFFER){if(u.debug(`Received offer from ${s} before peer connection exists, creating on-demand`),r.peers[s]||(r.peers[s]={userId:s,username:""}),!await this.createPeerConnection(s,r,!1)){u.error(`Failed to create on-demand peer connection for ${s}`);return}}else{u.warn(`No peer connection for user ${s}, dropping ${e.messageType} message`);return}let i=this.peerConnections.get(s);switch(e.messageType){case H.OFFER:{this.iceRestartInProgress.delete(s),u.debug(`Processing offer from peer ${s}:`),await i.setRemoteDescription(new RTCSessionDescription(e.data)),await this.flushPendingIceCandidates(s,i);let o=await i.createAnswer();await i.setLocalDescription(o),u.debug(" Answer created, waiting for ondatachannel events...");let a={type:o.type,sdp:o.sdp};await this.sendSignalingMessage(s,{type:H.ANSWER,data:a});break}case H.ANSWER:await i.setRemoteDescription(new RTCSessionDescription(e.data)),await this.flushPendingIceCandidates(s,i);break;case H.ICE_CANDIDATE:{let o=e.data;if(i.remoteDescription)await i.addIceCandidate(new RTCIceCandidate(o));else{let a=this.pendingIceCandidates.get(s)||[];a.push(o),this.pendingIceCandidates.set(s,a),u.debug(`Buffered ICE candidate for ${s} (remote description not yet set, ${a.length} buffered)`)}break}default:u.warn("Unknown signaling message type:",e.messageType)}}async flushPendingIceCandidates(e,r){let s=this.pendingIceCandidates.get(e);if(s&&s.length>0){for(let i of s)try{await r.addIceCandidate(new RTCIceCandidate(i))}catch(o){u.warn(`Failed to add buffered ICE candidate for ${e}:`,o)}this.pendingIceCandidates.delete(e)}}async establishPeerConnections(e){u.debug("Establishing WebRTC connections to peers...");let r=this.sdk.getUserId(),s=[];Object.entries(e.peers).forEach(([o,a])=>{let c=r<o;u.debug(`Creating connection to peer ${o} (${a.username}), shouldCreateChannels: ${c}`),s.push(this.createPeerConnection(o,e,c))}),await Promise.all(s);let i=Object.keys(e.peers).filter(o=>r<o);if(i.length>0){let o=i.map(a=>(u.debug(`Initiating offer to peer ${a} (lower userId rule)`),this.createOfferToPeer(a)));await Promise.all(o),u.debug(`Created ${s.length} peer connections and initiated ${o.length} offers`)}else u.debug(`Created ${s.length} peer connections, no offers to initiate`)}async createOfferToPeer(e){let r=this.peerConnections.get(e);if(!r)throw new Error(`No peer connection for user ${e}`);let s=this.reliableChannels.get(e),i=this.unreliableChannels.get(e);u.debug(`Creating offer to peer ${e}:`),u.debug(` Reliable channel state: ${s?.readyState||"none"}`),u.debug(` Unreliable channel state: ${i?.readyState||"none"}`);let o=await r.createOffer();await r.setLocalDescription(o);let a={type:o.type,sdp:o.sdp};await this.sendSignalingMessage(e,{type:H.OFFER,data:a})}async createPeerConnection(e,r,s=!1){let i=await this.getIceServers();if(!i)return u.error(`No ICE servers available for peer ${e}`),this.sdk.gameEventManager.notifyGame(b.P2P_CONNECTION_FAILED,{userId:e,username:r.peers[e]?.username||"",error:"No ICE servers available"}),!1;let o=new RTCPeerConnection({iceServers:i,iceCandidatePoolSize:0,bundlePolicy:"max-bundle",rtcpMuxPolicy:"require"});if(s){if(u.debug(`Creating data channels for peer ${e}`),this.config.enableReliableChannel){let a=o.createDataChannel("reliable",{ordered:!0,maxRetransmits:void 0});this.reliableChannels.set(e,a),this.setupDataChannelHandlers(a,e,"reliable")}if(this.config.enableUnreliableChannel){let a=o.createDataChannel("unreliable",{ordered:!1,maxRetransmits:0});this.unreliableChannels.set(e,a),this.setupDataChannelHandlers(a,e,"unreliable")}}else u.debug(`Will receive data channels from peer ${e} via ondatachannel`);return o.onicecandidate=a=>{if(a.candidate){let c=a.candidate.candidate.includes("typ host")?"host":a.candidate.candidate.includes("typ srflx")?"srflx (STUN)":a.candidate.candidate.includes("typ relay")?"relay (TURN)":"unknown";u.debug(`Peer ${e} gathered ICE candidate: ${c}`),u.debug(` Candidate: ${a.candidate.candidate}`);let l={candidate:a.candidate.candidate,sdpMid:a.candidate.sdpMid,sdpMLineIndex:a.candidate.sdpMLineIndex,usernameFragment:a.candidate.usernameFragment};this.sendSignalingMessage(e,{type:H.ICE_CANDIDATE,data:l})}},o.ondatachannel=a=>{let c=a.channel;u.debug(`Received ${c.label} data channel from peer ${e}`),c.label==="reliable"?this.reliableChannels.set(e,c):c.label==="unreliable"&&this.unreliableChannels.set(e,c),this.setupDataChannelHandlers(c,e,c.label)},o.onconnectionstatechange=()=>{u.debug(`Peer ${e} connection state: ${o.connectionState}`),o.connectionState==="connected"&&u.debug(` Peer ${e} fully connected, expecting ondatachannel events now...`)},o.oniceconnectionstatechange=()=>{if(u.debug(`Peer ${e} ICE connection state: ${o.iceConnectionState}`),o.iceConnectionState==="connected"){if(u.debug(` ICE connected to peer ${e}, data channels should be available...`),this.iceRestartAttempts.delete(e),this.iceRestartInProgress.delete(e),this.reconnectingPeers.delete(e)){let a=this.currentConnection?.peers[e];a&&this.sdk.gameEventManager.notifyGame(b.P2P_PEER_RECONNECTED,{userId:a.userId,username:a.username})}}else if(o.iceConnectionState==="failed"){if(u.debug(`ICE connection to peer ${e} failed, will retry in 500ms...`),!this.reconnectingPeers.has(e)){this.reconnectingPeers.add(e);let a=this.currentConnection?.peers[e];a&&this.sdk.gameEventManager.notifyGame(b.P2P_PEER_RECONNECTING,{userId:a.userId,username:a.username})}setTimeout(()=>{o.iceConnectionState==="failed"&&(u.warn(`ICE connection to peer ${e} still failed after delay, attempting ICE restart...`),this.attemptIceRestart(e,o))},500)}else o.iceConnectionState==="disconnected"&&u.debug(`ICE connection to peer ${e} disconnected, may recover...`)},o.onicegatheringstatechange=()=>{u.debug(`Peer ${e} ICE gathering state: ${o.iceGatheringState}`)},this.peerConnections.set(e,o),!0}async attemptIceRestart(e,r){if(this.sdk.getUserId()>e){u.debug(`Waiting for peer ${e} to initiate ICE restart (they have lower userId)`);return}if(this.iceRestartInProgress.has(e)){u.debug(`ICE restart already in progress for peer ${e}, skipping`);return}let i=this.iceRestartAttempts.get(e)||0;if(i>=this.MAX_ICE_RESTART_ATTEMPTS){u.error(`Max ICE restart attempts (${this.MAX_ICE_RESTART_ATTEMPTS}) reached for peer ${e}, giving up`),this.reconnectingPeers.delete(e),this.establishedPeers.delete(e);let o=this.currentConnection?.peers[e];o&&this.sdk.gameEventManager.notifyGame(b.P2P_CONNECTION_FAILED,{userId:o.userId,username:o.username,error:"ICE restart failed after maximum attempts"}),r.close();return}this.iceRestartAttempts.set(e,i+1),this.iceRestartInProgress.add(e),u.debug(`ICE restart attempt ${i+1}/${this.MAX_ICE_RESTART_ATTEMPTS} for peer ${e}`);try{r.restartIce();let o=await r.createOffer({iceRestart:!0});await r.setLocalDescription(o);let a={type:o.type,sdp:o.sdp};await this.sendSignalingMessage(e,{type:H.OFFER,data:a}),u.debug(`ICE restart offer sent to peer ${e}`)}catch(o){u.error(`Failed to initiate ICE restart for peer ${e}:`,o)}}setupDataChannelHandlers(e,r,s){e.onopen=()=>{if(u.debug(`${s} data channel opened with peer ${r}`),this.isPeerReady(r)&&!this.establishedPeers.has(r)){this.establishedPeers.add(r);let i=this.currentConnection?.peers[r];i&&this.sdk.gameEventManager.notifyGame(b.P2P_CONNECTION_ESTABLISHED,{userId:i.userId,username:i.username})}},e.onmessage=i=>{this.enqueueMessage(i.data,r)},e.onerror=i=>{u.error(`Data channel error with peer ${r}:`,i);let o=this.currentConnection?.peers[r];o&&this.sdk.gameEventManager.notifyGame(b.P2P_CONNECTION_FAILED,{userId:o.userId,username:o.username,error:i.toString()})},e.onclose=()=>{u.debug(`${s} data channel closed with peer ${r}`),this.establishedPeers.delete(r),this.reconnectingPeers.delete(r);let i=this.currentConnection?.peers[r];i&&this.sdk.gameEventManager.notifyGame(b.P2P_PEER_DISCONNECTED,{userId:i.userId,username:i.username})}}sendP2PMessage(e,r=0,s=!0,i,o=i.length){this.ensureInitialized();try{if(!this.currentConnection)return u.error("P2P send called before P2P is initialized, dropping message."),this.reportPacketDrop(r,"SEND","PEER_NOT_READY"),!1;if(!i)return u.error("P2P send called with missing payload, dropping message."),this.reportPacketDrop(r,"SEND","INVALID_PAYLOAD_SIZE"),!1;if(!Number.isInteger(r)||r<0||r>=this.MAX_CHANNELS)return u.error(`P2P appChannel must be an integer in [0, ${this.MAX_CHANNELS}), received ${r}, dropping message.`),this.reportPacketDrop(-1,"SEND","INVALID_CHANNEL"),!1;if(o<=0)return u.error(`P2P payloadSize must be greater than 0, received ${o}, dropping message.`),this.reportPacketDrop(r,"SEND","INVALID_PAYLOAD_SIZE"),!1;if(o>this.MAX_PAYLOAD_SIZE)return u.error(`P2P payload too large: ${o} bytes exceeds max ${this.MAX_PAYLOAD_SIZE} bytes, dropping message.`),this.reportPacketDrop(r,"SEND","PAYLOAD_TOO_LARGE"),!1;if(o>i.length)return u.error(`payloadSize is greater than payload buffer length: ${o} > ${i.length}, dropping message.`),this.reportPacketDrop(r,"SEND","INVALID_PAYLOAD_SIZE"),!1;let a=o<i.length?i.subarray(0,o):i,c=this.encodeWireMessage(r,a),l=s?this.reliableChannels:this.unreliableChannels;if(e===void 0)l.forEach((d,g)=>{if(d.readyState==="open")try{d.send(c)}catch(E){u.error(`P2P broadcast to peer ${g} failed:`,E)}});else{let d=l.get(e);if(!d||d.readyState!=="open")return u.error(`P2P no open channel to peer ${e}, dropping message.`),this.reportPacketDrop(r,"SEND","PEER_NOT_READY"),!1;try{d.send(c)}catch(g){return u.error(`P2P send to peer ${e} failed, dropping message:`,g),this.reportPacketDrop(r,"SEND","PEER_NOT_READY"),!1}}return!0}catch(a){return u.error("Error sending P2P message:",a),!1}}async sendSignalingMessage(e,r){if(!this.currentConnection)throw new Error("No active P2P connection for signaling");try{await this.sdk.convexClient.mutation(f.sdk.p2pSignaling.sendSignalingMessage,{lobbyId:this.currentConnection.lobbyId,toUserId:e,messageType:r.type,data:r.data}),u.debug("Sent signaling message:",r.type)}catch(s){throw u.error("Failed to send signaling message:",s),s}}disconnectP2P(){this.currentConnection&&(this.stopSignalingMessageSubscription(),Object.entries(this.currentConnection.peers).forEach(([e,r])=>{let s=this.peerConnections.get(e);s&&(s.close(),this.peerConnections.delete(e)),this.reliableChannels.delete(e),this.unreliableChannels.delete(e)}),this.currentConnection=null,this.processedSignalingMessages.clear(),this.pendingProcessedMessageIds.clear(),this.pendingIceCandidates.clear(),this.iceRestartAttempts.clear(),this.iceRestartInProgress.clear(),this.reconnectingPeers.clear(),this.establishedPeers.clear(),this.clearPacketDropTrackers(),this.initializationInProgress=null,this.initializationLobbyId=null,this.signalingSubscriptionReady=null,this.signalingSubscriptionReadyResolver=null)}isPeerReady(e){if(!this.currentConnection)return!1;let r=this.reliableChannels.get(e),s=this.unreliableChannels.get(e),i=!this.config.enableReliableChannel||r?.readyState==="open",o=!this.config.enableUnreliableChannel||s?.readyState==="open";return i&&o}isBroadcastReady(){return this.currentConnection?this.reliableChannels.size>0&&this.unreliableChannels.size>0:!1}getPeerStatuses(){if(!this.currentConnection)return{};let e={};for(let r of Object.keys(this.currentConnection.peers)){let s=this.reliableChannels.get(r),i=this.unreliableChannels.get(r);e[r]={reliable:s?.readyState,unreliable:i?.readyState,ready:this.isPeerReady(r)}}return e}createChannelQueue(e){let r=this.MESSAGE_SIZE*this.QUEUE_SIZE,s=new ArrayBuffer(r),i=new Uint8Array(s);this.channelQueues.set(e,{buffer:s,writeIndex:0,readIndex:0,messageCount:0,incomingDataView:i}),u.debug(`Allocated P2P ring buffer for channel ${e} (${(r/1024/1024).toFixed(1)}MB)`)}reportPacketDrop(e,r,s){let i=`${r}:${e}:${s}`,o=this.packetDropTrackers.get(i);if(o||(o={channel:e,direction:r,reason:s,pendingCount:0,windowTimer:null,droppedTotal:0},this.packetDropTrackers.set(i,o)),o.droppedTotal+=1,o.windowTimer===null){this.emitPacketDropped(o,1),o.windowTimer=setTimeout(()=>this.flushPacketDropWindow(i),this.PACKET_DROP_WINDOW_MS);return}o.pendingCount+=1}flushPacketDropWindow(e){let r=this.packetDropTrackers.get(e);if(r)if(r.pendingCount>0){let s=r.pendingCount;r.pendingCount=0,this.emitPacketDropped(r,s),r.windowTimer=setTimeout(()=>this.flushPacketDropWindow(e),this.PACKET_DROP_WINDOW_MS)}else r.windowTimer=null}emitPacketDropped(e,r){this.sdk.gameEventManager.notifyGame(b.P2P_PACKET_DROPPED,{channel:e.channel,direction:e.direction,reason:e.reason,droppedCount:r,droppedTotal:e.droppedTotal})}clearPacketDropTrackers(){for(let e of this.packetDropTrackers.values())e.windowTimer!==null&&clearTimeout(e.windowTimer);this.packetDropTrackers.clear()}enqueueMessage(e,r){try{if(e.byteLength<this.WIRE_PAYLOAD_OFFSET){u.warn("Binary message too short to extract channel"),this.reportPacketDrop(-1,"RECEIVE","MALFORMED");return}let s=new Uint8Array(e),i=s[this.WIRE_CHANNEL_OFFSET];if(!this.channelQueues.has(i)){if(i>=this.MAX_CHANNELS){u.warn(`Channel ${i} exceeds max channels (${this.MAX_CHANNELS}), dropping message`),this.reportPacketDrop(i,"RECEIVE","INVALID_CHANNEL");return}this.createChannelQueue(i)}let o=this.channelQueues.get(i);if(o.messageCount>=this.QUEUE_SIZE){u.warn(`P2P message queue full for channel ${i}, dropping message`),this.reportPacketDrop(i,"RECEIVE","QUEUE_FULL");return}let a=e.byteLength-this.WIRE_PAYLOAD_OFFSET,c=this.PAYLOAD_OFFSET+a,l=this.MESSAGE_SIZE-this.MESSAGE_SLOT_HEADER_SIZE;if(c>l){u.warn(`Message too large for queue: ${c} > ${l}, dropping message.`),this.reportPacketDrop(i,"RECEIVE","PAYLOAD_TOO_LARGE");return}let d=o.writeIndex*this.MESSAGE_SIZE,g=d+this.MESSAGE_SLOT_HEADER_SIZE,E=new DataView(o.buffer,d,this.MESSAGE_SIZE);E.setUint32(0,c,!0);let h=this.textEncoder.encode(r).slice(0,this.USERID_SIZE);o.incomingDataView.fill(0,g,g+this.USERID_SIZE),o.incomingDataView.set(h,g),E.setUint32(this.MESSAGE_SLOT_HEADER_SIZE+this.CHANNEL_OFFSET,i,!0),E.setUint32(this.MESSAGE_SLOT_HEADER_SIZE+this.DATALENGTH_OFFSET,a,!0),a>0&&o.incomingDataView.set(s.subarray(this.WIRE_PAYLOAD_OFFSET),g+this.PAYLOAD_OFFSET),o.writeIndex=(o.writeIndex+1)%this.QUEUE_SIZE,o.messageCount++}catch(s){u.error("Error enqueuing binary P2P message:",s)}}getMaxPayloadSize(){return this.ensureInitialized(),this.MAX_PAYLOAD_SIZE}getMaxIncomingMessages(){return this.ensureInitialized(),this.QUEUE_SIZE}getOutgoingMessageBuffer(){return this.ensureInitialized(),this.outgoingMessageBuffer}readMessageFromChannel(e){this.ensureInitialized();let r=this.channelQueues.get(e);if(!r)return null;let s=this.readRawMessage(r);return s?this.decodeBinaryMessage(s):null}readRawMessage(e){if(e.messageCount===0)return null;let r=e.readIndex*this.MESSAGE_SIZE,i=new DataView(e.buffer,r,this.MESSAGE_SIZE).getUint32(0,!0),o=this.MESSAGE_SIZE-this.MESSAGE_SLOT_HEADER_SIZE;if(i===0||i>o)return e.readIndex=(e.readIndex+1)%this.QUEUE_SIZE,e.messageCount--,null;let a=new Uint8Array(e.buffer,r+this.MESSAGE_SLOT_HEADER_SIZE,i);return e.readIndex=(e.readIndex+1)%this.QUEUE_SIZE,e.messageCount--,a}drainChannelToBuffer(e,r){this.ensureInitialized();let s=this.channelQueues.get(e);if(!s||s.messageCount===0)return new Uint8Array(0);if(!r||r.byteLength===0){let l=[],d=0;for(;s.messageCount>0;){let m=this.readRawMessage(s);m&&(l.push(m),d+=this.MESSAGE_SLOT_HEADER_SIZE+m.length)}if(l.length===0)return new Uint8Array(0);let g=new Uint8Array(d),E=new DataView(g.buffer),h=0;for(let m of l)E.setUint32(h,m.length,!0),h+=this.MESSAGE_SLOT_HEADER_SIZE,g.set(m,h),h+=m.length;return g}let i=new DataView(r.buffer,r.byteOffset,r.byteLength),o=new DataView(s.buffer),a=this.MESSAGE_SIZE-this.MESSAGE_SLOT_HEADER_SIZE,c=0;for(;s.messageCount>0;){let l=s.readIndex*this.MESSAGE_SIZE,d=o.getUint32(l,!0);if(d===0||d>a){s.readIndex=(s.readIndex+1)%this.QUEUE_SIZE,s.messageCount--;continue}let g=this.MESSAGE_SLOT_HEADER_SIZE+d;if(c+g>r.byteLength)break;i.setUint32(c,d,!0),c+=this.MESSAGE_SLOT_HEADER_SIZE,r.set(new Uint8Array(s.buffer,l+this.MESSAGE_SLOT_HEADER_SIZE,d),c),c+=d,s.readIndex=(s.readIndex+1)%this.QUEUE_SIZE,s.messageCount--}return r.subarray(0,c)}encodeWireMessage(e,r){if(e<0||e>=this.MAX_CHANNELS)throw new Error(`P2P channel ${e} must be between 0 and ${this.MAX_CHANNELS-1}`);let s=this.WIRE_PAYLOAD_OFFSET+r.length,i=new Uint8Array(s);return i[this.WIRE_CHANNEL_OFFSET]=e,r.length>0&&i.set(r,this.WIRE_PAYLOAD_OFFSET),i}decodeBinaryMessage(e){if(e.byteLength<this.PAYLOAD_OFFSET)throw new Error("Invalid binary message: too short");let r=new DataView(e.buffer,e.byteOffset,e.byteLength),s=e,i=0,o=s.slice(i,i+this.USERID_SIZE),a=this.textDecoder.decode(o).replace(/\0+$/,"");i+=this.USERID_SIZE;let c=r.getUint32(i,!0);i+=this.CHANNEL_SIZE;let l=r.getUint32(i,!0);i+=this.DATALENGTH_SIZE;let d=e.slice(i,i+l);return{fromUserId:a,channel:c,payload:d}}};J.MAX_MESSAGE_SIZE=64*1024,J.MEMORY_WARNING_THRESHOLD_BYTES=128*1024*1024;var Tt=J;var si=600*1e3;function ii(n){try{let[,t]=n.split(".");if(!t)return null;let e=t.replace(/-/g,"+").replace(/_/g,"/"),r=e+"===".slice((e.length+3)%4),s=Uint8Array.from(atob(r),o=>o.charCodeAt(0)),i=new TextDecoder().decode(s);return JSON.parse(i)}catch(t){return u.warn("Failed to decode JWT payload",t),null}}function nn(n){let e=ii(n)?.ents;return Array.isArray(e)?e.filter(r=>typeof r=="string"):[]}var At=class extends v{constructor(){super(...arguments);this.paywallOpen=!1}async isEntitled(e){let r=await this.sdk.ensureGameplayJwt();return nn(r).includes(e)}async getEntitlements(){let e=await this.sdk.ensureGameplayJwt();return nn(e)}async triggerPaywall(e){if(await this.isEntitled(e))return!0;if(this.paywallOpen)throw new Error("Paywall already in progress");this.paywallOpen=!0,this.restorePointerLock=It();let r;try{r=await this.sdk.iframeMessenger.requestFromParent(w.TRIGGER_PAYWALL,{contentIdentifier:e},si)}finally{this.restorePointerLock?.(),this.restorePointerLock=void 0,this.paywallOpen=!1}return r.purchased?(await this.sdk.ensureGameplayJwt(!0),!0):!1}isPaywallOpen(){return this.paywallOpen}destroy(){this.restorePointerLock?.(),this.restorePointerLock=void 0}};var sn=ur(ir(),1),oi=1e3,Mt=class extends v{constructor(e){super(e);this.stats=new Map;this.unlockedAchievements=new Set;this.dirtyStats=new Set;this.dirtyAchievements=new Set;this.knownStatIds=new Set;this.knownAchievementIds=new Set;this.loaded={stats:!1,achievements:!1};this.subscriptions=[];this.inFlightPersist=null;this.flushRequested=!1;this.throttledPersist=(0,sn.default)(()=>this.persist(),oi,{leading:!1,trailing:!0});this.subscribe(),this.requestStats().catch(r=>{u.error("Initial stats fetch failed:",r)})}destroy(){this.throttledPersist.cancel();for(let e of this.subscriptions)e();this.subscriptions=[]}isReady(){return this.loaded.stats&&this.loaded.achievements}subscribe(){this.subscriptions.push(this.sdk.convexClient.onUpdate(f.sdk.gameAchievements.listStatIdentifiers,{},e=>{this.knownStatIds=new Set(e)},e=>{u.error("Stat identifiers subscription error:",e)}),this.sdk.convexClient.onUpdate(f.sdk.gameAchievements.listAchievementIdentifiers,{},e=>{this.knownAchievementIds=new Set(e)},e=>{u.error("Achievement identifiers subscription error:",e)}),this.sdk.convexClient.onUpdate(f.sdk.gameAchievements.getMyAchievementsForGame,{},e=>{this.loaded.achievements=!0;for(let{achievement:r}of e)this.unlockedAchievements.add(r.identifier)},e=>{u.error("Achievement subscription error:",e)}))}async requestStats(){let e=await this.sdk.convexClient.query(f.sdk.gameAchievements.getMyStatsForGame,{});this.loaded.stats=!0;for(let r of e)this.stats.has(r.identifier)||this.stats.set(r.identifier,r.value);return!0}storeStats(){return this.isReady()?(this.throttledPersist(),this.requestPersistFlush(),!0):!1}requestPersistFlush(){this.inFlightPersist!==null&&(this.flushRequested=!0),this.throttledPersist.flush()}persist(){if(this.inFlightPersist!==null||this.dirtyStats.size===0&&this.dirtyAchievements.size===0)return;let e=this.getPendingData();e&&(this.inFlightPersist=Promise.all([e.stats.length>0?this.sdk.convexClient.mutation(f.sdk.gameAchievements.setUserGameStats,{stats:e.stats}):Promise.resolve(),e.achievements.length>0?this.sdk.convexClient.mutation(f.sdk.gameAchievements.setUserGameAchievements,{achievements:e.achievements}):Promise.resolve()]).then(()=>{this.sdk.gameEventManager.notifyGame(b.STATS_STORED,{success:!0})}).catch(r=>{let s=r instanceof Error?r.message:`Error storing stats: ${r}`;u.error(s),this.sdk.gameEventManager.notifyGame(b.STATS_STORED,{success:!1,message:s})}).finally(()=>{this.inFlightPersist=null;let r=this.flushRequested;this.flushRequested=!1,(this.dirtyStats.size>0||this.dirtyAchievements.size>0)&&(this.throttledPersist(),r&&this.throttledPersist.flush())}))}getStat(e){return!this.isReady()||!this.knownStatIds.has(e)?0:this.stats.get(e)??0}setStat(e,r,s=!1){return!this.isReady()||!this.knownStatIds.has(e)?!1:(this.stats.get(e)!==r&&(this.stats.set(e,r),this.dirtyStats.add(e),this.throttledPersist()),s&&this.requestPersistFlush(),!0)}getAchievement(e){return!this.isReady()||!this.knownAchievementIds.has(e)?!1:this.unlockedAchievements.has(e)}setAchievement(e,r=!1){return!this.isReady()||!this.knownAchievementIds.has(e)?!1:(this.unlockedAchievements.has(e)||(this.unlockedAchievements.add(e),this.dirtyAchievements.add(e),this.throttledPersist()),r&&this.requestPersistFlush(),!0)}getPendingData(){if(this.dirtyStats.size===0&&this.dirtyAchievements.size===0)return null;let e=[...this.dirtyStats].map(s=>({identifier:s,value:this.stats.get(s)})),r=[...this.dirtyAchievements];return this.dirtyStats.clear(),this.dirtyAchievements.clear(),{stats:e,achievements:r}}};var xt=class extends v{constructor(t){super(t)}async createUGCItem(t,e,r,s,i){let{ugcId:o,uploadUrl:a}=await this.sdk.convexClient.mutation(f.sdk.userGeneratedContent.createUGCItem,{ugcType:t,title:e,description:r,visibility:s,createPresignedUploadUrl:!!i});if(i&&!a)throw new Error(`Failed to create a presigned upload URL for UGC item: ${i}`);if(i&&a&&!await this.sdk.fileSystemManager.upload(a,i))throw new Error(`Failed to upload UGC item: ${i}`);return o}async updateUGCItem(t,e={}){let{title:r,description:s,visibility:i,filePath:o}=e,{uploadUrl:a}=await this.sdk.convexClient.mutation(f.sdk.userGeneratedContent.updateUGCItem,{ugcId:t,title:r,description:s,visibility:i,createPresignedUploadUrl:!!o});if(o&&!a)throw new Error(`Failed to create a presigned upload URL for UGC item: ${o}`);if(o&&a&&!await this.sdk.fileSystemManager.upload(a,o))throw new Error(`Failed to upload UGC item: ${o}`);return t}async deleteUGCItem(t){return await this.sdk.convexClient.mutation(f.sdk.userGeneratedContent.deleteUGCItem,{ugcId:t}),t}async downloadUGCItem(t,e){let r=await this.sdk.convexClient.query(f.sdk.userGeneratedContent.getUGCItemDownloadUrl,{ugcId:t});try{await this.sdk.fileSystemManager.download(r,e)}catch(s){let i=s instanceof Error?s.message:String(s);throw new Error(`Failed to download UGC item ${t}: ${i}`)}return t}async listUGCItems(t={}){let{createdBy:e,ugcType:r,titleSearch:s,numItems:i,continueCursor:o}=t,a=e!==void 0||r!==void 0||s!==void 0?{createdBy:e,ugcType:r,titleSearch:s}:void 0;return await this.sdk.convexClient.query(f.sdk.userGeneratedContent.listUGCItems,{filters:a,numItems:i,continueCursor:o})}};var on="";function an(n){on=n}function Rt(){return on}var ai=15e3,_t=class{constructor(){this.handleMessage=t=>{if(t.origin!==Rt()){t.source!==window&&console.warn(`Ignored message from untrusted origin: ${t.origin}`);return}if(t.data?.requestId){let s=this.pendingRequests.get(t.data.requestId);s&&(clearTimeout(s.timeout),this.pendingRequests.delete(t.data.requestId),s.resolve(t.data.data));return}let e=t.data?.type;if(!e)return;let r=this.listeners.get(e);if(r)for(let s of r)s(t.data)};this.pendingRequests=new Map,this.requestIdCounter=0,this.listeners=new Map,typeof window<"u"&&window.addEventListener("message",this.handleMessage)}addEventListener(t,e){let r=this.listeners.get(t);r||(r=new Set,this.listeners.set(t,r)),r.add(e)}removeEventListener(t,e){this.listeners.get(t)?.delete(e)}postToParent(t,e){let r=Rt();return typeof window>"u"||!r?!1:(window.parent.postMessage({type:t,...e},r),!0)}async requestFromParent(t,e,r=ai){return new Promise((s,i)=>{let o=Rt();if(typeof window>"u"||!o){i(new Error("Parent origin not found"));return}let a=`${t}_${++this.requestIdCounter}_${Date.now()}`,c=setTimeout(()=>{this.pendingRequests.delete(a),i(new Error(`${t} request timed out after ${r}ms`))},r);this.pendingRequests.set(a,{resolve:s,reject:i,timeout:c}),window.parent.postMessage({type:t,requestId:a,...e},o)})}};var Lt=class{constructor(){this.handleMessage=t=>{let e=t.data,r=e?.type;if(!r)return;let s=this.listeners.get(r);if(!s||s.size===0)return;let i=t.ports?.[0],o=a=>{if(i)try{i.postMessage(a)}catch(c){u.warn("Failed to reply to SW via port",c)}this.postToServiceWorker(a)};for(let a of s)a(e?.payload,o)};this.listeners=new Map,typeof navigator<"u"&&navigator.serviceWorker&&navigator.serviceWorker.addEventListener("message",this.handleMessage)}addEventListener(t,e){let r=this.listeners.get(t);r||(r=new Set,this.listeners.set(t,r)),r.add(e)}removeEventListener(t,e){this.listeners.get(t)?.delete(e)}postToServiceWorker(t){if(typeof navigator>"u"||!navigator.serviceWorker)return!1;try{return navigator.serviceWorker.controller?.postMessage(t),!0}catch(e){return u.warn("Failed to post message to service worker",e),!1}}};var ci=/^[0-9a-z]{31,37}$/,y=(n,t)=>{if(typeof n!="string")throw new Error(`${t}: expected string, got ${N(n)}`);return n},U=(n,t)=>{if(typeof n!="number"||!Number.isFinite(n))throw new Error(`${t}: expected finite number, got ${N(n)}`);return n},te=(n,t)=>{if(typeof n!="boolean")throw new Error(`${t}: expected boolean, got ${N(n)}`);return n},cn=(n,t)=>{if(n!==null)throw new Error(`${t}: expected null, got ${N(n)}`);return null},un=(n,t)=>{if(!(n instanceof Uint8Array))throw new Error(`${t}: expected Uint8Array, got ${N(n)}`);return n},ln=(n,t)=>{if(typeof n!="object"||n===null||Array.isArray(n))throw new Error(`${t}: expected plain object, got ${N(n)}`);for(let[e,r]of Object.entries(n))if(typeof r=="object"&&r!==null)throw new Error(`${t}: expected flat record with no nested objects, but key "${e}" contains ${N(r)}`);return n};function C(n){return(t,e)=>{if(typeof t!="string"||!ci.test(t))throw new Error(`${e}: expected Id<"${n}"> (base32 string, 31-37 chars), got ${N(t)}`);return t}}function re(n,t){let e=Object.values(n);return(r,s)=>{if(!e.includes(r)){let i=Object.entries(n).map(([a,c])=>`${JSON.stringify(c)} (${a})`).join(", "),o=t?`${t} `:"";throw new Error(`${s}: invalid ${o}value ${N(r)}. Expected one of: ${i}`)}return r}}function M(n){return(t,e)=>{if(t!==void 0)return n(t,e)}}function dn(...n){return(t,e)=>{for(let r of n)try{return r(t,e)}catch{}throw new Error(`${e}: no variant matched, got ${N(t)}`)}}function kt(n){return(t,e)=>{if(typeof t!="object"||t===null||Array.isArray(t))throw new Error(`${e}: expected plain object, got ${N(t)}`);let r=t;for(let s of Object.keys(r))if(!(s in n))throw new Error(`${e}: unrecognized property "${s}"`);for(let s of Object.keys(n))n[s](r[s],`${e}.${s}`);return r}}function ne(n,t,e){let r={},s={};for(let i=0;i<t.length;i++){let[o,a]=t[i];r[o]=a,s[o]=e[i]}kt(r)(s,n)}function N(n){if(n===void 0)return"undefined";if(n===null)return"null";if(n instanceof Uint8Array)return`Uint8Array(byteLength=${n.byteLength})`;if(Array.isArray(n))return`array(length=${n.length})`;let t=typeof n;return t==="string"||t==="number"||t==="boolean"?`${t} ${JSON.stringify(n)}`:t}var ke=new _t,or=class extends EventTarget{constructor(e){super();this._initialized=!1;this._eventsReady=!1;this.destroyed=!1;this.gameFinishedLoading=!1;this.Events=b;this.LobbyVisibility=ct;this.LeaderboardSortOrder=lt;this.LeaderboardDisplayType=dt;this.UGCType=Ae;this.UGCVisibility=Me;this.AvatarSize=pt;this.LobbyKickedReason=me;this.LobbyUserChangeType=Le;this.P2PPacketDropReason=$r;this.config=null;this.engineCallbackReceiver="WavedashCallbackReceiver";this.engineInstance=null;this.gameplayJwt=null;this.gameplayJwtPromise=null;this.setupWarningTimeout=null;this.listenerWrappers=new Map;this.convexClient=new Te(e.convexCloudUrl,{expectAuth:!0}),this.gameCloudId=e.gameCloudId,this.iframeMessenger=ke,this.convexClient.setAuth(({forceRefreshToken:r})=>this.getAuthToken(r)),this.wavedashUser=e.wavedashUser,this.ugcHost=e.ugcHost,this.uploadsHost=e.uploadsHost,this.swMessenger=new Lt,this.p2pManager=new Tt(this),this.lobbyManager=new St(this),this.statsManager=new Mt(this),this.heartbeatManager=new vt(this),this.fileSystemManager=new gt(this),this.ugcManager=new xt(this),this.leaderboardManager=new wt(this),this.friendsManager=new mt(this),this.gameEventManager=new bt(this),this.fullscreenManager=new yt(this),this.overlayManager=new Pt(this),this.audioManager=new ft(this),this.paidContentManager=new At(this),this.managers=[this.p2pManager,this.lobbyManager,this.statsManager,this.heartbeatManager,this.fileSystemManager,this.ugcManager,this.leaderboardManager,this.friendsManager,this.gameEventManager,this.fullscreenManager,this.overlayManager,this.audioManager,this.paidContentManager],this.friendsManager.cacheUsers([{userId:this.wavedashUser.id,username:this.wavedashUser.username,avatarUrl:this.wavedashUser.avatarUrl}]),this.setupSessionEndListeners(),this.setupSwCredsListener(),this.launchParams=e.launchParams??{},this.setupWarningTimeout=setTimeout(()=>{this.setupWarningTimeout=null,u.warn("Wavedash.init(), Wavedash.loadComplete(), or Wavedash.updateLoadProgressZeroToOne() not called yet")},1e4)}get initialized(){return this._initialized}get eventsReady(){return this._eventsReady}clearSetupWarning(){this.setupWarningTimeout!==null&&(clearTimeout(this.setupWarningTimeout),this.setupWarningTimeout=null)}init(e){if(this.loadComplete(),this._initialized)return u.warn("init called twice! Already initialized, skipping init"),!1;if(typeof e=="string")try{e=JSON.parse(e)}catch(r){return u.error("Initialized with invalid config:",r),!1}return this.config=e??{},this._initialized=!0,u.setLogLevel(this.config.debug?X.DEBUG:X.WARN),this.p2pManager.init(this.config.p2p),u.debug("Initialized with config:",this.config),this.config.deferEvents||this.readyForEvents(),!0}readyForEvents(){this._eventsReady||(this.ensureInit(),this._eventsReady=!0,this.gameEventManager.flushEventQueue())}on(e,r){let s=a=>{r(a.detail)},i=this.listenerWrappers.get(e);i||(i=new Map,this.listenerWrappers.set(e,i));let o=i.get(r);return o&&this.removeEventListener(e,o),i.set(r,s),this.addEventListener(e,s),()=>this.off(e,r)}off(e,r){let s=this.listenerWrappers.get(e),i=s?.get(r);!s||!i||(this.removeEventListener(e,i),s.delete(r),s.size===0&&this.listenerWrappers.delete(e))}addEventListener(e,r,s){super.addEventListener(e,r,s)}removeEventListener(e,r,s){super.removeEventListener(e,r,s)}loadScript(e){return ne("loadScript",[["src",y]],[e]),new Promise((r,s)=>{let i=document.createElement("script");i.type="text/javascript",i.crossOrigin="anonymous",i.src=e,i.onload=r,i.onerror=s,document.head.appendChild(i)})}updateLoadProgressZeroToOne(e){ne("updateLoadProgressZeroToOne",[["progress",U]],[e]),this.clearSetupWarning(),ke.postToParent(w.PROGRESS_UPDATE,{progress:e})}loadComplete(){this.clearSetupWarning(),!this.gameFinishedLoading&&(this.gameFinishedLoading=!0,this.heartbeatManager.start(),ke.postToParent(w.LOADING_COMPLETE,{}))}get gameLoaded(){return this.gameFinishedLoading}toggleOverlay(){this.overlayManager.toggleOverlay()}isFullscreen(){return this.fullscreenManager.isFullscreen()}async requestFullscreen(e){return this.fullscreenManager.requestFullscreen(e)}async toggleFullscreen(){return this.fullscreenManager.toggleFullscreen()}isMuted(){return this.audioManager.isMuted()}async requestMute(e){return this.audioManager.requestMute(e)}async toggleMute(){return this.audioManager.toggleMute()}getUser(){return this.formatResponse(this.wavedashUser)}getUsername(e){return e===void 0?this.wavedashUser.username:(ne("getUsername",[["userId",C("users")]],[e]),this.friendsManager.getUsername(e))}getUserId(){return this.wavedashUser.id}async getUserJwt(){u.debug("getUserJwt");try{let e=await this.ensureGameplayJwt();return this.formatResponse({success:!0,data:e})}catch(e){let r=e instanceof Error?e.message:String(e);return u.error("getUserJwt",r),this.formatResponse({success:!1,data:null,message:r})}}getLaunchParams(){return this.formatResponse(this.launchParams)}async listFriends(){return this.apiCall(this.friendsManager,"listFriends",[])}getUserAvatarUrl(e,r=this.AvatarSize.MEDIUM){return this.apiCallSync(this.friendsManager,"getUserAvatarUrl",[["userId",C("users")],["size",U]],e,r)}async getLeaderboard(e){return this.apiCall(this.leaderboardManager,"getLeaderboard",[["name",y]],e)}async getOrCreateLeaderboard(e,r,s){return this.apiCall(this.leaderboardManager,"getOrCreateLeaderboard",[["name",y],["sortOrder",re(lt,"LeaderboardSortOrder")],["displayType",re(dt,"LeaderboardDisplayType")]],e,r,s)}getLeaderboardEntryCount(e){return this.apiCallSync(this.leaderboardManager,"getLeaderboardEntryCount",[["leaderboardId",C("leaderboards")]],e)}async getMyLeaderboardEntries(e){return this.apiCall(this.leaderboardManager,"getMyLeaderboardEntries",[["leaderboardId",C("leaderboards")]],e)}async listLeaderboardEntriesAroundUser(e,r,s,i=!1){return this.apiCall(this.leaderboardManager,"listLeaderboardEntriesAroundUser",[["leaderboardId",C("leaderboards")],["countAhead",U],["countBehind",U],["friendsOnly",te]],e,r,s,i)}async listLeaderboardEntries(e,r,s,i=!1){return this.apiCall(this.leaderboardManager,"listLeaderboardEntries",[["leaderboardId",C("leaderboards")],["offset",U],["limit",U],["friendsOnly",te]],e,r,s,i)}async uploadLeaderboardScore(e,r,s,i){return this.apiCall(this.leaderboardManager,"uploadLeaderboardScore",[["leaderboardId",C("leaderboards")],["score",U],["keepBest",te],["ugcId",M(C("userGeneratedContent"))]],e,r,s,i)}async createUGCItem(e,r,s,i,o){return this.apiCall(this.ugcManager,"createUGCItem",[["ugcType",re(Ae,"UGCType")],["title",M(y)],["description",M(y)],["visibility",M(re(Me,"UGCVisibility"))],["filePath",M(y)]],e,r,s,i,o)}async updateUGCItem(e,r={}){if(typeof r=="string"){let s=r;try{r=JSON.parse(s)}catch(i){let o=`updateUGCItem: invalid JSON: ${s}`;return u.error(o,i),this.formatResponse({success:!1,data:null,message:o})}}return this.apiCall(this.ugcManager,"updateUGCItem",[["ugcId",C("userGeneratedContent")],["updates",M(kt({title:M(y),description:M(y),visibility:M(re(Me,"UGCVisibility")),filePath:M(y)}))]],e,r)}async deleteUGCItem(e){return this.apiCall(this.ugcManager,"deleteUGCItem",[["ugcId",C("userGeneratedContent")]],e)}async downloadUGCItem(e,r){return this.apiCall(this.ugcManager,"downloadUGCItem",[["ugcId",C("userGeneratedContent")],["filePath",y]],e,r)}async listUGCItems(e={}){if(typeof e=="string"){let r=e;try{e=JSON.parse(r)}catch(s){let i=`listUGCItems: invalid JSON: ${r}`;return u.error(i,s),this.formatResponse({success:!1,data:null,message:i})}}return this.apiCall(this.ugcManager,"listUGCItems",[["args",M((r,s)=>{let i=kt({createdBy:M(C("users")),ugcType:M(re(Ae,"UGCType")),titleSearch:M(y),numItems:M(U),continueCursor:M(y)})(r,s);if(i.continueCursor!==void 0&&(i.createdBy!==void 0||i.ugcType!==void 0||i.titleSearch!==void 0||i.numItems!==void 0))throw new Error(`${s}: continueCursor should be the only argument if present`);return i})]],e)}async deleteRemoteFile(e){return this.apiCall(this.fileSystemManager,"deleteRemoteFile",[["filePath",y]],e)}async downloadRemoteFile(e){return this.apiCall(this.fileSystemManager,"downloadRemoteFile",[["filePath",y]],e)}async remoteFileExists(e){return this.apiCall(this.fileSystemManager,"remoteFileExists",[["filePath",y]],e)}async uploadRemoteFile(e){return this.apiCall(this.fileSystemManager,"uploadRemoteFile",[["filePath",y]],e)}async listRemoteDirectory(e){return this.apiCall(this.fileSystemManager,"listRemoteDirectory",[["path",y]],e)}async downloadRemoteDirectory(e){return this.apiCall(this.fileSystemManager,"downloadRemoteDirectory",[["path",y]],e)}async writeLocalFile(e,r){return ne("writeLocalFile",[["filePath",y],["data",un]],[e,r]),await this.fileSystemManager.writeLocalFile(e,r)}async readLocalFile(e){return ne("readLocalFile",[["filePath",y]],[e]),await this.fileSystemManager.readLocalFile(e)}getAchievement(e){return this.apiCallSync(this.statsManager,"getAchievement",[["identifier",y]],e)}getStat(e){return this.apiCallSync(this.statsManager,"getStat",[["identifier",y]],e)}setAchievement(e,r=!1){return this.apiCallSync(this.statsManager,"setAchievement",[["identifier",y],["storeNow",te]],e,r)}setStat(e,r,s=!1){return this.apiCallSync(this.statsManager,"setStat",[["identifier",y],["value",U],["storeNow",te]],e,r,s)}async requestStats(){return this.apiCall(this.statsManager,"requestStats",[])}storeStats(){return this.apiCallSync(this.statsManager,"storeStats",[])}getP2PMaxPayloadSize(){return this.ensureInit(),this.apiCallSync(this.p2pManager,"getMaxPayloadSize",[])}getP2PMaxIncomingMessages(){return this.ensureInit(),this.apiCallSync(this.p2pManager,"getMaxIncomingMessages",[])}getP2POutgoingMessageBuffer(){return this.ensureInit(),this.apiCallSync(this.p2pManager,"getOutgoingMessageBuffer",[])}sendP2PMessage(e,r=0,s=!0,i,o=i.length){return e&&!this.p2pManager.isPeerReady(e)||!e&&!this.p2pManager.isBroadcastReady()?!1:this.p2pManager.sendP2PMessage(e,r,s,i,o)}broadcastP2PMessage(e=0,r=!0,s,i=s.length){return this.p2pManager.isBroadcastReady()?this.p2pManager.sendP2PMessage(void 0,e,r,s,i):!1}readP2PMessageFromChannel(e){return this.p2pManager.readMessageFromChannel(e)}drainP2PChannelToBuffer(e,r){return this.p2pManager.drainChannelToBuffer(e,r)}async createLobby(e,r){return this.apiCall(this.lobbyManager,"createLobby",[["visibility",re(ct,"LobbyVisibility")],["maxPlayers",M(U)]],e,r)}async joinLobby(e){return this.apiCall(this.lobbyManager,"joinLobby",[["lobbyId",C("lobbies")]],e)}async listAvailableLobbies(e=!1){return this.apiCall(this.lobbyManager,"listAvailableLobbies",[["friendsOnly",te]],e)}getLobbyUsers(e){return this.apiCallSync(this.lobbyManager,"getLobbyUsers",[["lobbyId",C("lobbies")]],e)}getNumLobbyUsers(e){return this.apiCallSync(this.lobbyManager,"getNumLobbyUsers",[["lobbyId",C("lobbies")]],e)}getLobbyHostId(e){return this.apiCallSync(this.lobbyManager,"getHostId",[["lobbyId",C("lobbies")]],e)}getLobbyData(e,r){return this.apiCallSync(this.lobbyManager,"getLobbyData",[["lobbyId",C("lobbies")],["key",y]],e,r)}setLobbyData(e,r,s){return this.apiCallSync(this.lobbyManager,"setLobbyData",[["lobbyId",C("lobbies")],["key",y],["value",dn(y,U,cn)]],e,r,s)}deleteLobbyData(e,r){return this.apiCallSync(this.lobbyManager,"deleteLobbyData",[["lobbyId",C("lobbies")],["key",y]],e,r)}async leaveLobby(e){return this.apiCall(this.lobbyManager,"leaveLobby",[["lobbyId",C("lobbies")]],e)}sendLobbyMessage(e,r){return this.apiCallSync(this.lobbyManager,"sendLobbyMessage",[["lobbyId",C("lobbies")],["message",y]],e,r)}async inviteUserToLobby(e,r){return this.apiCall(this.lobbyManager,"inviteUserToLobby",[["lobbyId",C("lobbies")],["userId",C("users")]],e,r)}async getLobbyInviteLink(e=!1){return this.apiCall(this.lobbyManager,"getLobbyInviteLink",[["copyToClipboard",te]],e)}async isEntitled_EXPERIMENTAL(e){return this.apiCall(this.paidContentManager,"isEntitled",[["contentId",y]],e)}async getEntitlements_EXPERIMENTAL(){return this.apiCall(this.paidContentManager,"getEntitlements",[])}async triggerPaywall_EXPERIMENTAL(e){return this.apiCall(this.paidContentManager,"triggerPaywall",[["contentIdentifier",y]],e)}async updateUserPresence(e){if(typeof e=="string"){let r=e;try{e=JSON.parse(r)}catch(s){let i=`updateUserPresence: invalid JSON: ${r}`;return u.error(i,s),this.formatResponse({success:!1,data:null,message:i})}}return this.apiCall(this.heartbeatManager,"updateUserPresence",[["data",ln]],e)}isGodot(){return this.engineInstance!==null&&this.engineInstance.type===Yt.GODOT}formatResponse(e){return this.isGodot()&&e!==null&&(Array.isArray(e)||Object.getPrototypeOf(e)===Object.prototype)?JSON.stringify(e):e}ensureInit(){if(!this._initialized)throw u.error("SDK not initialized. Call WavedashJS.init first."),new Error("SDK not initialized")}async apiCall(e,r,s,...i){u.debug(r,...i);try{ne(r,s,i);let o=await e[r](...i);return this.formatResponse({success:!0,data:o})}catch(o){let a=o instanceof Error?o.message:String(o);return u.error(r,a),this.formatResponse({success:!1,data:null,message:a})}}apiCallSync(e,r,s,...i){u.debug(r,...i);try{ne(r,s,i)}catch(o){let a=o instanceof Error?o.message:String(o);throw u.error(r,a),o}return this.formatResponse(e[r](...i))}setEngineInstance(e){this.engineInstance?Object.assign(this.engineInstance,e):this.engineInstance=e}getAuthToken(e=!1){if(!e&&this.gameplayJwt)return Promise.resolve(this.gameplayJwt);if(!e&&this.gameplayJwtPromise)return this.gameplayJwtPromise;let r=this.gameplayJwtPromise,i=(async()=>{r&&await r.catch(()=>{});let a=`/auth/refresh?${new URLSearchParams({[xe.Caller]:Nr.Wavedash}).toString()}`,c=await fetch(a,{method:"POST",credentials:"same-origin"});if(!c.ok)throw new Error(`Failed to refresh gameplay token: ${c.status}`);return c.text()})().then(o=>(this.gameplayJwt=o,this.gameplayJwtPromise===i&&ke.postToParent(w.GAMEPLAY_JWT_READY,{gameplayJwt:o}),o)).finally(()=>{this.gameplayJwtPromise===i&&(this.gameplayJwtPromise=null)});return this.gameplayJwtPromise=i,i}async ensureGameplayJwt(e=!1){return this.getAuthToken(e)}destroy(){if(!this.destroyed){this.destroyed=!0;for(let e of this.managers)e.destroy()}}setupSessionEndListeners(){ke.addEventListener(w.END_SESSION,()=>this.destroy())}setupSwCredsListener(){this.swMessenger.addEventListener(Zt.EMBED_CREDS_REQUEST,async(e,r)=>{let s;try{s=await this.ensureGameplayJwt()}catch(i){u.warn("Failed to resolve JWT for creds-request",i);return}r({type:Zt.EMBED_CREDS_RESPONSE,payload:{gameplayJwt:s}})})}};function hn(){let n=window.Wavedash;if(n)return n;let t=new URLSearchParams(window.location.search).get(xe.SdkConfig);if(!t)throw new Error(`Wavedash SDK: missing ?${xe.SdkConfig}= query param on the iframe URL.`);let e;try{e=JSON.parse(t)}catch(s){let i=s instanceof Error?s.message:String(s);throw new Error(`Wavedash SDK: failed to parse ?${xe.SdkConfig}= as JSON: ${i}`)}an(e.parentOrigin);let r=new or(e);return window.Wavedash=r,window.WavedashJS=r,r}hn();})();
|