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