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