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,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==c.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),M=(e,t,a)=>n(a)?(v(e,t),e):e?.set(t,a),C=(e,t,a,s)=>{var n,o;return n=e,o=t,n?.has(o)||M(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=[])=>c.fromEntries(e))(),e),L=b(),T=b(),H="-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".split("");b(l(H,((e,t)=>[e,t])));const D=t.crypto?e=>t.crypto.getRandomValues(e):e=>l(e,(()=>s(256*a.random()))),E=(e=16)=>{return t=(e,t)=>e+H[63&t],D(new Uint8Array(e)).reduce(t,"");var t},P=(t,a,s,l,c)=>{const g=E(),u=new BroadcastChannel(a);return((t,a,s,l,c,g,u,y,H={})=>{let D,P=0,R=0;const V=b(),x=(e,t,s,n)=>{P++,g?.(e,t,s,n),a(e,t,s,n)},z=async(t,a,s="")=>new e(((e,n)=>{const o=E(),r=setTimeout((()=>{v(V,o),n(`No response from ${t??"anyone"} to ${o}, `+a)}),1e3*c);M(V,o,[t,(t,a)=>{clearTimeout(r),v(V,o),e([t,a])}]),x(t,o,a,s)})),N=(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)},O=async(e=null,a)=>{n(a)&&([a,e]=await z(e,1));const[s,o]=a,[r,i]=t.getMergeableContentHashes();let l=m();if(r!=s){const[a,s]=(await z(e,4,t.getMergeableTableHashes()))[0];if(l=a,!p(s)){const[a,n]=(await z(e,5,t.getMergeableRowHashes(s)))[0];if(N(l,a),!p(n)){const a=(await z(e,6,t.getMergeableCellHashes(n)))[0];N(l,a)}}}return[l,i==o?m():(await z(e,7,t.getMergeableValueHashes()))[0],1]},j=((e,t,a,s,l,c,g,u={},y=[])=>{let h,w,v,b=0,A=0,S=0;C(L,y,(()=>0)),C(T,y,(()=>[]));const[m,H,D,E,P]=((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),R=t=>{(m&&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)?R(e):i("Content is not an array "+e)}catch(t){c?.(t),e&&P(e)}b=0}))),j),x=()=>(w&&(l(w),w=void 0),j),z=async e=>(1!=b&&(b=2,S++,await O((async()=>{try{await a(H,e)}catch(e){c?.(e)}b=0}))),j),N=()=>(o(v,e.delListener),v=void 0,j),O=async(...e)=>(((e,...t)=>{e.push(...t)})(f(T,y),...e),await(async()=>{if(!f(L,y)){for(M(L,y,1);!n((e=f(T,y),h=e.shift()));)try{await h()}catch(e){c?.(e)}M(L,y,0)}var e})(),j),j={load:V,startAutoLoad:async e=>{await x().load(e);try{w=await s((async(e,t)=>{t||e?2!=b&&(b=1,A++,R(t??e),b=0):await V()}))}catch(e){c?.(e)}return j},stopAutoLoad:x,isAutoLoading:()=>!n(w),save:z,startAutoSave:async()=>(await N().save(),v=e.addDidFinishTransactionListener((()=>{const e=D();E(e)&&z(e)})),j),stopAutoSave:N,isAutoSaving:()=>!n(v),schedule:O,getStore:()=>e,destroy:()=>(f(T,y).splice(0,void 0),x().stopAutoSave()),getStats:()=>({loads:A,saves:S}),...u};return d(j)})(t,(async()=>{const e=await O();return p(e[0][0])&&p(e[1][0])?void 0:e}),(async(e,a)=>a?x(null,null,3,a):x(null,null,2,t.getMergeableContentHashes())),(e=>D=e),(()=>D=void 0),y,2,{startSync:async e=>await(await j.startAutoLoad(e)).startAutoSave(),stopSync:()=>j.stopAutoLoad().stopAutoSave(),destroy:()=>(l(),j.stopSync()),getSynchronizerStats:()=>({sends:P,receives:R}),...H});return s(((e,a,s,r)=>{R++,u?.(e,a,s,r),0==s?o(f(V,a),(([t,a])=>n(t)||t==e?a(r,e):0)):2==s&&j.isAutoLoading()?O(e,r).then((e=>{D?.(void 0,e)})):3==s&&j.isAutoLoading()?D?.(void 0,r):o(1==s&&j.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)}))})),j})(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,l,c,{getChannelName:()=>a})};export{P as createBroadcastChannelSynchronizer};
