"use strict";var e=require("yjs");const t="t",n="v",o=e=>null==e,r=(e,t,n)=>o(e)?null==n?void 0:n():t(e),l=e=>Array.isArray(e),a=e=>e.length,i=e=>{throw Error(e)},s=e=>e.shift(),u=Object,c=e=>u.getPrototypeOf(e),d=u.entries,v=u.keys,g=u.freeze,y=(e=[])=>u.fromEntries(e),p=(e,t)=>t in e,h=(e,t)=>((e,t)=>e.map(t))(d(e),(([e,n])=>t(n,e))),f=e=>(e=>!o(e)&&r(c(e),(e=>e==u.prototype||o(c(e))),(()=>!0)))(e)&&0==(e=>a(v(e)))(e),b=(e,t,n)=>(p(e,t)||(e[t]=n()),e[t]),O=e=>new Map(e),S=(e,t)=>null==e?void 0:e.get(t),w=(e,t)=>((e,t)=>null==e?void 0:e.forEach(t))(e,((e,n)=>t(n,e))),C=(e,t,n)=>{return o(n)?(l=t,null==(r=e)||r.delete(l),e):null==e?void 0:e.set(t,n);var r,l},M=(e,t,n,o)=>{var r,l,a;return l=t,null!=(a=null==(r=e)?void 0:r.has(l))&&a||C(e,t,n()),S(e,t)};var P=Object.defineProperty,m=Object.getOwnPropertySymbols,A=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable,E=(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{i(n.next(e))}catch(e){r(e)}},a=e=>{try{i(n.throw(e))}catch(e){r(e)}},i=e=>e.done?o(e.value):Promise.resolve(e.value).then(l,a);i((n=n.apply(e,t)).next())}));const N=O(),x=O();var D=(e,t,n)=>new Promise(((o,r)=>{var l=e=>{try{i(n.next(e))}catch(e){r(e)}},a=e=>{try{i(n.throw(e))}catch(e){r(e)}},i=e=>e.done?o(e.value):Promise.resolve(e.value).then(l,a);i((n=n.apply(e,t)).next())}));const L="delete",z=e=>[e.get(t),e.get(n)],T=(t,n,r,l)=>{var a;const i=o(n)?t:null!=(a=t.get(n))?a:t.set(n,new e.Map);let s;return h(r,((e,t)=>{l(i,t,e)&&(s=1)})),i.forEach(((e,t)=>{p(r,t)||(i.delete(t),s=1)})),o(n)||i.size||t.delete(n),s};exports.createYjsPersister=(u,c,d="tinybase",v)=>{const p=c.getMap(d);return((e,t,n,a,u,c,d,v={},y=[])=>{let p,h,b,O=0,w=0,P=0;M(N,y,(()=>0)),M(x,y,(()=>[]));const[D,L,z,T,k]=((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]:i("Store type not supported by this Persister"))(d,e),Y=t=>{(D&&l(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)},q=e=>J(void 0,null,(function*(){return 2!=O&&(O=1,w++,yield G((()=>J(void 0,null,(function*(){try{const e=yield t();l(e)?Y(e):i("Content is not an array "+e)}catch(t){null==c||c(t),e&&k(e)}O=0}))))),H})),F=()=>(h&&(u(h),h=void 0),H),I=e=>J(void 0,null,(function*(){return 1!=O&&(O=2,P++,yield G((()=>J(void 0,null,(function*(){try{yield n(L,e)}catch(e){null==c||c(e)}O=0}))))),H})),B=()=>(r(b,e.delListener),b=void 0,H),G=(...e)=>J(void 0,null,(function*(){return((e,...t)=>{e.push(...t)})(S(x,y),...e),yield J(void 0,null,(function*(){if(!S(N,y)){for(C(N,y,1);!o(p=s(S(x,y)));)try{yield p()}catch(e){null==c||c(e)}C(N,y,0)}})),H})),H=((e,t)=>{for(var n in t||(t={}))A.call(t,n)&&E(e,n,t[n]);if(m)for(var n of m(t))j.call(t,n)&&E(e,n,t[n]);return e})({load:q,startAutoLoad:e=>J(void 0,null,(function*(){yield F().load(e);try{h=yield a(((e,t)=>J(void 0,null,(function*(){t||e?2!=O&&(O=1,w++,Y(null!=t?t:e),O=0):yield q()}))))}catch(e){null==c||c(e)}return H})),stopAutoLoad:F,isAutoLoading:()=>!o(h),save:I,startAutoSave:()=>J(void 0,null,(function*(){return yield B().save(),b=e.addDidFinishTransactionListener((()=>{const e=z();T(e)&&I(e)})),H})),stopAutoSave:B,isAutoSaving:()=>!o(b),schedule:G,getStore:()=>e,destroy:()=>(S(x,y).splice(0,void 0),F().stopAutoSave()),getStats:()=>({loads:w,saves:P})},v);return g(H)})(u,(()=>D(void 0,null,(function*(){return p.size?[p.get(t).toJSON(),p.get(n).toJSON()]:void 0}))),((l,a)=>D(void 0,null,(function*(){return c.transact((()=>((l,a,i)=>{l.size||(l.set(t,new e.Map),l.set(n,new e.Map));const[s,u]=z(l),c=()=>{d=1};let d=1;if(r(i,(([e,t])=>{d=0,h(e,((e,t)=>d?0:o(e)?s.delete(t):r(s.get(t),(t=>h(e,((e,n)=>d?0:o(e)?t.delete(n):r(t.get(n),(t=>h(e,((e,n)=>o(e)?t.delete(n):t.set(n,e)))),c)))),c))),h(t,((e,t)=>d?0:o(e)?u.delete(t):u.set(t,e)))})),d){const[e,t]=a();T(s,void 0,e,((e,t,n)=>T(s,t,n,((e,t,n)=>T(e,t,n,((e,t,n)=>{if(e.get(t)!==n)return e.set(t,n),1})))))),T(u,void 0,t,((e,t,n)=>{u.get(t)!==n&&u.set(t,n)}))}})(p,l,a)))}))),(e=>{const o=o=>e(void 0,((e,o)=>{if(1==a(o)&&(l=o[0].path,0==a(l)))return[e.get(t).toJSON(),e.get(n).toJSON(),1];var l;const[i,u]=z(e),c={},d={};return(e=>{e.forEach((({path:e,changes:{keys:n}})=>s(e)==t?r(s(e),(t=>{const o=b(c,t,y),l=i.get(t);r(s(e),(e=>{const t=b(o,e,y),r=l.get(e);w(n,((e,{action:n})=>t[e]=n==L?null:r.get(e)))}),(()=>w(n,((e,{action:t})=>{var n;return o[e]=t==L?null:null==(n=l.get(e))?void 0:n.toJSON()}))))}),(()=>w(n,((e,{action:t})=>{var n;return c[e]=t==L?null:null==(n=i.get(e))?void 0:n.toJSON()})))):w(n,((e,{action:t})=>d[e]=t==L?null:u.get(e)))))})(o),[c,d,1]})(p,o));return p.observeDeep(o),o}),(e=>{p.unobserveDeep(e)}),v,1,{getYDoc:()=>c})};
