var t,e;t=this,e=function(t){"use strict";const e=t=>typeof t,a="",s=e(a),o=t=>null==t,n=(t,e,a)=>o(t)?a?.():e(t),r=t=>e(t)==s,i=t=>Array.isArray(t),c=t=>t.length,y=t=>{throw Error(t)},d=Object,p=t=>d.getPrototypeOf(t),l=d.keys,g=d.freeze,h=t=>(t=>!o(t)&&n(p(t),(t=>t==d.prototype||o(p(t))),(()=>!0)))(t)&&0==(t=>c(l(t)))(t),u=JSON.stringify,f=JSON.parse,v=t=>u(t,((t,e)=>e instanceof Map?d.fromEntries([...e]):e)),w="/store",C=t=>new Map(t),b=(t,e)=>t?.get(e),P=(t,e,a)=>{return o(a)?(s=t,n=e,s?.delete(n),t):t?.set(e,a);var s,n},m=(t,e,a,s)=>{var o,n;return o=t,n=e,o?.has(n)||P(t,e,a()),b(t,e)},S=C(),A=C(),L="message";t.createPartyKitPersister=(t,e,s,d)=>{const{host:p,room:l}=e.partySocketOptions,{storeProtocol:u="https",storePath:C=w,messagePrefix:M=a}={...r(s)?{storeProtocol:s}:s},T=u+"://"+p+"/parties/"+e.name+"/"+l+C,O=async t=>await(await fetch(T,{...t?{method:"PUT",body:v(t)}:{},mode:"cors",cache:"no-store"})).json();return((t,e,a,s,r,c,d,p={},l=[])=>{let u,f,v,w=0,C=0,L=0;m(S,l,(()=>0)),m(A,l,(()=>[]));const[M,T,O,x,E]=((t=1,e)=>1!=t&&e.isMergeable()?[1,e.getMergeableContent,e.getTransactionMergeableChanges,([[t],[e]])=>!h(t)||!h(e),e.setDefaultContent]:2!=t?[0,e.getContent,e.getTransactionChanges,([t,e])=>!h(t)||!h(e),e.setContent]:y("Store type not supported by this Persister"))(d,t),j=e=>{(M&&i(e?.[0])?1===e?.[2]?t.applyMergeableChanges:t.setMergeableContent:1===e?.[2]?t.applyChanges:t.setContent)(e)},k=async t=>(2!=w&&(w=1,C++,await N((async()=>{try{const t=await e();i(t)?j(t):y("Content is not an array "+t)}catch(e){c?.(e),t&&E(t)}w=0}))),z),D=()=>(f&&(r(f),f=void 0),z),J=async t=>(1!=w&&(w=2,L++,await N((async()=>{try{await a(T,t)}catch(t){c?.(t)}w=0}))),z),K=()=>(n(v,t.delListener),v=void 0,z),N=async(...t)=>(((t,...e)=>{t.push(...e)})(b(A,l),...t),await(async()=>{if(!b(S,l)){for(P(S,l,1);!o((t=b(A,l),u=t.shift()));)try{await u()}catch(t){c?.(t)}P(S,l,0)}var t})(),z),z={load:k,startAutoLoad:async t=>{await D().load(t);try{f=await s((async(t,e)=>{e||t?2!=w&&(w=1,C++,j(e??t),w=0):await k()}))}catch(t){c?.(t)}return z},stopAutoLoad:D,isAutoLoading:()=>!o(f),save:J,startAutoSave:async()=>(await K().save(),v=t.addDidFinishTransactionListener((()=>{const t=O();x(t)&&J(t)})),z),stopAutoSave:K,isAutoSaving:()=>!o(v),schedule:N,getStore:()=>t,destroy:()=>(b(A,l).splice(0,void 0),D().stopAutoSave()),getStats:()=>({loads:C,saves:L}),...p};return g(z)})(t,(async()=>await O()),(async(t,a)=>{var s;a?e.send(M+"s"+(r(s=a)?s:v(s))):await O(t())}),(t=>{const a=e=>n(((t,e)=>{const a=c(t);return((t,e)=>t.startsWith(e))(e,t)?[e[a],f((s=e,o=a+1,s.slice(o,void 0)))]:void 0;var s,o})(M,e.data),(([e,a])=>{"s"==e&&t(void 0,a)}));return e.addEventListener(L,a),a}),(t=>{e.removeEventListener(L,t)}),d,1,{getConnection:()=>e})}},"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).TinyBasePersisterPartyKitClient={});
