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