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