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