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