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