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