var e,t;e=this,t=function(e){"use strict";const t=clearInterval,n=e=>null==e,o=(e,t,o)=>n(e)?null==o?void 0:o():t(e),r=e=>Array.isArray(e),l=e=>{throw Error(e)},i=Object,a=e=>i.getPrototypeOf(e),s=i.keys,u=i.freeze,d=e=>(e=>!n(e)&&o(a(e),(e=>e==i.prototype||n(a(e))),(()=>!0)))(e)&&0==(e=>s(e).length)(e),c=JSON.stringify,y=JSON.parse,v=e=>new Map(e),f=(e,t)=>null==e?void 0:e.get(t),p=(e,t,o)=>{return n(o)?(l=t,null==(r=e)||r.delete(l),e):null==e?void 0:e.set(t,o);var r,l},h=(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()),f(e,t)};var g=Object.defineProperty,b=Object.getOwnPropertySymbols,m=Object.prototype.hasOwnProperty,C=Object.prototype.propertyIsEnumerable,P=(e,t,n)=>t in e?g(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,O=(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 S=v(),A=v();var w=(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 T=e=>e.headers.get("ETag");e.createRemotePersister=(e,a,s,v=5,g)=>{let x;return((e,t,i,a,s,c,y,v={},g=[])=>{let w,T,x,j=0,M=0,E=0;h(S,g,(()=>0)),h(A,g,(()=>[]));const[L,D,I,J,N]=((e=1,t)=>1!=e&&t.isMergeable()?[1,t.getMergeableContent,t.getTransactionMergeableChanges,([[e],[t]])=>!d(e)||!d(t),t.setDefaultContent]:2!=e?[0,t.getContent,t.getTransactionChanges,([e,t])=>!d(e)||!d(t),t.setContent]:l("Store type not supported by this Persister"))(y,e),R=t=>{(L&&r(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)},k=e=>O(void 0,null,(function*(){return 2!=j&&(j=1,M++,yield H((()=>O(void 0,null,(function*(){try{const e=yield t();r(e)?R(e):l("Content is not an array "+e)}catch(t){null==c||c(t),e&&N(e)}j=0}))))),U})),z=()=>(T&&(s(T),T=void 0),U),B=e=>O(void 0,null,(function*(){return 1!=j&&(j=2,E++,yield H((()=>O(void 0,null,(function*(){try{yield i(D,e)}catch(e){null==c||c(e)}j=0}))))),U})),F=()=>(o(x,e.delListener),x=void 0,U),H=(...e)=>O(void 0,null,(function*(){return((e,...t)=>{e.push(...t)})(f(A,g),...e),yield O(void 0,null,(function*(){if(!f(S,g)){for(p(S,g,1);!n((e=f(A,g),w=e.shift()));)try{yield w()}catch(e){null==c||c(e)}p(S,g,0)}var e})),U})),U=((e,t)=>{for(var n in t||(t={}))m.call(t,n)&&P(e,n,t[n]);if(b)for(var n of b(t))C.call(t,n)&&P(e,n,t[n]);return e})({load:k,startAutoLoad:e=>O(void 0,null,(function*(){yield z().load(e);try{T=yield a(((e,t)=>O(void 0,null,(function*(){t||e?2!=j&&(j=1,M++,R(null!=t?t:e),j=0):yield k()}))))}catch(e){null==c||c(e)}return U})),stopAutoLoad:z,isAutoLoading:()=>!n(T),save:B,startAutoSave:()=>O(void 0,null,(function*(){return yield F().save(),x=e.addDidFinishTransactionListener((()=>{const e=I();J(e)&&B(e)})),U})),stopAutoSave:F,isAutoSaving:()=>!n(x),schedule:H,getStore:()=>e,destroy:()=>(f(A,g).splice(0,void 0),z().stopAutoSave()),getStats:()=>({loads:M,saves:E})},v);return u(U)})(e,(()=>w(void 0,null,(function*(){const e=yield fetch(a);return x=T(e),y(yield e.text())}))),(e=>w(void 0,null,(function*(){return yield fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:(t=e(),c(t,((e,t)=>t instanceof Map?i.fromEntries([...t]):t)))});var t}))),(e=>setInterval((()=>w(void 0,null,(function*(){const t=yield fetch(a,{method:"HEAD"}),o=T(t);n(x)||n(o)||o==x||(x=o,e())}))),1e3*v)),(e=>t(e)),g,1,{getUrls:()=>[a,s]})}},"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TinyBasePersisterRemote={});
