!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateInterpreter={})}(this,(function(t){"use strict"; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */var e,n,r=function(){return(r=Object.assign||function(t){for(var e,n=1,r=arguments.length;n=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function o(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,o=n.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(r=o.next()).done;)s.push(r.value)}catch(t){i={error:t}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return s}function s(t,e,n){if(n||2===arguments.length)for(var r,i=0,o=e.length;i0;){var e=o.shift();n=t.transition(n,e,c),i.forEach((function(t){return t.next(n)}))}s=!1}}()},getSnapshot:function(){return n},subscribe:function(t,e,r){var o=R(t,e,r);return i.add(o),o.next(n),{unsubscribe:function(){i.delete(o)}}}}),c={parent:e.parent,self:a,id:e.id||"anonymous",observers:i};return n=t.start?t.start(c):n,a}var st,at={sync:!1,autoForward:!1};t.InterpreterStatus=void 0,(st=t.InterpreterStatus||(t.InterpreterStatus={}))[st.NotStarted=0]="NotStarted",st[st.Running=1]="Running",st[st.Stopped=2]="Stopped";var ct=function(){function a(i,o){void 0===o&&(o=a.defaultOptions);var s=this;this.machine=i,this.delayedEventsMap={},this.listeners=new Set,this.contextListeners=new Set,this.stopListeners=new Set,this.doneListeners=new Set,this.eventListeners=new Set,this.sendListeners=new Set,this.initialized=!1,this.status=t.InterpreterStatus.NotStarted,this.children=new Map,this.forwardTo=new Set,this._outgoingQueue=[],this.init=this.start,this.send=function(e,n){if(L(e))return s.batch(e),s.state;var r=M(D(e,n));if(s.status===t.InterpreterStatus.Stopped)return s.state;if(s.status!==t.InterpreterStatus.Running&&!s.options.deferEvents)throw new Error('Event "'.concat(r.name,'" was sent to uninitialized service "').concat(s.machine.id,'". Make sure .start() is called for this service, or set { deferEvents: true } in the service options.\nEvent: ').concat(JSON.stringify(r.data)));return s.scheduler.schedule((function(){s.forward(r);var t=s._nextState(r);s.update(t,r)})),s._state},this.sendTo=function(e,i,o){var a,c=s.parent&&(i===n.Parent||s.parent.id===i),u=c?s.parent:j(i)?i===n.Internal?s:s.children.get(i)||et(i):(a=i)&&"function"==typeof a.send?i:void 0;if(u)if("machine"in u){if(s.status!==t.InterpreterStatus.Stopped||s.parent!==u||s.state.done){var f=r(r({},e),{name:e.name===m?"".concat($(s.id)):e.name,origin:s.sessionId});!o&&s.machine.config.predictableActionArguments?s._outgoingQueue.push([u,f]):u.send(f)}}else!o&&s.machine.config.predictableActionArguments?s._outgoingQueue.push([u,e.data]):u.send(e.data);else if(!c)throw new Error("Unable to send event to child '".concat(i,"' from service '").concat(s.id,"'."))},this._exec=function(n,r,i,o){void 0===o&&(o=s.machine.options.actions);var a,l=n.exec||V(n.type,o),v=k(l)?l:l?l.exec:n.exec;if(v)try{return v(r,i.data,s.machine.config.predictableActionArguments?{action:n,_event:i}:{action:n,state:s.state,_event:i})}catch(t){throw s.parent&&s.parent.send({type:"xstate.error",data:t}),t}switch(n.type){case f:var y=n;s.defer(y);break;case d:var g=n;if("number"==typeof g.delay)return void s.defer(g);g.to?s.sendTo(g._event,g.to,i===z):s.send(g._event);break;case h:s.cancel(n.sendId);break;case c:if(s.status!==t.InterpreterStatus.Running)return;var m=n.activity;if(!s.machine.config.predictableActionArguments&&!s.state.activities[m.id||m.type])break;if(m.type===e.Invoke){var x="string"==typeof(a=m.src)?{type:a}:a,b=s.machine.options.services?s.machine.options.services[x.type]:void 0,S=m.id,w=m.data,_="autoForward"in m?m.autoForward:!!m.forward;if(!b)return;var E=w?I(w,r,i):void 0;if("string"==typeof b)return;var O=k(b)?b(r,i.data,{data:E,src:x,meta:m.meta}):b;if(!O)return;var T=void 0;P(O)&&(O=E?O.withContext(E):O,T={autoForward:_}),s.spawn(O,S,T)}else s.spawnActivity(m);break;case u:s.stopChild(n.activity.id);break;case p:var A=n,L=A.label,j=A.value;L?s.logger(L,j):s.logger(j)}};var l=r(r({},a.defaultOptions),o),v=l.clock,y=l.logger,g=l.parent,x=l.id,b=void 0!==x?x:i.id;this.id=b,this.logger=y,this.clock=v,this.parent=g,this.options=l,this.scheduler=new B({deferEvents:this.options.deferEvents}),this.sessionId=Z()}return Object.defineProperty(a.prototype,"initialState",{get:function(){var t=this;return this._initialState?this._initialState:H(this,(function(){return t._initialState=t.machine.initialState,t._initialState}))},enumerable:!1,configurable:!0}),Object.defineProperty(a.prototype,"state",{get:function(){return this._state},enumerable:!1,configurable:!0}),a.prototype.execute=function(t,e){var n,r;try{for(var o=i(t.actions),s=o.next();!s.done;s=o.next()){var a=s.value;this.exec(a,t,e)}}catch(t){n={error:t}}finally{try{s&&!s.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}},a.prototype.update=function(t,e){var n,r,o,s,a,c,u,f,d=this;if(t._sessionid=this.sessionId,this._state=t,this.machine.config.predictableActionArguments&&e!==z||!this.options.execute)for(var h=void 0;h=this._outgoingQueue.shift();)h[0].send(h[1]);else this.execute(this.state);if(this.children.forEach((function(t){d.state.children[t.id]=t})),this.devTools&&this.devTools.send(e.data,t),t.event)try{for(var l=i(this.eventListeners),p=l.next();!p.done;p=l.next()){(0,p.value)(t.event)}}catch(t){n={error:t}}finally{try{p&&!p.done&&(r=l.return)&&r.call(l)}finally{if(n)throw n.error}}try{for(var v=i(this.listeners),y=v.next();!y.done;y=v.next()){(0,y.value)(t,t.event)}}catch(t){o={error:t}}finally{try{y&&!y.done&&(s=v.return)&&s.call(v)}finally{if(o)throw o.error}}try{for(var g=i(this.contextListeners),m=g.next();!m.done;m=g.next()){(0,m.value)(this.state.context,this.state.history?this.state.history.context:void 0)}}catch(t){a={error:t}}finally{try{m&&!m.done&&(c=g.return)&&c.call(g)}finally{if(a)throw a.error}}if(this.state.done){var x=t.configuration.find((function(t){return"final"===t.type&&t.parent===d.machine})),b=x&&x.doneData?I(x.doneData,t.context,e):void 0;this._doneEvent=U(this.id,b);try{for(var S=i(this.doneListeners),w=S.next();!w.done;w=S.next()){(0,w.value)(this._doneEvent)}}catch(t){u={error:t}}finally{try{w&&!w.done&&(f=S.return)&&f.call(S)}finally{if(u)throw u.error}}this._stop(),this._stopChildren(),nt(this.sessionId)}},a.prototype.onTransition=function(e){return this.listeners.add(e),this.status===t.InterpreterStatus.Running&&e(this.state,this.state.event),this},a.prototype.subscribe=function(e,n,r){var i=this,o=R(e,n,r);this.listeners.add(o.next),this.status!==t.InterpreterStatus.NotStarted&&o.next(this.state);var s=function(){i.doneListeners.delete(s),i.stopListeners.delete(s),o.complete()};return this.status===t.InterpreterStatus.Stopped?o.complete():(this.onDone(s),this.onStop(s)),{unsubscribe:function(){i.listeners.delete(o.next),i.doneListeners.delete(s),i.stopListeners.delete(s)}}},a.prototype.onEvent=function(t){return this.eventListeners.add(t),this},a.prototype.onSend=function(t){return this.sendListeners.add(t),this},a.prototype.onChange=function(t){return this.contextListeners.add(t),this},a.prototype.onStop=function(t){return this.stopListeners.add(t),this},a.prototype.onDone=function(e){return this.status===t.InterpreterStatus.Stopped&&this._doneEvent?e(this._doneEvent):this.doneListeners.add(e),this},a.prototype.off=function(t){return this.listeners.delete(t),this.eventListeners.delete(t),this.sendListeners.delete(t),this.stopListeners.delete(t),this.doneListeners.delete(t),this.contextListeners.delete(t),this},a.prototype.start=function(e){var n=this;if(this.status===t.InterpreterStatus.Running)return this;this.machine._init(),tt(this.sessionId,this),this.initialized=!0,this.status=t.InterpreterStatus.Running;var r=void 0===e?this.initialState:H(this,(function(){return"object"==typeof(t=e)&&null!==t&&"value"in t&&"_event"in t?n.machine.resolveState(e):n.machine.resolveState(X.from(e,n.machine.context));var t}));return this.options.devTools&&this.attachDev(),this.scheduler.initialize((function(){n.update(r,z)})),this},a.prototype._stopChildren=function(){this.children.forEach((function(t){k(t.stop)&&t.stop()})),this.children.clear()},a.prototype._stop=function(){var e,n,r,o,s,a,c,u,f,d;try{for(var h=i(this.listeners),l=h.next();!l.done;l=h.next()){var p=l.value;this.listeners.delete(p)}}catch(t){e={error:t}}finally{try{l&&!l.done&&(n=h.return)&&n.call(h)}finally{if(e)throw e.error}}try{for(var v=i(this.stopListeners),y=v.next();!y.done;y=v.next()){(p=y.value)(),this.stopListeners.delete(p)}}catch(t){r={error:t}}finally{try{y&&!y.done&&(o=v.return)&&o.call(v)}finally{if(r)throw r.error}}try{for(var g=i(this.contextListeners),m=g.next();!m.done;m=g.next()){p=m.value;this.contextListeners.delete(p)}}catch(t){s={error:t}}finally{try{m&&!m.done&&(a=g.return)&&a.call(g)}finally{if(s)throw s.error}}try{for(var x=i(this.doneListeners),b=x.next();!b.done;b=x.next()){p=b.value;this.doneListeners.delete(p)}}catch(t){c={error:t}}finally{try{b&&!b.done&&(u=x.return)&&u.call(x)}finally{if(c)throw c.error}}if(!this.initialized)return this;this.initialized=!1,this.status=t.InterpreterStatus.Stopped,this._initialState=void 0;try{for(var S=i(Object.keys(this.delayedEventsMap)),w=S.next();!w.done;w=S.next()){var _=w.value;this.clock.clearTimeout(this.delayedEventsMap[_])}}catch(t){f={error:t}}finally{try{w&&!w.done&&(d=S.return)&&d.call(S)}finally{if(f)throw f.error}}this.scheduler.clear(),this.scheduler=new B({deferEvents:this.options.deferEvents})},a.prototype.stop=function(){var t=this,e=this.scheduler;return this._stop(),e.schedule((function(){var e;if(!(null===(e=t._state)||void 0===e?void 0:e.done)){var r=M({type:"xstate.stop"}),i=H(t,(function(){var e=E(s([],o(t.state.configuration),!1).sort((function(t,e){return e.order-t.order})).map((function(e){return q(e.onExit,t.machine.options.actions)}))),i=o(F(t.machine,t.state,t.state.context,r,[{type:"exit",actions:e}],t.machine.config.predictableActionArguments?t._exec:void 0,t.machine.config.predictableActionArguments||t.machine.config.preserveActionOrder),2),a=i[0],c=i[1],u=new X({value:t.state.value,context:c,_event:r,_sessionid:t.sessionId,historyValue:void 0,history:t.state,actions:a.filter((function(t){return!function(t){return(t.type===f||t.type===d&&t.to===n.Internal)&&"number"!=typeof t.delay}(t)})),activities:{},events:[],configuration:[],transitions:[],children:{},done:t.state.done,tags:t.state.tags,machine:t.machine});return u.changed=!0,u}));t.update(i,r),t._stopChildren(),nt(t.sessionId)}})),this},a.prototype.batch=function(e){var n=this;if(this.status===t.InterpreterStatus.NotStarted&&this.options.deferEvents);else if(this.status!==t.InterpreterStatus.Running)throw new Error("".concat(e.length,' event(s) were sent to uninitialized service "').concat(this.machine.id,'". Make sure .start() is called for this service, or set { deferEvents: true } in the service options.'));if(e.length){var a=!!this.machine.config.predictableActionArguments&&this._exec;this.scheduler.schedule((function(){var t,c,u=n.state,f=!1,d=[],h=function(t){var e=M(t);n.forward(e),u=H(n,(function(){return n.machine.transition(u,e,void 0,a||void 0)})),d.push.apply(d,s([],o(n.machine.config.predictableActionArguments?u.actions:u.actions.map((function(t){return function(t,e){var n=t.exec;return r(r({},t),{exec:void 0!==n?function(){return n(e.context,e.event,{action:t,state:e,_event:e._event})}:void 0})}(t,u)}))),!1)),f=f||!!u.changed};try{for(var l=i(e),p=l.next();!p.done;p=l.next()){h(p.value)}}catch(e){t={error:e}}finally{try{p&&!p.done&&(c=l.return)&&c.call(l)}finally{if(t)throw t.error}}u.changed=f,u.actions=d,n.update(u,M(e[e.length-1]))}))}},a.prototype.sender=function(t){return this.send.bind(this,t)},a.prototype._nextState=function(t,e){var n=this;void 0===e&&(e=!!this.machine.config.predictableActionArguments&&this._exec);var r=M(t);if(0===r.name.indexOf(g)&&!this.state.nextEvents.some((function(t){return 0===t.indexOf(g)})))throw r.data.data;return H(this,(function(){return n.machine.transition(n.state,r,void 0,e||void 0)}))},a.prototype.nextState=function(t){return this._nextState(t,!1)},a.prototype.forward=function(t){var e,n;try{for(var r=i(this.forwardTo),o=r.next();!o.done;o=r.next()){var s=o.value,a=this.children.get(s);if(!a)throw new Error("Unable to forward event '".concat(t,"' from interpreter '").concat(this.id,"' to nonexistant child '").concat(s,"'."));a.send(t)}}catch(t){e={error:t}}finally{try{o&&!o.done&&(n=r.return)&&n.call(r)}finally{if(e)throw e.error}}},a.prototype.defer=function(t){var e=this,n=this.clock.setTimeout((function(){"to"in t&&t.to?e.sendTo(t._event,t.to,!0):e.send(t._event)}),t.delay);t.id&&(this.delayedEventsMap[t.id]=n)},a.prototype.cancel=function(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]},a.prototype.exec=function(t,e,n){void 0===n&&(n=this.machine.options.actions),this._exec(t,e.context,e._event,n)},a.prototype.removeChild=function(t){var e;this.children.delete(t),this.forwardTo.delete(t),null===(e=this.state)||void 0===e||delete e.children[t]},a.prototype.stopChild=function(t){var e=this.children.get(t);e&&(this.removeChild(t),k(e.stop)&&e.stop())},a.prototype.spawn=function(e,n,i){if(this.status!==t.InterpreterStatus.Running)return K(e,n);if(T(e))return this.spawnPromise(Promise.resolve(e),n);if(k(e))return this.spawnCallback(e,n);if(function(t){try{return"function"==typeof t.send}catch(t){return!1}}(s=e)&&"id"in s)return this.spawnActor(e,n);if(function(t){try{return"subscribe"in t&&k(t.subscribe)}catch(t){return!1}}(e))return this.spawnObservable(e,n);if(P(e))return this.spawnMachine(e,r(r({},i),{id:n}));if(null!==(o=e)&&"object"==typeof o&&"transition"in o&&"function"==typeof o.transition)return this.spawnBehavior(e,n);throw new Error('Unable to spawn entity "'.concat(n,'" of type "').concat(typeof e,'".'));var o,s},a.prototype.spawnMachine=function(t,e){var n=this;void 0===e&&(e={});var i=new a(t,r(r({},this.options),{parent:this,id:e.id||t.id})),o=r(r({},at),e);o.sync&&i.onTransition((function(t){n.send(x,{state:t,id:i.id})}));var s=i;return this.children.set(i.id,s),o.autoForward&&this.forwardTo.add(i.id),i.onDone((function(t){n.removeChild(i.id),n.send(M(t,{origin:i.id}))})).start(),s},a.prototype.spawnBehavior=function(t,e){var n=ot(t,{id:e,parent:this});return this.children.set(e,n),n},a.prototype.spawnPromise=function(t,e){var n,r,i=this,o=!1;t.then((function(t){o||(r=t,i.removeChild(e),i.send(M(U(e,t),{origin:e})))}),(function(t){if(!o){i.removeChild(e);var n=$(e,t);try{i.send(M(n,{origin:e}))}catch(t){i.devTools&&i.devTools.send(n,i.state),i.machine.strict&&i.stop()}}}));var s=((n={id:e,send:function(){},subscribe:function(e,n,r){var i=R(e,n,r),o=!1;return t.then((function(t){o||(i.next(t),o||i.complete())}),(function(t){o||i.error(t)})),{unsubscribe:function(){return o=!0}}},stop:function(){o=!0},toJSON:function(){return{id:e}},getSnapshot:function(){return r}})[C]=function(){return this},n);return this.children.set(e,s),s},a.prototype.spawnCallback=function(t,e){var n,r,i,o=this,s=!1,a=new Set,c=new Set;try{i=t((function(t){r=t,c.forEach((function(e){return e(t)})),s||o.send(M(t,{origin:e}))}),(function(t){a.add(t)}))}catch(t){this.send($(e,t))}if(T(i))return this.spawnPromise(i,e);var u=((n={id:e,send:function(t){return a.forEach((function(e){return e(t)}))},subscribe:function(t){var e=R(t);return c.add(e.next),{unsubscribe:function(){c.delete(e.next)}}},stop:function(){s=!0,k(i)&&i()},toJSON:function(){return{id:e}},getSnapshot:function(){return r}})[C]=function(){return this},n);return this.children.set(e,u),u},a.prototype.spawnObservable=function(t,e){var n,r,i=this,o=t.subscribe((function(t){r=t,i.send(M(t,{origin:e}))}),(function(t){i.removeChild(e),i.send(M($(e,t),{origin:e}))}),(function(){i.removeChild(e),i.send(M(U(e),{origin:e}))})),s=((n={id:e,send:function(){},subscribe:function(e,n,r){return t.subscribe(e,n,r)},stop:function(){return o.unsubscribe()},getSnapshot:function(){return r},toJSON:function(){return{id:e}}})[C]=function(){return this},n);return this.children.set(e,s),s},a.prototype.spawnActor=function(t,e){return this.children.set(e,t),t},a.prototype.spawnActivity=function(t){var e=this.machine.options&&this.machine.options.activities?this.machine.options.activities[t.type]:void 0;if(e){var n=e(this.state.context,t);this.spawnEffect(t.id,n)}},a.prototype.spawnEffect=function(t,e){var n;this.children.set(t,((n={id:t,send:function(){},subscribe:function(){return{unsubscribe:function(){}}},stop:e||void 0,getSnapshot:function(){},toJSON:function(){return{id:t}}})[C]=function(){return this},n))},a.prototype.attachDev=function(){var t=rt();if(this.options.devTools&&t){if(t.__REDUX_DEVTOOLS_EXTENSION__){var e="object"==typeof this.options.devTools?this.options.devTools:void 0;this.devTools=t.__REDUX_DEVTOOLS_EXTENSION__.connect(r(r({name:this.id,autoPause:!0,stateSanitizer:function(t){return{value:t.value,context:t.context,actions:t.actions}}},e),{features:r({jump:!1,skip:!1},e?e.features:void 0)}),this.machine),this.devTools.init(this.state)}it(this)}},a.prototype.toJSON=function(){return{id:this.id}},a.prototype[C]=function(){return this},a.prototype.getSnapshot=function(){return this.status===t.InterpreterStatus.NotStarted?this.initialState:this._state},a.defaultOptions={execute:!0,deferEvents:!0,clock:{setTimeout:function(t,e){return setTimeout(t,e)},clearTimeout:function(t){return clearTimeout(t)}},logger:console.log.bind(console),devTools:!1},a.interpret=ut,a}();function ut(t,e){return new ct(t,e)}t.Interpreter=ct,t.interpret=ut,t.spawn=function(t,e){var n=function(t){return j(t)?r(r({},at),{name:t}):r(r(r({},at),{name:N()}),t)}(e);return function(e){return e?e.spawn(t,n.name,n):K(t,n.name)}(G[G.length-1])},Object.defineProperty(t,"__esModule",{value:!0})}));