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