var e,t;e=this,t=function(e){"use strict";const t=e=>null==e,a=(e,a,n)=>t(e)?n?.():a(e),n=e=>Array.isArray(e),s=e=>{throw Error(e)},o=Object,r=e=>o.getPrototypeOf(e),i=o.entries,c=o.keys,d=o.freeze,y=(e=[])=>o.fromEntries(e),g=(e,t)=>a(e,(e=>e[t])),l=(e,t)=>t in e,u=(e,t)=>(delete e[t],e),p=(e,t)=>((e,t)=>e.map(t))(i(e),(([e,a])=>t(a,e))),h=e=>c(e).length,f=e=>(e=>!t(e)&&a(r(e),(e=>e==o.prototype||t(r(e))),(()=>!0)))(e)&&0==h(e),v=(e,t,a)=>(l(e,t)||(e[t]=a()),e[t]),w=e=>new Map(e),b=(e,t)=>e?.get(t),A=(e,a,n)=>{return t(n)?(s=e,o=a,s?.delete(o),e):e?.set(a,n);var s,o},C=(e,t,a,n)=>{var s,o;return s=e,o=t,s?.has(o)||A(e,t,a()),b(e,t)},m=w(),S=w(),L=(e,t)=>[e[t].t,e[t].v],M=(e,a,n,s)=>{const o=t(a)?e:v(e,a,(()=>({})));let r;return p(n,((e,t)=>{s(o,t,e)&&(r=1)})),p(o,((e,t)=>{l(n,t)||(u(o,t),r=1)})),!t(a)&&f(o)&&u(e,a),r};e.createAutomergePersister=(e,o,r="tinybase",i)=>(o.change((e=>v(e,r,y))),((e,o,r,i,c,y,g,l={},u=[])=>{let p,h,v,w=0,L=0,M=0;C(m,u,(()=>0)),C(S,u,(()=>[]));const[T,P,x,D,j]=((e=1,t)=>1!=e&&t.isMergeable()?[1,t.getMergeableContent,t.getTransactionMergeableChanges,([[e],[t]])=>!f(e)||!f(t),t.setDefaultContent]:2!=e?[0,t.getContent,t.getTransactionChanges,([e,t])=>!f(e)||!f(t),t.setContent]:s("Store type not supported by this Persister"))(g,e),E=t=>{(T&&n(t?.[0])?1===t?.[2]?e.applyMergeableChanges:e.setMergeableContent:1===t?.[2]?e.applyChanges:e.setContent)(t)},O=async e=>(2!=w&&(w=1,L++,await F((async()=>{try{const e=await o();n(e)?E(e):s("Content is not an array "+e)}catch(t){y?.(t),e&&j(e)}w=0}))),H),k=()=>(h&&(c(h),h=void 0),H),z=async e=>(1!=w&&(w=2,M++,await F((async()=>{try{await r(P,e)}catch(e){y?.(e)}w=0}))),H),B=()=>(a(v,e.delListener),v=void 0,H),F=async(...e)=>(((e,...t)=>{e.push(...t)})(b(S,u),...e),await(async()=>{if(!b(m,u)){for(A(m,u,1);!t((e=b(S,u),p=e.shift()));)try{await p()}catch(e){y?.(e)}A(m,u,0)}var e})(),H),H={load:O,startAutoLoad:async e=>{await k().load(e);try{h=await i((async(e,t)=>{t||e?2!=w&&(w=1,L++,E(t??e),w=0):await O()}))}catch(e){y?.(e)}return H},stopAutoLoad:k,isAutoLoading:()=>!t(h),save:z,startAutoSave:async()=>(await B().save(),v=e.addDidFinishTransactionListener((()=>{const e=x();D(e)&&z(e)})),H),stopAutoSave:B,isAutoSaving:()=>!t(v),schedule:F,getStore:()=>e,destroy:()=>(b(S,u).splice(0,void 0),k().stopAutoSave()),getStats:()=>({loads:L,saves:M}),...l};return d(H)})(e,(async()=>{const e=await o.doc();return 2==h(e?.[r])?L(e,r):void 0}),(async(e,n)=>o.change((s=>((e,n,s,o)=>{((e,t)=>{f(e[t])&&(e[t]={t:{},v:{}})})(e,n);const[r,i]=L(e,n),c=()=>{d=1};let d=1;if(a(o,(([e,n])=>{d=0,p(e,((e,n)=>d?0:t(e)?u(r,n):a(r[n],(n=>p(e,((e,s)=>d?0:t(e)?u(n,s):a(g(n,s),(a=>p(e,((e,n)=>t(e)?u(a,n):a[n]=e))),c)))),c))),p(n,((e,a)=>d?0:t(e)?u(i,a):i[a]=e))})),d){const[e,t]=s();M(r,void 0,e,((e,t,a)=>M(r,t,a,((e,t,a)=>M(e,t,a,((e,t,a)=>{if(g(e,t)!==a)return e[t]=a,1})))))),M(i,void 0,t,((e,t,a)=>{g(i,t)!==a&&(i[t]=a)}))}})(s,r,e,n)))),(e=>{const t=({doc:t})=>e(L(t,r));return o.on("change",t),t}),(e=>{o.removeListener("change",e)}),i,1,{getDocHandle:()=>o}))},"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TinyBasePersisterAutomerge={});
