!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("jotai/vanilla")):"function"==typeof define&&define.amd?define(["exports","react","jotai/vanilla"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).jotaiReact={},e.React,e.jotaiVanilla)}(this,(function(e,t,n){"use strict";var r=t.createContext(void 0),u=function(e){var u=t.useContext(r);return(null==e?void 0:e.store)||u||n.getDefaultStore()},o=function(e){return"function"==typeof(null==e?void 0:e.then)},i=function(e){e.status="pending",e.then((function(t){e.status="fulfilled",e.value=t}),(function(t){e.status="rejected",e.reason=t}))},a=t.use||function(e){if("pending"===e.status)throw e;if("fulfilled"===e.status)return e.value;throw"rejected"===e.status?e.reason:(i(e),e)},f=new WeakMap,c=function(e){var t=f.get(e);return t||(t=new Promise((function(n,r){var u=e,i=function(e){return function(t){u===e&&n(t)}},a=function(e){return function(t){u===e&&r(t)}},c=function(e){"onCancel"in e&&"function"==typeof e.onCancel&&e.onCancel((function(e){o(e)?(f.set(e,t),u=e,e.then(i(e),a(e)),c(e)):n(e)}))};e.then(i(e),a(e)),c(e)})),f.set(e,t)),t};function s(e,n){var r=u(n),f=t.useReducer((function(t){var n=r.get(e);return Object.is(t[0],n)&&t[1]===r&&t[2]===e?t:[n,r,e]}),void 0,(function(){return[r.get(e),r,e]})),s=f[0],l=s[0],d=s[1],v=s[2],p=f[1],g=l;d===r&&v===e||(p(),g=r.get(e));var h=null==n?void 0:n.delay;if(t.useEffect((function(){var t=r.sub(e,(function(){if("number"==typeof h){var t=r.get(e);return o(t)&&i(c(t)),void setTimeout(p,h)}p()}));return p(),t}),[r,e,h]),t.useDebugValue(g),o(g)){var y=c(g);return a(y)}return g}function l(e,n){var r=u(n),o=t.useCallback((function(){for(var t=arguments.length,n=new Array(t),u=0;u<t;u++)n[u]=arguments[u];return r.set.apply(r,[e].concat(n))}),[r,e]);return o}e.Provider=function(e){var u=e.children,o=e.store,i=t.useRef(void 0);return o||i.current||(i.current=n.createStore()),t.createElement(r.Provider,{value:o||i.current},u)},e.useAtom=function(e,t){return[s(e,t),l(e,t)]},e.useAtomValue=s,e.useSetAtom=l,e.useStore=u}));
