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