const e=e=>typeof e,t="",n=e(t),o=e=>null==e,r=(e,t,n)=>o(e)?null==n?void 0:n():t(e),l=t=>e(t)==n,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=>!o(e)&&r(c(e),(e=>e==u.prototype||o(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),O=(e,t)=>null==e?void 0:e.get(t),P=(e,t,n)=>{return o(n)?(l=t,null==(r=e)||r.delete(l),e):null==e?void 0:e.set(t,n);var r,l},m=(e,t,n,o)=>{var r,l,i;return l=t,null!=(i=null==(r=e)?void 0:r.has(l))&&i||P(e,t,n()),O(e,t)};var w=Object.defineProperty,j=Object.getOwnPropertySymbols,C=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable,A=(e,t,n)=>t in e?w(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,L=(e,t,n)=>new Promise(((o,r)=>{var l=e=>{try{a(n.next(e))}catch(e){r(e)}},i=e=>{try{a(n.throw(e))}catch(e){r(e)}},a=e=>e.done?o(e.value):Promise.resolve(e.value).then(l,i);a((n=n.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,n)=>t in e?E(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,z=(e,t)=>{for(var n in t||(t={}))I.call(t,n)&&N(e,n,t[n]);if(k)for(var n of k(t))J.call(t,n)&&N(e,n,t[n]);return e},F=(e,t,n)=>new Promise(((o,r)=>{var l=e=>{try{a(n.next(e))}catch(e){r(e)}},i=e=>{try{a(n.throw(e))}catch(e){r(e)}},a=e=>e.done?o(e.value):Promise.resolve(e.value).then(l,i);a((n=n.apply(e,t)).next())}));const U="message",W=(e,n,u,c)=>{const{host:d,room:p}=n.partySocketOptions,{storeProtocol:b="https",storePath:w=g,messagePrefix:E=t}=z({},l(u)?{storeProtocol:u}:u),k=b+"://"+d+"/parties/"+n.name+"/"+p+w,I=e=>F(void 0,null,(function*(){return yield(yield fetch(k,(t=z({},e?{method:"PUT",body:h(e)}:{}),n={mode:"cors",cache:"no-store"},T(t,D(n))))).json();var t,n}));return((e,t,n,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 q((()=>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}))))),B})),F=()=>(h&&(a(h),h=void 0),B),U=e=>L(void 0,null,(function*(){return 1!=b&&(b=2,E++,yield q((()=>L(void 0,null,(function*(){try{yield n(D,e)}catch(e){null==u||u(e)}b=0}))))),B})),W=()=>(r(g,e.delListener),g=void 0,B),q=(...e)=>L(void 0,null,(function*(){return((e,...t)=>{e.push(...t)})(O(x,p),...e),yield L(void 0,null,(function*(){if(!O(M,p)){for(P(M,p,1);!o((e=O(x,p),f=e.shift()));)try{yield f()}catch(e){null==u||u(e)}P(M,p,0)}var e})),B})),B=((e,t)=>{for(var n in t||(t={}))C.call(t,n)&&A(e,n,t[n]);if(j)for(var n of j(t))S.call(t,n)&&A(e,n,t[n]);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 B})),stopAutoLoad:F,isAutoLoading:()=>!o(h),save:U,startAutoSave:()=>L(void 0,null,(function*(){return yield W().save(),g=e.addDidFinishTransactionListener((()=>{const e=k();I(e)&&U(e)})),B})),stopAutoSave:W,isAutoSaving:()=>!o(g),schedule:q,getStore:()=>e,destroy:()=>(O(x,p).splice(0,void 0),F().stopAutoSave()),getStats:()=>({loads:w,saves:E})},d);return v(B)})(e,(()=>F(void 0,null,(function*(){return yield I()}))),((e,t)=>F(void 0,null,(function*(){var o;t?n.send(E+"s"+(l(o=t)?o:h(o))):yield I(e())}))),(e=>{const t=t=>r(((e,t)=>{const n=a(e);return((e,t)=>e.startsWith(t))(t,e)?[t[n],f((o=t,r=n+1,o.slice(r,void 0)))]:void 0;var o,r})(E,t.data),(([t,n])=>{"s"==t&&e(void 0,n)}));return n.addEventListener(U,t),t}),(e=>{n.removeEventListener(U,e)}),c,1,{getConnection:()=>n})};export{W as createPartyKitPersister};
