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,o=t=>s.getPrototypeOf(t),r=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),h=(t,e)=>((t,e)=>t.map(e))(r(t),(([t,a])=>e(a,t))),u=t=>i(t).length,v=a=>(a=>!t(a)&&e(o(a),(e=>e==s.prototype||t(o(e))),(()=>!0)))(a)&&0==u(a),p=(t,e,a)=>(d(t,e)||(t[e]=a()),t[e]),w=t=>new Map(t),C=(t,e)=>t?.get(e),f=(e,a,n)=>{return t(n)?(s=e,o=a,s?.delete(o),e):e?.set(a,n);var s,o},A=(t,e,a,n)=>{var s,o;return s=t,o=e,s?.has(o)||f(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 o=t(a)?e:p(e,a,(()=>({})));let r;return h(n,((t,e)=>{s(o,e,t)&&(r=1)})),h(o,((t,e)=>{d(n,e)||(l(o,e),r=1)})),!t(a)&&v(o)&&l(e,a),r},m=(s,o,r="tinybase",i)=>(o.change((t=>p(t,r,y))),((s,o,r,i,y,g,d,l={},h=[])=>{let u,p,w,L=0,M=0,m=0;A(b,h,(()=>0)),A(S,h,(()=>[]));const[D,T,E,O,P]=((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 o();a(t)?j(t):n("Content is not an array "+t)}catch(e){g?.(e),t&&P(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 r(T,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,h),...e),await(async()=>{if(!C(b,h)){for(f(b,h,1);!t((e=C(S,h),u=e.shift()));)try{await u()}catch(t){g?.(t)}f(b,h,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=E();O(t)&&z(t)})),q),stopAutoSave:F,isAutoSaving:()=>!t(w),schedule:H,getStore:()=>s,destroy:()=>(C(S,h).splice(0,void 0),x().stopAutoSave()),getStats:()=>({loads:M,saves:m}),...l};return c(q)})(s,(async()=>{const t=await o.doc();return 2==u(t?.[r])?L(t,r):void 0}),(async(a,n)=>o.change((s=>((a,n,s,o)=>{((t,e)=>{v(t[e])&&(t[e]={t:{},v:{}})})(a,n);const[r,i]=L(a,n),c=()=>{y=1};let y=1;if(e(o,(([a,n])=>{y=0,h(a,((a,n)=>y?0:t(a)?l(r,n):e(r[n],(n=>h(a,((a,s)=>y?0:t(a)?l(n,s):e(g(n,s),(e=>h(a,((a,n)=>t(a)?l(e,n):e[n]=a))),c)))),c))),h(n,((e,a)=>y?0:t(e)?l(i,a):i[a]=e))})),y){const[t,e]=s();M(r,void 0,t,((t,e,a)=>M(r,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,r,a,n)))),(t=>{const e=({doc:e})=>t(L(e,r));return o.on("change",e),e}),(t=>{o.removeListener("change",t)}),i,1,{getDocHandle:()=>o}));export{m as createAutomergePersister};
