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