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