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