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