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