"use strict";const e=Promise,t=globalThis,a=Math,s=a.floor,n=e=>null==e,o=(e,t,a)=>n(e)?a?.():t(e),r=e=>Array.isArray(e),i=e=>{throw Error(e)},c=(e,t)=>e.map(t),l=Object,g=e=>l.getPrototypeOf(e),u=l.entries,y=l.keys,d=l.freeze,h=(e,t)=>((e,t)=>e.forEach(t))(u(e),(([e,a])=>t(a,e))),p=e=>(e=>!n(e)&&o(g(e),(e=>e==l.prototype||n(g(e))),(()=>!0)))(e)&&0==(e=>y(e).length)(e),w=(e,t,a)=>(((e,t)=>t in e)(e,t)||(e[t]=a()),e[t]),v=(e,t)=>e?.delete(t),f=e=>new Map(e),b=(e,t)=>e?.get(t),M=(e,t,a)=>n(a)?(v(e,t),e):e?.set(t,a),A=(e,t,a,s)=>{var n,o;return n=e,o=t,n?.has(o)||M(e,t,a()),b(e,t)};new t.TextEncoder;const C=(e,t)=>t?[e,t]:[e],S=(e,t)=>((e??"")>(t??"")?e:t)??"",L=(e="")=>C(((e=[])=>l.fromEntries(e))(),e),T=f(),m=f(),H="-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".split("");f(c(H,((e,t)=>[e,t])));const D=t.crypto?e=>t.crypto.getRandomValues(e):e=>c(e,(()=>s(256*a.random()))),E=(e=16)=>{return t=(e,t)=>e+H[63&t],D(new Uint8Array(e)).reduce(t,"");var t},z=f();exports.createLocalSynchronizer=(t,a,s,c)=>{const l=E();return((t,a,s,c,l,g,u,y,H={})=>{let D,z=0,P=0;const R=f(),V=(e,t,s,n)=>{z++,g?.(e,t,s,n),a(e,t,s,n)},x=async(t,a,s="")=>new e(((e,n)=>{const o=E(),r=setTimeout((()=>{v(R,o),n(`No response from ${t??"anyone"} to ${o}, `+a)}),1e3*l);M(R,o,[t,(t,a)=>{clearTimeout(r),v(R,o),e([t,a])}]),V(t,o,a,s)})),O=(e,[t,a])=>{h(t,(([t,a],s)=>{const n=w(e[0],s,L);h(t,(([e,t],a)=>{const s=w(n[0],a,L);h(e,(([e,t],a)=>s[0][a]=C(e,t))),s[1]=S(s[1],t)})),n[1]=S(n[1],a)})),e[1]=S(e[1],a)},j=async(e=null,a)=>{n(a)&&([a,e]=await x(e,1));const[s,o]=a,[r,i]=t.getMergeableContentHashes();let c=L();if(r!=s){const[a,s]=(await x(e,4,t.getMergeableTableHashes()))[0];if(c=a,!p(s)){const[a,n]=(await x(e,5,t.getMergeableRowHashes(s)))[0];if(O(c,a),!p(n)){const a=(await x(e,6,t.getMergeableCellHashes(n)))[0];O(c,a)}}}return[c,i==o?L():(await x(e,7,t.getMergeableValueHashes()))[0],1]},k=((e,t,a,s,c,l,g,u={},y=[])=>{let h,w,v,f=0,C=0,S=0;A(T,y,(()=>0)),A(m,y,(()=>[]));const[L,H,D,E,z]=((e=1,t)=>1!=e&&t.isMergeable()?[1,t.getMergeableContent,t.getTransactionMergeableChanges,([[e],[t]])=>!p(e)||!p(t),t.setDefaultContent]:2!=e?[0,t.getContent,t.getTransactionChanges,([e,t])=>!p(e)||!p(t),t.setContent]:i("Store type not supported by this Persister"))(g,e),P=t=>{(L&&r(t?.[0])?1===t?.[2]?e.applyMergeableChanges:e.setMergeableContent:1===t?.[2]?e.applyChanges:e.setContent)(t)},R=async e=>(2!=f&&(f=1,C++,await j((async()=>{try{const e=await t();r(e)?P(e):i("Content is not an array "+e)}catch(t){l?.(t),e&&z(e)}f=0}))),k),V=()=>(w&&(c(w),w=void 0),k),x=async e=>(1!=f&&(f=2,S++,await j((async()=>{try{await a(H,e)}catch(e){l?.(e)}f=0}))),k),O=()=>(o(v,e.delListener),v=void 0,k),j=async(...e)=>(((e,...t)=>{e.push(...t)})(b(m,y),...e),await(async()=>{if(!b(T,y)){for(M(T,y,1);!n((e=b(m,y),h=e.shift()));)try{await h()}catch(e){l?.(e)}M(T,y,0)}var e})(),k),k={load:R,startAutoLoad:async e=>{await V().load(e);try{w=await s((async(e,t)=>{t||e?2!=f&&(f=1,C++,P(t??e),f=0):await R()}))}catch(e){l?.(e)}return k},stopAutoLoad:V,isAutoLoading:()=>!n(w),save:x,startAutoSave:async()=>(await O().save(),v=e.addDidFinishTransactionListener((()=>{const e=D();E(e)&&x(e)})),k),stopAutoSave:O,isAutoSaving:()=>!n(v),schedule:j,getStore:()=>e,destroy:()=>(b(m,y).splice(0,void 0),V().stopAutoSave()),getStats:()=>({loads:C,saves:S}),...u};return d(k)})(t,(async()=>{const e=await j();return p(e[0][0])&&p(e[1][0])?void 0:e}),(async(e,a)=>a?V(null,null,3,a):V(null,null,2,t.getMergeableContentHashes())),(e=>D=e),(()=>D=void 0),y,2,{startSync:async e=>await(await k.startAutoLoad(e)).startAutoSave(),stopSync:()=>k.stopAutoLoad().stopAutoSave(),destroy:()=>(c(),k.stopSync()),getSynchronizerStats:()=>({sends:z,receives:P}),...H});return s(((e,a,s,r)=>{P++,u?.(e,a,s,r),0==s?o(b(R,a),(([t,a])=>n(t)||t==e?a(r,e):0)):2==s&&k.isAutoLoading()?j(e,r).then((e=>{D?.(void 0,e)})):3==s&&k.isAutoLoading()?D?.(void 0,r):o(1==s&&k.isAutoSaving()?t.getMergeableContentHashes():4==s?t.getMergeableTableDiff(r):5==s?t.getMergeableRowDiff(r):6==s?t.getMergeableCellDiff(r):7==s?t.getMergeableValueDiff(r):void 0,(t=>{V(e,a,0,t)}))})),k})(t,((e,t,a,s)=>{setTimeout((()=>{return n(e)?(o=(e,n)=>e!=l?n(l,t,a,s):0,((e,t)=>e?.forEach(t))(z,((e,t)=>o(t,e)))):b(z,e)?.(l,t,a,s);var o}),0)}),(e=>{M(z,l,e)}),(()=>{v(z,l)}),.01,a,s,c)};
