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