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