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