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=async(t,e,a)=>{try{return await t()}catch(t){e?.(t)}},l=(t,e)=>t.forEach(e),g=(t,...e)=>t.push(...e),u=t=>t.shift(),d=t=>n(t)||0==(t=>t?.size??0)(t),y=(t,e)=>t?.forEach(e),p=(t,e)=>t?.delete(e),w=Object,v=t=>w.getPrototypeOf(t),h=w.entries,f=w.keys,S=w.freeze,C=(t=[])=>w.fromEntries(t),b=(t,e)=>e in t,A=(t,e)=>C(((t,e)=>((t,e)=>t.map(e))(h(t),([t,a])=>e(a,t)))(t,(t,a)=>[a,e(t,a)])),M=t=>(t=>!n(t)&&s(v(t),t=>t==w.prototype||n(v(t)),()=>!0))(t)&&0==(t=>r(f(t)))(t),O=(t,e,a)=>(b(t,e)||(t[e]=a()),t[e]),L=t=>new Map(t),J=(t,e)=>t?.get(e),N=(t,e)=>y(t,(t,a)=>e(a,t)),z=(t,e,a)=>n(a)?(p(t,e),t):t?.set(e,a),D=(t,e,a,n)=>{var s,o;return s=t,o=e,s?.has(o)?n?.(J(t,e)):z(t,e,a()),J(t,e)},E=(t,e,a,n,o=0)=>s((a?D:J)(t,e[o],o>r(e)-2?a:L),s=>{if(o>r(e)-2)return n?.(s)&&z(t,e[o]),s;const i=E(s,e,a,n,o+1);return d(s)&&z(t,e[o]),i}),m=/^\d+$/,P=t=>new Set(o(t)||n(t)?t:[t]),T=L(),j=L(),k=(t,e,a,w,v,h,f,C={},b=0,A=[])=>{let O,N,k,x=0,F=0,Y=0;D(T,A,()=>0),D(j,A,()=>[]);const $=L(),[q,B,G,H,I]=((t=1,e,a)=>1!=t&&e.isMergeable()?[1,e.getMergeableContent,()=>e.getTransactionMergeableChanges(!a),([[t],[e]])=>!M(t)||!M(e),e.setDefaultContent]:2!=t?[0,e.getContent,e.getTransactionChanges,([t,e])=>!M(t)||!M(e),e.setContent]:i("Store type not supported by this Persister"))(f,t,b),[K,Q,R]=(()=>{let t;const[e,a]=(()=>{const t=[];let e=0;return[a=>(a?u(t):null)??""+e++,e=>{m.test(e)&&r(t)<1e3&&g(t,e)}]})(),o=L();return[(a,n,s,r=[],i=()=>[])=>{t??=st;const c=e(1);var l,g;return z(o,c,[a,n,s,r,i]),l=E(n,s??[""],P),g=c,l?.add(g),c},(e,a,...n)=>l(((t,e=[""])=>{const a=[],n=(t,s)=>s==r(e)?g(a,t):null===e[s]?y(t,t=>n(t,s+1)):l([e[s],null],e=>n(J(t,e),s+1));return n(t,0),a})(e,a),e=>y(e,e=>J(o,e)[0](t,...a??[],...n))),t=>s(J(o,t),([,e,n])=>(E(e,n??[""],void 0,e=>(p(e,t),d(e)?1:0)),z(o,t),a(t),n)),e=>s(J(o,e),([e,,a=[],s,o])=>{const i=(...c)=>{const g=r(c);g==r(a)?e(t,...c,...o(c)):n(a[g])?l(s[g]?.(...c)??[],t=>i(...c,t)):i(...c,a[g])};i()})]})(),U=t=>{t!=x&&(x=t,Q($,void 0,x))},V=e=>{(q&&o(e?.[0])?1===e?.[2]?t.applyMergeableChanges:t.setMergeableContent:1===e?.[2]?t.applyChanges:t.setContent)(e)},W=async t=>(2!=x&&(U(1),F++,await nt(async()=>{await c(async()=>{const a=await e();o(a)?V(a):t?I(t):i("Content is not an array: "+a)},()=>{t&&I(t)}),U(0)})),st),X=async t=>(Z(),await W(t),await c(async()=>N=await w(async(t,e)=>{e||t?2!=x&&(U(1),F++,V(e??t),U(0)):await W()}),h),st),Z=async()=>(N&&(await c(()=>v(N),h),N=void 0),st),_=async t=>(1!=x&&(U(2),Y++,await nt(async()=>{await c(()=>a(B,t),h),U(0)})),st),tt=async()=>(et(),await _(),k=t.addDidFinishTransactionListener(()=>{const t=G();H(t)&&_(t)}),st),et=async()=>(k&&(t.delListener(k),k=void 0),st),at=async(t=!1)=>{const[e,a]=t?[et,Z]:[Z,et];return await e(),await a(),st},nt=async(...t)=>(g(J(j,A),...t),await(async()=>{if(!J(T,A)){for(z(T,A,1);!n(O=u(J(j,A)));)await c(O,h);z(T,A,0)}})(),st),st={load:W,startAutoLoad:X,stopAutoLoad:Z,isAutoLoading:()=>!n(N),save:_,startAutoSave:tt,stopAutoSave:et,isAutoSaving:()=>!n(k),startAutoPersisting:async(t,e=!1)=>{const[a,n]=e?[tt,X]:[X,tt];return await a(t),await n(t),st},stopAutoPersisting:at,getStatus:()=>x,addStatusListener:t=>K(t,$),delListener:e=>(R(e),t),schedule:nt,getStore:()=>t,destroy:()=>(J(j,A).splice(0,void 0),at()),getStats:()=>({loads:F,saves:Y}),...C};return S(st)},x="delete",F=t=>[t.get(e),t.get(a)],Y=(e,a,s,o)=>{const r=n(a)?e:e.get(a)??e.set(a,new t);let i;return A(s,(t,e)=>{o(r,e,t)&&(i=1)}),r.forEach((t,e)=>{b(s,e)||(r.delete(e),i=1)}),n(a)||r.size||e.delete(a),i},$=(o,i,c="tinybase",g)=>{const d=i.getMap(c);return k(o,async()=>d.size?[d.get(e).toJSON(),d.get(a).toJSON()]:void 0,async(o,r)=>i.transact(()=>((o,r,i)=>{o.size||(o.set(e,new t),o.set(a,new t));const[c,l]=F(o),g=()=>{u=1};let u=1;if(s(i,([t,e])=>{u=0,A(t,(t,e)=>u?0:n(t)?c.delete(e):s(c.get(e),e=>A(t,(t,a)=>u?0:n(t)?e.delete(a):s(e.get(a),e=>A(t,(t,a)=>n(t)?e.delete(a):e.set(a,t)),g)),g)),A(e,(t,e)=>u?0:n(t)?l.delete(e):l.set(e,t))}),u){const[t,e]=r();Y(c,void 0,t,(t,e,a)=>Y(c,e,a,(t,e,a)=>Y(t,e,a,(t,e,a)=>{if(t.get(e)!==a)return t.set(e,a),1}))),Y(l,void 0,e,(t,e,a)=>{l.get(e)!==a&&l.set(e,a)})}})(d,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,c]=F(t),g={},d={};return l(n,({path:t,changes:{keys:a}})=>u(t)==e?s(u(t),e=>{const n=O(g,e,C),o=i.get(e);s(u(t),t=>{const e=O(n,t,C),s=o.get(t);N(a,(t,{action:a})=>e[t]=a==x?null:s.get(t))},()=>N(a,(t,{action:e})=>n[t]=e==x?null:o.get(t)?.toJSON()))},()=>N(a,(t,{action:e})=>g[t]=e==x?null:i.get(t)?.toJSON())):N(a,(t,{action:e})=>d[t]=e==x?null:c.get(t))),[g,d,1]})(d,n));return d.observeDeep(n),n},t=>{d.unobserveDeep(t)},g,1,{getYDoc:()=>i})};export{$ as createYjsPersister};
