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