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