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