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