const t=t=>{let e;const n=new Set,r=(t,r)=>{const o="function"==typeof t?t(e):t;if(!Object.is(o,e)){const t=e;e=(null!=r?r:"object"!=typeof o||null===o)?o:Object.assign({},e,o),n.forEach(n=>n(e,t))}},o=()=>e,a={setState:r,getState:o,getInitialState:()=>s,subscribe:t=>(n.add(t),()=>n.delete(t))},s=e=t(r,o,a);return a},e=e=>e?t(e):t;function n(t,e){let n;try{n=t()}catch(r){return}return{getItem:t=>{var e;const r=t=>null===t?null:JSON.parse(t,void 0),o=null!=(e=n.getItem(t))?e:null;return o instanceof Promise?o.then(r):r(o)},setItem:(t,e)=>n.setItem(t,JSON.stringify(e,void 0)),removeItem:t=>n.removeItem(t)}}const r=t=>e=>{try{const n=t(e);return n instanceof Promise?n:{then:t=>r(t)(n),catch(t){return this}}}catch(n){return{then(t){return this},catch:t=>r(t)(n)}}},o=(t,e)=>(o,a,s)=>{let i={storage:n(()=>localStorage),partialize:t=>t,version:0,merge:(t,e)=>({...e,...t}),...e},l=!1;const c=new Set,u=new Set;let d=i.storage;if(!d)return t((...t)=>{o(...t)},a,s);const m=()=>{const t=i.partialize({...a()});return d.setItem(i.name,{state:t,version:i.version})},h=s.setState;s.setState=(t,e)=>(h(t,e),m());const v=t((...t)=>(o(...t),m()),a,s);let g;s.getInitialState=()=>v;const f=()=>{var t,e;if(!d)return;l=!1,c.forEach(t=>{var e;return t(null!=(e=a())?e:v)});const n=(null==(e=i.onRehydrateStorage)?void 0:e.call(i,null!=(t=a())?t:v))||void 0;return r(d.getItem.bind(d))(i.name).then(t=>{if(t){if("number"!=typeof t.version||t.version===i.version)return[!1,t.state];if(i.migrate){const e=i.migrate(t.state,t.version);return e instanceof Promise?e.then(t=>[!0,t]):[!0,e]}}return[!1,void 0]}).then(t=>{var e;const[n,r]=t;if(g=i.merge(r,null!=(e=a())?e:v),o(g,!0),n)return m()}).then(()=>{null==n||n(g,void 0),g=a(),l=!0,u.forEach(t=>t(g))}).catch(t=>{null==n||n(void 0,t)})};return s.persist={setOptions:t=>{i={...i,...t},t.storage&&(d=t.storage)},clearStorage:()=>{null==d||d.removeItem(i.name)},getOptions:()=>i,rehydrate:()=>f(),hasHydrated:()=>l,onHydrate:t=>(c.add(t),()=>{c.delete(t)}),onFinishHydration:t=>(u.add(t),()=>{u.delete(t)})},i.skipHydration||f(),g||v};export{n as a,e as c,o as p};
