const t=t=>typeof t,e="",a=t(e),s="message",n=t=>null==t,o=(t,e,a)=>n(t)?a?.():e(t),r=e=>t(e)==a,i=t=>Array.isArray(t),c=t=>t.length,d=t=>{throw Error(t)},y=async(t,e,a)=>{try{return await t()}catch(t){e?.(t)}},u=(t,e)=>t.forEach(e),l=(t,...e)=>t.push(...e),g=t=>t.shift(),p=Object,w=t=>p.getPrototypeOf(t),h=p.keys,v=p.freeze,f=t=>(t=>!n(t)&&o(w(t),t=>t==p.prototype||n(w(t)),()=>!0))(t)&&0==(t=>c(h(t)))(t),C=JSON.stringify,S=JSON.parse,A=t=>C(t,(t,e)=>e instanceof Map?p.fromEntries([...e]):e),L=t=>n(t)||0==(t=>t?.size??0)(t),P=(t,e)=>t?.forEach(e),b=(t,e)=>t?.delete(e),m=t=>new Map(t),M=(t,e)=>t?.get(e),E=(t,e,a)=>n(a)?(b(t,e),t):t?.set(e,a),O=(t,e,a,s)=>{var n,o;return n=t,o=e,n?.has(o)?s?.(M(t,e)):E(t,e,a()),M(t,e)},T=(t,e,a,s,n=0)=>o((a?O:M)(t,e[n],n>c(e)-2?a:m),o=>{if(n>c(e)-2)return s?.(o)&&E(t,e[n]),o;const r=T(o,e,a,s,n+1);return L(o)&&E(t,e[n]),r}),j=/^\d+$/,k=t=>new Set(i(t)||n(t)?t:[t]),x=m(),z=m(),D=(t,a,s,r,p,w,h,C={},S=0,A=[])=>{let D,J,N,F=0,U=0,W=0;O(x,A,()=>0),O(z,A,()=>[]);const $=m(),[q,B,G,H,I]=((t=1,e,a)=>1!=t&&e.isMergeable()?[1,e.getMergeableContent,()=>e.getTransactionMergeableChanges(!a),([[t],[e]])=>!f(t)||!f(e),e.setDefaultContent]:2!=t?[0,e.getContent,e.getTransactionChanges,([t,e])=>!f(t)||!f(e),e.setContent]:d("Store type not supported by this Persister"))(h,t,S),[K,Q,R]=(()=>{let t;const[a,s]=(()=>{const t=[];let a=0;return[s=>(s?g(t):null)??e+a++,e=>{j.test(e)&&c(t)<1e3&&l(t,e)}]})(),r=m();return[(s,n,o,i=[],c=()=>[])=>{t??=ot;const d=a(1);var y,u;return E(r,d,[s,n,o,i,c]),y=T(n,o??[e],k),u=d,y?.add(u),d},(a,s,...n)=>u(((t,a=[e])=>{const s=[],n=(t,e)=>e==c(a)?l(s,t):null===a[e]?P(t,t=>n(t,e+1)):u([a[e],null],a=>n(M(t,a),e+1));return n(t,0),s})(a,s),e=>P(e,e=>M(r,e)[0](t,...s??[],...n))),t=>o(M(r,t),([,a,n])=>(T(a,n??[e],void 0,e=>(b(e,t),L(e)?1:0)),E(r,t),s(t),n)),e=>o(M(r,e),([e,,a=[],s,o])=>{const r=(...i)=>{const d=c(i);d==c(a)?e(t,...i,...o(i)):n(a[d])?u(s[d]?.(...i)??[],t=>r(...i,t)):r(...i,a[d])};r()})]})(),V=t=>{t!=F&&(F=t,Q($,void 0,F))},X=e=>{(q&&i(e?.[0])?1===e?.[2]?t.applyMergeableChanges:t.setMergeableContent:1===e?.[2]?t.applyChanges:t.setContent)(e)},Y=async t=>(2!=F&&(V(1),U++,await nt(async()=>{await y(async()=>{const e=await a();i(e)?X(e):t?I(t):d("Content is not an array: "+e)},()=>{t&&I(t)}),V(0)})),ot),Z=async t=>(_(),await Y(t),await y(async()=>J=await r(async(t,e)=>{e||t?2!=F&&(V(1),U++,X(e??t),V(0)):await Y()}),w),ot),_=async()=>(J&&(await y(()=>p(J),w),J=void 0),ot),tt=async t=>(1!=F&&(V(2),W++,await nt(async()=>{await y(()=>s(B,t),w),V(0)})),ot),et=async()=>(at(),await tt(),N=t.addDidFinishTransactionListener(()=>{const t=G();H(t)&&tt(t)}),ot),at=async()=>(N&&(t.delListener(N),N=void 0),ot),st=async(t=!1)=>{const[e,a]=t?[at,_]:[_,at];return await e(),await a(),ot},nt=async(...t)=>(l(M(z,A),...t),await(async()=>{if(!M(x,A)){for(E(x,A,1);!n(D=g(M(z,A)));)await y(D,w);E(x,A,0)}})(),ot),ot={load:Y,startAutoLoad:Z,stopAutoLoad:_,isAutoLoading:()=>!n(J),save:tt,startAutoSave:et,stopAutoSave:at,isAutoSaving:()=>!n(N),startAutoPersisting:async(t,e=!1)=>{const[a,s]=e?[et,Z]:[Z,et];return await a(t),await s(t),ot},stopAutoPersisting:st,getStatus:()=>F,addStatusListener:t=>K(t,$),delListener:e=>(R(e),t),schedule:nt,getStore:()=>t,destroy:()=>(M(z,A).splice(0,void 0),st()),getStats:()=>({loads:U,saves:W}),...C};return v(ot)},J="/store",N=(t,a,n,i)=>{const{host:d,room:y}=a.partySocketOptions,{storeProtocol:u="https",storePath:l=J,messagePrefix:g=e}={...r(n)?{storeProtocol:n}:n},p=u+"://"+d+"/parties/"+a.name+"/"+y+l,w=async t=>await(await fetch(p,{...t?{method:"PUT",body:A(t)}:{},mode:"cors",cache:"no-store"})).json();return D(t,w,async(t,e)=>{var s;e?a.send(g+"s"+(r(s=e)?s:A(s))):await w(t())},t=>{const e=e=>o(((t,e)=>{const a=c(t);return((t,e)=>t.startsWith(e))(e,t)?[e[a],S((s=e,n=a+1,s.slice(n,void 0)))]:void 0;var s,n})(g,e.data),([e,a])=>{"s"==e&&t(void 0,a)});return a.addEventListener(s,e),e},t=>{a.removeEventListener(s,t)},i,1,{getConnection:()=>a})};export{N as createPartyKitPersister};
