function e(e,t){let n;try{n=e()}catch{return}return{getItem:e=>{let r=e=>e===null?null:JSON.parse(e,t?.reviver),i=n.getItem(e)??null;return i instanceof Promise?i.then(r):r(i)},setItem:(e,r)=>n.setItem(e,JSON.stringify(r,t?.replacer)),removeItem:e=>n.removeItem(e)}}var t=e=>n=>{try{let r=e(n);return r instanceof Promise?r:{then(e){return t(e)(r)},catch(e){return this}}}catch(e){return{then(e){return this},catch(n){return t(n)(e)}}}},n=(n,r)=>(i,a,o)=>{let s={storage:e(()=>window.localStorage),partialize:e=>e,version:0,merge:(e,t)=>({...t,...e}),...r},c=!1,l=0,u=new Set,d=new Set,f=s.storage;if(!f)return n((...e)=>{console.warn(`[zustand persist middleware] Unable to update item '${s.name}', the given storage is currently unavailable.`),i(...e)},a,o);let p=()=>{let e=s.partialize({...a()});return f.setItem(s.name,{state:e,version:s.version})},m=o.setState;o.setState=(e,t)=>(m(e,t),p());let h=n((...e)=>(i(...e),p()),a,o);o.getInitialState=()=>h;let g,_=()=>{if(!f)return;let e=++l;c=!1,u.forEach(e=>e(a()??h));let n=s.onRehydrateStorage?.call(s,a()??h)||void 0;return t(f.getItem.bind(f))(s.name).then(e=>{if(e)if(typeof e.version==`number`&&e.version!==s.version){if(s.migrate){let t=s.migrate(e.state,e.version);return t instanceof Promise?t.then(e=>[!0,e]):[!0,t]}console.error(`State loaded from storage couldn't be migrated since no migrate function was provided`)}else return[!1,e.state];return[!1,void 0]}).then(t=>{if(e!==l)return;let[n,r]=t;if(g=s.merge(r,a()??h),i(g,!0),n)return p()}).then(()=>{e===l&&(n?.(a(),void 0),g=a(),c=!0,d.forEach(e=>e(g)))}).catch(t=>{e===l&&n?.(void 0,t)})};return o.persist={setOptions:e=>{s={...s,...e},e.storage&&(f=e.storage)},clearStorage:()=>{f?.removeItem(s.name)},getOptions:()=>s,rehydrate:()=>_(),hasHydrated:()=>c,onHydrate:e=>(u.add(e),()=>{u.delete(e)}),onFinishHydration:e=>(d.add(e),()=>{d.delete(e)})},s.skipHydration||_(),g||h};export{n as t};