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