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