import{Map as t}from"yjs";const e="t",a="v",n=t=>null==t,s=(t,e,a)=>n(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),y=g.entries,d=g.keys,u=g.freeze,p=(t=[])=>g.fromEntries(t),h=(t,e)=>e in t,v=(t,e)=>((t,e)=>t.map(e))(y(t),(([t,a])=>e(a,t))),w=t=>(t=>!n(t)&&s(l(t),(t=>t==g.prototype||n(l(t))),(()=>!0)))(t)&&0==(t=>r(d(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))),A=(t,e,a)=>{return n(a)?(s=t,o=e,s?.delete(o),t):t?.set(e,a);var s,o},M=(t,e,a,n)=>{var s,o;return s=t,o=e,s?.has(o)||A(t,e,a()),C(t,e)},O=S(),J=S(),N="delete",D=t=>[t.get(e),t.get(a)],E=(e,a,s,o)=>{const r=n(a)?e:e.get(a)??e.set(a,new t);let i;return v(s,((t,e)=>{o(r,e,t)&&(i=1)})),r.forEach(((t,e)=>{h(s,e)||(r.delete(e),i=1)})),n(a)||r.size||e.delete(a),i},L=(g,l,y="tinybase",d)=>{const h=l.getMap(y);return((t,e,a,r,g,l,y,d={},p=[])=>{let h,v,f,S=0,b=0,N=0;M(O,p,(()=>0)),M(J,p,(()=>[]));const[D,E,L,m,z]=((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"))(y,t),T=e=>{(D&&o(e?.[0])?1===e?.[2]?t.applyMergeableChanges:t.setMergeableContent:1===e?.[2]?t.applyChanges:t.setContent)(e)},j=async t=>(2!=S&&(S=1,b++,await F((async()=>{try{const t=await e();o(t)?T(t):i("Content is not an array "+t)}catch(e){l?.(e),t&&z(t)}S=0}))),Y),k=()=>(v&&(g(v),v=void 0),Y),P=async t=>(1!=S&&(S=2,N++,await F((async()=>{try{await a(E,t)}catch(t){l?.(t)}S=0}))),Y),x=()=>(s(f,t.delListener),f=void 0,Y),F=async(...t)=>(((t,...e)=>{t.push(...e)})(C(J,p),...t),await(async()=>{if(!C(O,p)){for(A(O,p,1);!n(h=c(C(J,p)));)try{await h()}catch(t){l?.(t)}A(O,p,0)}})(),Y),Y={load:j,startAutoLoad:async t=>{await k().load(t);try{v=await r((async(t,e)=>{e||t?2!=S&&(S=1,b++,T(e??t),S=0):await j()}))}catch(t){l?.(t)}return Y},stopAutoLoad:k,isAutoLoading:()=>!n(v),save:P,startAutoSave:async()=>(await x().save(),f=t.addDidFinishTransactionListener((()=>{const t=L();m(t)&&P(t)})),Y),stopAutoSave:x,isAutoSaving:()=>!n(f),schedule:F,getStore:()=>t,destroy:()=>(C(J,p).splice(0,void 0),k().stopAutoSave()),getStats:()=>({loads:b,saves:N}),...d};return u(Y)})(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),o.set(a,new t));const[c,g]=D(o),l=()=>{y=1};let y=1;if(s(i,(([t,e])=>{y=0,v(t,((t,e)=>y?0:n(t)?c.delete(e):s(c.get(e),(e=>v(t,((t,a)=>y?0:n(t)?e.delete(a):s(e.get(a),(e=>v(t,((t,a)=>n(t)?e.delete(a):e.set(a,t)))),l)))),l))),v(e,((t,e)=>y?0:n(t)?g.delete(e):g.set(e,t)))})),y){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 n=n=>t(void 0,((t,n)=>{if(1==r(n)&&(o=n[0].path,0==r(o)))return[t.get(e).toJSON(),t.get(a).toJSON(),1];var o;const[i,g]=D(t),l={},y={};return(t=>{t.forEach((({path:t,changes:{keys:a}})=>c(t)==e?s(c(t),(e=>{const n=f(l,e,p),o=i.get(e);s(c(t),(t=>{const e=f(n,t,p),s=o.get(t);b(a,((t,{action:a})=>e[t]=a==N?null:s.get(t)))}),(()=>b(a,((t,{action:e})=>n[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})=>y[t]=e==N?null:g.get(t)))))})(n),[l,y,1]})(h,n));return h.observeDeep(n),n}),(t=>{h.unobserveDeep(t)}),d,1,{getYDoc:()=>l})};export{L as createYjsPersister};
