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