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