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)??"",T=(e="")=>C(((e=[])=>l.fromEntries(e))(),e),m=f(),L=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},P=f(),R=(t,a,s,c)=>{const l=E();return((t,a,s,c,l,g,u,y,H={})=>{let D,P=0,R=0;const V=f(),x=(e,t,s,n)=>{P++,g?.(e,t,s,n),a(e,t,s,n)},z=async(t,a,s="")=>new e(((e,n)=>{const o=E(),r=setTimeout((()=>{v(V,o),n(`No response from ${t??"anyone"} to ${o}, `+a)}),1e3*l);M(V,o,[t,(t,a)=>{clearTimeout(r),v(V,o),e([t,a])}]),x(t,o,a,s)})),O=(e,[t,a])=>{h(t,(([t,a],s)=>{const n=w(e[0],s,T);h(t,(([e,t],a)=>{const s=w(n[0],a,T);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 z(e,1));const[s,o]=a,[r,i]=t.getMergeableContentHashes();let c=T();if(r!=s){const[a,s]=(await z(e,4,t.getMergeableTableHashes()))[0];if(c=a,!p(s)){const[a,n]=(await z(e,5,t.getMergeableRowHashes(s)))[0];if(O(c,a),!p(n)){const a=(await z(e,6,t.getMergeableCellHashes(n)))[0];O(c,a)}}}return[c,i==o?T():(await z(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(m,y,(()=>0)),A(L,y,(()=>[]));const[T,H,D,E,P]=((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),R=t=>{(T&&r(t?.[0])?1===t?.[2]?e.applyMergeableChanges:e.setMergeableContent:1===t?.[2]?e.applyChanges:e.setContent)(t)},V=async e=>(2!=f&&(f=1,C++,await j((async()=>{try{const e=await t();r(e)?R(e):i("Content is not an array "+e)}catch(t){l?.(t),e&&P(e)}f=0}))),k),x=()=>(w&&(c(w),w=void 0),k),z=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(L,y),...e),await(async()=>{if(!b(m,y)){for(M(m,y,1);!n((e=b(L,y),h=e.shift()));)try{await h()}catch(e){l?.(e)}M(m,y,0)}var e})(),k),k={load:V,startAutoLoad:async e=>{await x().load(e);try{w=await s((async(e,t)=>{t||e?2!=f&&(f=1,C++,R(t??e),f=0):await V()}))}catch(e){l?.(e)}return k},stopAutoLoad:x,isAutoLoading:()=>!n(w),save:z,startAutoSave:async()=>(await O().save(),v=e.addDidFinishTransactionListener((()=>{const e=D();E(e)&&z(e)})),k),stopAutoSave:O,isAutoSaving:()=>!n(v),schedule:j,getStore:()=>e,destroy:()=>(b(L,y).splice(0,void 0),x().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?x(null,null,3,a):x(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:P,receives:R}),...H});return s(((e,a,s,r)=>{R++,u?.(e,a,s,r),0==s?o(b(V,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=>{x(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))(P,((e,t)=>o(t,e)))):b(P,e)?.(l,t,a,s);var o}),0)}),(e=>{M(P,l,e)}),(()=>{v(P,l)}),.01,a,s,c)};export{R as createLocalSynchronizer};
