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