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