xstate 5.32.2 → 6.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/README.md +25 -24
  2. package/dist/StateMachine-0aa98f54.development.esm.js +746 -0
  3. package/dist/StateMachine-aef700df.cjs.js +732 -0
  4. package/dist/StateMachine-d4931336.esm.js +729 -0
  5. package/dist/StateMachine-da872dde.development.cjs.js +749 -0
  6. package/dist/declarations/src/State.d.ts +11 -13
  7. package/dist/declarations/src/StateMachine.d.ts +40 -26
  8. package/dist/declarations/src/StateNode.d.ts +18 -54
  9. package/dist/declarations/src/actions.d.ts +14 -9
  10. package/dist/declarations/src/actors/callback.d.ts +42 -16
  11. package/dist/declarations/src/actors/index.d.ts +9 -6
  12. package/dist/declarations/src/actors/listener.d.ts +18 -0
  13. package/dist/declarations/src/actors/logic.d.ts +105 -0
  14. package/dist/declarations/src/actors/observable.d.ts +72 -27
  15. package/dist/declarations/src/actors/promise.d.ts +133 -48
  16. package/dist/declarations/src/actors/subscription.d.ts +22 -0
  17. package/dist/declarations/src/createActor.d.ts +33 -34
  18. package/dist/declarations/src/createMachine.d.ts +38 -7
  19. package/dist/declarations/src/createMachineFromConfig.d.ts +177 -0
  20. package/dist/declarations/src/graph/graph.d.ts +6 -4
  21. package/dist/declarations/src/graph/shortestPaths.d.ts +2 -2
  22. package/dist/declarations/src/graph/simplePaths.d.ts +2 -2
  23. package/dist/declarations/src/index.d.ts +13 -9
  24. package/dist/declarations/src/inspection.d.ts +84 -30
  25. package/dist/declarations/src/mapState.d.ts +4 -2
  26. package/dist/declarations/src/schema.types.d.ts +90 -0
  27. package/dist/declarations/src/serialize.d.ts +50 -0
  28. package/dist/declarations/src/setup.d.ts +396 -136
  29. package/dist/declarations/src/spawn.d.ts +7 -32
  30. package/dist/declarations/src/stateUtils.d.ts +22 -19
  31. package/dist/declarations/src/system.d.ts +8 -7
  32. package/dist/declarations/src/transition.d.ts +8 -5
  33. package/dist/declarations/src/transitionActions.d.ts +4 -0
  34. package/dist/declarations/src/types.d.ts +494 -429
  35. package/dist/declarations/src/types.v6.d.ts +423 -0
  36. package/dist/declarations/src/utils.d.ts +5 -3
  37. package/dist/index-219cb621.development.cjs.js +4476 -0
  38. package/dist/index-2f2fbd9b.development.esm.js +4421 -0
  39. package/dist/index-559ceab3.cjs.js +4437 -0
  40. package/dist/index-8828376f.esm.js +4382 -0
  41. package/dist/xstate-actors.cjs.js +12 -838
  42. package/dist/xstate-actors.cjs.mjs +10 -5
  43. package/dist/xstate-actors.development.cjs.js +12 -838
  44. package/dist/xstate-actors.development.cjs.mjs +10 -5
  45. package/dist/xstate-actors.development.esm.js +1 -836
  46. package/dist/xstate-actors.esm.js +1 -836
  47. package/dist/xstate-actors.umd.min.js +1 -1
  48. package/dist/xstate-actors.umd.min.js.map +1 -1
  49. package/dist/xstate-graph.cjs.js +18 -29
  50. package/dist/xstate-graph.development.cjs.js +18 -29
  51. package/dist/xstate-graph.development.esm.js +15 -26
  52. package/dist/xstate-graph.esm.js +15 -26
  53. package/dist/xstate-graph.umd.min.js +1 -1
  54. package/dist/xstate-graph.umd.min.js.map +1 -1
  55. package/dist/xstate.cjs.js +1477 -122
  56. package/dist/xstate.cjs.mjs +19 -22
  57. package/dist/xstate.development.cjs.js +1477 -122
  58. package/dist/xstate.development.cjs.mjs +19 -22
  59. package/dist/xstate.development.esm.js +1442 -86
  60. package/dist/xstate.esm.js +1442 -86
  61. package/dist/xstate.umd.min.js +1 -1
  62. package/dist/xstate.umd.min.js.map +1 -1
  63. package/package.json +3 -47
  64. package/dist/StateMachine-23c204bb.development.esm.js +0 -566
  65. package/dist/StateMachine-67a0f463.development.cjs.js +0 -569
  66. package/dist/StateMachine-94d215e3.cjs.js +0 -563
  67. package/dist/StateMachine-d1d80308.esm.js +0 -560
  68. package/dist/assign-43b1b558.development.esm.js +0 -133
  69. package/dist/assign-54b312bc.esm.js +0 -127
  70. package/dist/assign-b5bc78f7.cjs.js +0 -129
  71. package/dist/assign-f8f66278.development.cjs.js +0 -135
  72. package/dist/declarations/src/actions/assign.d.ts +0 -45
  73. package/dist/declarations/src/actions/cancel.d.ts +0 -39
  74. package/dist/declarations/src/actions/emit.d.ts +0 -44
  75. package/dist/declarations/src/actions/enqueueActions.d.ts +0 -57
  76. package/dist/declarations/src/actions/log.d.ts +0 -16
  77. package/dist/declarations/src/actions/raise.d.ts +0 -21
  78. package/dist/declarations/src/actions/send.d.ts +0 -42
  79. package/dist/declarations/src/actions/spawnChild.d.ts +0 -34
  80. package/dist/declarations/src/actions/stopChild.d.ts +0 -19
  81. package/dist/declarations/src/actors/transition.d.ts +0 -143
  82. package/dist/declarations/src/dev/index.d.ts +0 -14
  83. package/dist/declarations/src/guards.d.ts +0 -128
  84. package/dist/log-9b02c355.esm.js +0 -401
  85. package/dist/log-c94995c7.cjs.js +0 -409
  86. package/dist/log-d9f3f459.development.cjs.js +0 -437
  87. package/dist/log-db444cad.development.esm.js +0 -429
  88. package/dist/raise-235108c6.esm.js +0 -2762
  89. package/dist/raise-45f11c00.development.esm.js +0 -2824
  90. package/dist/raise-c445379d.cjs.js +0 -2817
  91. package/dist/raise-eca73993.development.cjs.js +0 -2879
  92. package/dist/xstate-actions.cjs.d.mts +0 -2
  93. package/dist/xstate-actions.cjs.d.ts +0 -2
  94. package/dist/xstate-actions.cjs.js +0 -23
  95. package/dist/xstate-actions.cjs.mjs +0 -14
  96. package/dist/xstate-actions.development.cjs.js +0 -23
  97. package/dist/xstate-actions.development.cjs.mjs +0 -14
  98. package/dist/xstate-actions.development.esm.js +0 -4
  99. package/dist/xstate-actions.esm.js +0 -4
  100. package/dist/xstate-actions.umd.min.js +0 -2
  101. package/dist/xstate-actions.umd.min.js.map +0 -1
  102. package/dist/xstate-dev.cjs.d.mts +0 -2
  103. package/dist/xstate-dev.cjs.d.ts +0 -2
  104. package/dist/xstate-dev.cjs.js +0 -48
  105. package/dist/xstate-dev.cjs.mjs +0 -5
  106. package/dist/xstate-dev.development.cjs.js +0 -51
  107. package/dist/xstate-dev.development.cjs.mjs +0 -5
  108. package/dist/xstate-dev.development.esm.js +0 -45
  109. package/dist/xstate-dev.esm.js +0 -42
  110. package/dist/xstate-dev.umd.min.js +0 -2
  111. package/dist/xstate-dev.umd.min.js.map +0 -1
  112. package/dist/xstate-guards.cjs.d.mts +0 -2
  113. package/dist/xstate-guards.cjs.d.ts +0 -2
  114. package/dist/xstate-guards.cjs.js +0 -14
  115. package/dist/xstate-guards.cjs.mjs +0 -7
  116. package/dist/xstate-guards.development.cjs.js +0 -14
  117. package/dist/xstate-guards.development.cjs.mjs +0 -7
  118. package/dist/xstate-guards.development.esm.js +0 -2
  119. package/dist/xstate-guards.esm.js +0 -2
  120. package/dist/xstate-guards.umd.min.js +0 -2
  121. package/dist/xstate-guards.umd.min.js.map +0 -1
@@ -1,2 +1,2 @@
1
- !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).XState={})}(this,function(t){"use strict";class e{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const s="xstate.init",n="xstate.stop";function o(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const i=t=>{const e=o();e&&e.register(t)};function r(t,e){return{type:`xstate.done.state.${t}`,output:e}}function a(t,e){return{type:`xstate.error.actor.${t}`,error:e,actorId:t}}function c(t){return{type:s,input:t}}function u(t){setTimeout(()=>{throw t})}const h="function"==typeof Symbol&&Symbol.observable||"@@observable";function f(t,e){const s=p(t),n=p(e);return"string"==typeof n?"string"==typeof s&&n===s:"string"==typeof s?s in n:Object.keys(s).every(t=>t in n&&f(s[t],n[t]))}function d(t){if(_(t))return t;const e=[];let s="";for(let n=0;n<t.length;n++){switch(t.charCodeAt(n)){case 92:s+=t[n+1],n++;continue;case 46:e.push(s),s="";continue}s+=t[n]}return e.push(s),e}function p(t){if(Rt(t))return t.value;if("string"!=typeof t)return t;return l(d(t))}function l(t){if(1===t.length)return t[0];const e={};let s=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)s[t[e]]=t[e+1];else{const n=s;s={},n[t[e]]=s}return e}function y(t,e){const s={},n=Object.keys(t);for(let o=0;o<n.length;o++){const i=n[o];s[i]=e(t[i],i,t,o)}return s}function g(t){return _(t)?t:[t]}function v(t){return void 0===t?[]:g(t)}function m(t,e,s,n){return"function"==typeof t?t({context:e,event:s,self:n}):t}function _(t){return Array.isArray(t)}function b(t){return g(t).map(t=>void 0===t||"string"==typeof t?{target:t}:t)}function x(t){if(void 0!==t&&""!==t)return v(t)}function S(t,e,s){const n="object"==typeof t,o=n?t:void 0;return{next:(n?t.next:t)?.bind(o),error:(n?t.error:e)?.bind(o),complete:(n?t.complete:s)?.bind(o)}}function w(t,e){return`${e}.${t}`}function I(t,e){const s=e.match(/^xstate\.invoke\.(\d+)\.(.*)/);if(!s)return t.implementations.actors[e];const[,n,o]=s,i=t.getStateNodeById(o).config.invoke;return(Array.isArray(i)?i[n]:i).src}function E(t,e){if(e===t)return!0;if("*"===e)return!0;if(!e.endsWith(".*"))return!1;const s=e.split("."),n=t.split(".");for(let t=0;t<s.length;t++){const e=s[t],o=n[t];if("*"===e){return t===s.length-1}if(e!==o)return!1}return!0}function k(t,e){return`${t.sessionId}.${e}`}let $=0;let T=!1;let O=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const j={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class A{constructor(t,s){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new e(this._process.bind(this)),this.observers=new Set,this.eventListeners=new Map,this.logger=void 0,this._processingStatus=O.NotStarted,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this.systemId=void 0,this.sessionId=void 0,this.system=void 0,this._doneEvent=void 0,this.src=void 0,this._deferred=[];const n={...j,...s},{clock:o,logger:i,parent:r,syncSnapshot:a,id:c,systemId:h,inspect:f}=n;this.system=r?r.system:function(t,e){const s=new Map,n=new Map,o=new WeakMap,i=new Set,r={},{clock:a,logger:c}=e,u={schedule:(t,e,s,n,o=Math.random().toString(36).slice(2))=>{const i={source:t,target:e,event:s,delay:n,id:o,startedAt:Date.now()},c=k(t,o);h._snapshot._scheduledEvents[c]=i;const u=a.setTimeout(()=>{delete r[c],delete h._snapshot._scheduledEvents[c],h._relay(t,e,s)},n);r[c]=u},cancel:(t,e)=>{const s=k(t,e),n=r[s];delete r[s],delete h._snapshot._scheduledEvents[s],void 0!==n&&a.clearTimeout(n)},cancelAll:t=>{for(const e in h._snapshot._scheduledEvents){const s=h._snapshot._scheduledEvents[e];s.source===t&&u.cancel(t,s.id)}}},h={_snapshot:{_scheduledEvents:(e?.snapshot&&e.snapshot.scheduler)??{}},_bookId:()=>"x:"+$++,_register:(t,e)=>(s.set(t,e),t),_unregister:t=>{s.delete(t.sessionId);const e=o.get(t);void 0!==e&&(n.delete(e),o.delete(t))},get:t=>n.get(t),getAll:()=>Object.fromEntries(n.entries()),_set:(t,e)=>{const s=n.get(t);if(s&&s!==e)throw new Error(`Actor with system ID '${t}' already exists.`);n.set(t,e),o.set(e,t)},inspect:t=>{const e=S(t);return i.add(e),{unsubscribe(){i.delete(e)}}},_sendInspectionEvent:e=>{if(!i.size)return;const s={...e,rootId:t.sessionId};i.forEach(t=>t.next?.(s))},_relay:(t,e,s)=>{h._sendInspectionEvent({type:"@xstate.event",sourceRef:t,actorRef:e,event:s}),e._send(s)},scheduler:u,getSnapshot:()=>({_scheduledEvents:{...h._snapshot._scheduledEvents}}),start:()=>{const t=h._snapshot._scheduledEvents;h._snapshot._scheduledEvents={};for(const e in t){const{source:s,target:n,event:o,delay:i,id:r}=t[e];u.schedule(s,n,o,i,r)}},_clock:a,_logger:c};return h}(this,{clock:o,logger:i}),f&&!r&&this.system.inspect(S(f)),this.sessionId=this.system._bookId(),this.id=c??this.sessionId,this.logger=s?.logger??this.system._logger,this.clock=s?.clock??this.system._clock,this._parent=r,this._syncSnapshot=a,this.options=n,this.src=n.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()},emit:t=>{const e=this.eventListeners.get(t.type),s=this.eventListeners.get("*");if(!e&&!s)return;const n=[...e?e.values():[],...s?s.values():[]];for(const e of n)try{e(t)}catch(t){u(t)}},actionExecutor:t=>{const e=()=>{if(this._actorScope.system._sendInspectionEvent({type:"@xstate.action",actorRef:this,action:{type:t.type,params:t.params}}),!t.exec)return;const e=T;try{T=!0,t.exec(t.info,t.params)}finally{T=e}};this._processingStatus===O.Running?e():this._deferred.push(e)}},this.send=this.send.bind(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this}),h&&(this.systemId=h,this.system._set(h,this)),this._initState(s?.snapshot??s?.state),h&&"active"!==this._snapshot.status&&this.system._unregister(this)}_initState(t){try{this._snapshot=t?this.logic.restoreSnapshot?this.logic.restoreSnapshot(t,this._actorScope):t:this.logic.getInitialSnapshot(this._actorScope,this.options?.input)}catch(t){this._snapshot={status:"error",output:void 0,error:t}}}update(t,e){let s;for(this._snapshot=t;s=this._deferred.shift();)try{s()}catch(e){this._deferred.length=0,this._snapshot={...t,status:"error",error:e}}switch(this._snapshot.status){case"active":for(const e of this.observers)try{e.next?.(t)}catch(t){u(t)}break;case"done":for(const e of this.observers)try{e.next?.(t)}catch(t){u(t)}this._stopProcedure(),this._complete(),this._doneEvent=(n=this.id,o=this._snapshot.output,{type:`xstate.done.actor.${n}`,output:o,actorId:n}),this._parent&&this.system._relay(this,this._parent,this._doneEvent);break;case"error":this._error(this._snapshot.error)}var n,o;this.system._sendInspectionEvent({type:"@xstate.snapshot",actorRef:this,event:e,snapshot:t})}subscribe(t,e,s){const n=S(t,e,s);if(this._processingStatus!==O.Stopped)this.observers.add(n);else switch(this._snapshot.status){case"done":try{n.complete?.()}catch(t){u(t)}break;case"error":{const t=this._snapshot.error;if(n.error)try{n.error(t)}catch(t){u(t)}else u(t);break}}return{unsubscribe:()=>{this.observers.delete(n)}}}on(t,e){let s=this.eventListeners.get(t);s||(s=new Set,this.eventListeners.set(t,s));const n=e.bind(void 0);return s.add(n),{unsubscribe:()=>{s.delete(n)}}}select(t,e=Object.is){return{subscribe:s=>{const n=S(s),o=this.getSnapshot();let i=t(o);return this.subscribe(s=>{const o=t(s);e(i,o)||(i=o,n.next?.(o))})},get:()=>t(this.getSnapshot())}}start(){if(this._processingStatus===O.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this.systemId&&this.system._set(this.systemId,this),this._processingStatus=O.Running;const t=c(this.options.input);this.system._sendInspectionEvent({type:"@xstate.event",sourceRef:this._parent,actorRef:this,event:t});switch(this._snapshot.status){case"done":return this.update(this._snapshot,t),this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._snapshot={...this._snapshot,status:"error",error:t},this._error(t),this}return this.update(this._snapshot,t),this.options.devTools&&this.attachDevTools(),this.mailbox.start(),this}_process(t){let e,s;try{e=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){s={err:t}}if(s){const{err:t}=s;return this._snapshot={...this._snapshot,status:"error",error:t},void this._error(t)}this.update(e,t),t.type===n&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===O.Stopped?this:(this.mailbox.clear(),this._processingStatus===O.NotStarted?(this._processingStatus=O.Stopped,this):(this.mailbox.enqueue({type:n}),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){u(t)}this.observers.clear(),this.eventListeners.clear()}_reportError(t){if(!this.observers.size)return this._parent||u(t),void this.eventListeners.clear();let e=!1;for(const s of this.observers){const n=s.error;e||=!n;try{n?.(t)}catch(t){u(t)}}this.observers.clear(),this.eventListeners.clear(),e&&u(t)}_error(t){this._stopProcedure(),this._reportError(t),this._parent&&this.system._relay(this,this._parent,a(this.id,t))}_stopProcedure(){return this._processingStatus!==O.Running||(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new e(this._process.bind(this)),this._processingStatus=O.Stopped,this.system._unregister(this)),this}_send(t){this._processingStatus!==O.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:i)(this)}}toJSON(){return{xstate$$type:1,id:this.id}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[h](){return this}getSnapshot(){return this._snapshot}}function M(t,...[e]){return new A(t,e)}const N=M;function P(t,e,s,n,{sendId:o}){return[e,{sendId:"function"==typeof o?o(s,n):o},void 0]}function C(t,e){t.defer(()=>{t.system.scheduler.cancel(t.self,e.sendId)})}function R(t){function e(t,e){}return e.type="xstate.cancel",e.sendId=t,e.resolve=P,e.execute=C,e}function V(t,e,s,n,{id:o,systemId:i,src:r,input:a,syncSnapshot:c}){const u="string"==typeof r?I(e.machine,r):r,h="function"==typeof o?o(s):o;let f,d;return u&&(d="function"==typeof a?a({context:e.context,event:s.event,self:t.self}):a,f=M(u,{id:h,src:r,parent:t.self,syncSnapshot:c,systemId:i,input:d})),[zt(e,{children:{...e.children,[h]:f}}),{id:o,systemId:i,actorRef:f,src:r,input:d},void 0]}function D(t,{actorRef:e}){e&&t.defer(()=>{e._processingStatus!==O.Stopped&&e.start()})}function J(...[t,{id:e,systemId:s,input:n,syncSnapshot:o=!1}={}]){function i(t,e){}return i.type="xstate.spawnChild",i.id=e,i.systemId=s,i.src=t,i.input=n,i.syncSnapshot=o,i.resolve=V,i.execute=D,i}function B(t,e,s,n,{actorRef:o}){const i="function"==typeof o?o(s,n):o,r="string"==typeof i?e.children[i]:i;let a=e.children;return r&&(a={...a},delete a[r.id]),[zt(e,{children:a}),r,void 0]}function L(t,e){const s=e.getSnapshot();if(s&&"children"in s)for(const e of Object.values(s.children))L(t,e);t.system._unregister(e)}function q(t,e){e&&(L(t,e),e._processingStatus===O.Running?t.defer(()=>{t.stopChild(e)}):t.stopChild(e))}function z(t){function e(t,e){}return e.type="xstate.stopChild",e.actorRef=t,e.resolve=B,e.execute=q,e}const W=z;function U(t,e,{stateValue:s}){if("string"==typeof s&&it(s)){const e=t.machine.getStateNodeById(s);return t._nodes.some(t=>t===e)}return t.matches(s)}function Q(t,{context:e,event:s},{guards:n}){return!H(n[0],e,s,t)}function F(t,{context:e,event:s},{guards:n}){return n.every(n=>H(n,e,s,t))}function G(t){function e(t,e){return!1}return e.check=F,e.guards=t,e}function X(t,{context:e,event:s},{guards:n}){return n.some(n=>H(n,e,s,t))}function H(t,e,s,n){const{machine:o}=n,i="function"==typeof t,r=i?t:o.implementations.guards["string"==typeof t?t:t.type];if(!i&&!r)throw new Error(`Guard '${"string"==typeof t?t:t.type}' is not implemented.'.`);if("function"!=typeof r)return H(r,e,s,n);const a={context:e,event:s},c=i||"string"==typeof t?void 0:"params"in t?"function"==typeof t.params?t.params({context:e,event:s}):t.params:void 0;if(!("check"in r))return r(a,c);return r.check(n,a,r)}function K(t){return"atomic"===t.type||"final"===t.type}function Y(t){return Object.values(t.states).filter(t=>"history"!==t.type)}function Z(t,e){const s=[];if(e===t)return s;let n=t.parent;for(;n&&n!==e;)s.push(n),n=n.parent;return s}function tt(t){const e=new Set(t),s=st(e);for(const t of e)if("compound"!==t.type||s.get(t)&&s.get(t).length){if("parallel"===t.type)for(const s of Y(t))if("history"!==s.type&&!e.has(s)){const t=ht(s);for(const s of t)e.add(s)}}else ht(t).forEach(t=>e.add(t));for(const t of e){let s=t.parent;for(;s;)e.add(s),s=s.parent}return e}function et(t,e){const s=e.get(t);if(!s)return{};if("compound"===t.type){const t=s[0];if(!t)return{};if(K(t))return t.key}const n={};for(const t of s)n[t.key]=et(t,e);return n}function st(t){const e=new Map;for(const s of t)e.has(s)||e.set(s,[]),s.parent&&(e.has(s.parent)||e.set(s.parent,[]),e.get(s.parent).push(s));return e}function nt(t,e){return et(t,st(tt(e)))}function ot(t,e){return"compound"===e.type?Y(e).some(e=>"final"===e.type&&t.has(e)):"parallel"===e.type?Y(e).every(e=>ot(t,e)):"final"===e.type}const it=t=>"#"===t[0];function rt(t){const e=t.config.after;if(!e)return[];const s=Object.keys(e).flatMap(s=>{const n=e[s],o="string"==typeof n?{target:n}:n,i=Number.isNaN(+s)?s:+s,r=(e=>{const s=(n=e,o=t.id,{type:`xstate.after.${n}.${o}`});var n,o;const i=s.type;return t.entry.push(te(s,{id:i,delay:e})),t.exit.push(R(i)),i})(i);return v(o).map(t=>({...t,event:r,delay:i}))});return s.map(e=>{const{delay:s}=e;return{...at(t,e.event,e),delay:s}})}function at(t,e,s){const n=x(s.target),o=s.reenter??!1,i=function(t,e){if(void 0===e)return;return e.map(e=>{if("string"!=typeof e)return e;if(it(e))return t.machine.getStateNodeById(e);const s="."===e[0];if(s&&!t.parent)return pt(t,e.slice(1));const n=s?t.key+e:e;if(!t.parent)throw new Error(`Invalid target: "${e}" is not a valid target from the root node. Did you mean ".${e}"?`);try{return pt(t.parent,n)}catch(e){throw new Error(`Invalid transition definition for state node '${t.id}':\n${e.message}`)}})}(t,n),r={...s,actions:v(s.actions),guard:s.guard,target:i,source:t,reenter:o,eventType:e,toJSON:()=>({...r,source:`#${t.id}`,target:i?i.map(t=>`#${t.id}`):void 0})};return r}function ct(t){const e=x(t.config.target);return e?{target:e.map(e=>"string"==typeof e?pt(t.parent,e):e)}:t.parent.initial}function ut(t){return"history"===t.type}function ht(t){const e=ft(t);for(const s of e)for(const n of Z(s,t))e.add(n);return e}function ft(t){const e=new Set;return function t(s){if(!e.has(s))if(e.add(s),"compound"===s.type)t(s.initial.target[0]);else if("parallel"===s.type)for(const e of Y(s))t(e)}(t),e}function dt(t,e){if(it(e))return t.machine.getStateNodeById(e);if(!t.states)throw new Error(`Unable to retrieve child state '${e}' from '${t.id}'; no child states exist.`);const s=t.states[e];if(!s)throw new Error(`Child state '${e}' does not exist on '${t.id}'`);return s}function pt(t,e){if("string"==typeof e&&it(e))try{return t.machine.getStateNodeById(e)}catch{}const s=d(e).slice();let n=t;for(;s.length;){const t=s.shift();if(!t.length)break;n=dt(n,t)}return n}function lt(t,e){if("string"==typeof e){const s=t.states[e];if(!s)throw new Error(`State '${e}' does not exist on '${t.id}'`);return[t,s]}const s=Object.keys(e),n=s.map(e=>dt(t,e)).filter(Boolean);return[t.machine.root,t].concat(n,s.reduce((s,n)=>{const o=dt(t,n);if(!o)return s;const i=lt(o,e[n]);return s.concat(i)},[]))}function yt(t,e,s,n){return"string"==typeof e?function(t,e,s,n){const o=dt(t,e).next(s,n);return o&&o.length?o:t.next(s,n)}(t,e,s,n):1===Object.keys(e).length?function(t,e,s,n){const o=Object.keys(e),i=yt(dt(t,o[0]),e[o[0]],s,n);return i&&i.length?i:t.next(s,n)}(t,e,s,n):function(t,e,s,n){const o=[];for(const i of Object.keys(e)){const r=e[i];if(!r)continue;const a=yt(dt(t,i),r,s,n);a&&o.push(...a)}return o.length?o:t.next(s,n)}(t,e,s,n)}function gt(t){return Object.keys(t.states).map(e=>t.states[e]).filter(t=>"history"===t.type)}function vt(t,e){let s=t;for(;s.parent&&s.parent!==e;)s=s.parent;return s.parent===e}function mt(t,e){const s=new Set(t),n=new Set(e);for(const t of s)if(n.has(t))return!0;for(const t of n)if(s.has(t))return!0;return!1}function _t(t,e,s){const n=new Set;for(const o of t){let t=!1;const i=new Set;for(const r of n)if(mt(St([o],e,s),St([r],e,s))){if(!vt(o.source,r.source)){t=!0;break}i.add(r)}if(!t){for(const t of i)n.delete(t);n.add(o)}}return Array.from(n)}function bt(t,e){if(!t.target)return[];const s=new Set;for(const n of t.target)if(ut(n))if(e[n.id])for(const t of e[n.id])s.add(t);else for(const t of bt(ct(n),e))s.add(t);else s.add(n);return[...s]}function xt(t,e){const s=bt(t,e);if(!s)return;if(!t.reenter&&s.every(e=>e===t.source||vt(e,t.source)))return t.source;const n=function(t){const[e,...s]=t;for(const t of Z(e,void 0))if(s.every(e=>vt(e,t)))return t}(s.concat(t.source));return n||(t.reenter?void 0:t.source.machine.root)}function St(t,e,s){const n=new Set;for(const o of t)if(o.target?.length){const t=xt(o,s);o.reenter&&o.source===t&&n.add(t);for(const s of e)vt(s,t)&&n.add(s)}return[...n]}function wt(t,e,s,n,o){return It([{target:[...ft(t)],source:t,reenter:!0,actions:[],eventType:null,toJSON:null}],e,s,n,!0,o)}function It(t,e,s,n,o,i){const a=[];if(!t.length)return[e,a];const c=s.actionExecutor;s.actionExecutor=t=>{a.push(t),c(t)};try{const c=new Set(e._nodes);let u=e.historyValue;const h=_t(t,c,u);let f=e;o||([f,u]=function(t,e,s,n,o,i,r){let a=t;const c=St(n,o,i);let u;c.sort((t,e)=>e.order-t.order);for(const t of c)for(const e of gt(t)){let s;s="deep"===e.history?e=>K(e)&&vt(e,t):e=>e.parent===t,u??={...i},u[e.id]=Array.from(o).filter(s)}for(const t of c)a=At(a,e,s,[...t.exit,...t.invoke.map(t=>z(t.id))],r,void 0),o.delete(t);return[a,u||i]}(f,n,s,h,c,u,i,s.actionExecutor)),f=At(f,n,s,h.flatMap(t=>t.actions),i,void 0),f=function(t,e,s,n,o,i,a,c){let u=t;const h=new Set,f=new Set;(function(t,e,s,n){for(const o of t){const t=xt(o,e);for(const i of o.target||[])ut(i)||o.source===i&&o.source===t&&!o.reenter||(n.add(i),s.add(i)),kt(i,e,s,n);const i=bt(o,e);for(const r of i){const i=Z(r,t);"parallel"===t?.type&&i.push(t),$t(n,e,s,i,!o.source.parent&&o.reenter?void 0:t)}}})(n,a,f,h),c&&f.add(t.machine.root);const d=new Set;for(const t of[...h].sort((t,e)=>t.order-e.order)){o.add(t);const n=[];n.push(...t.entry);for(const e of t.invoke)n.push(J(e.src,{...e,syncSnapshot:!!e.onSnapshot}));if(f.has(t)){const e=t.initial.actions;n.push(...e)}if(u=At(u,e,s,n,i,t.invoke.map(t=>t.id)),"final"===t.type){const n=t.parent;let a="parallel"===n?.type?n:n?.parent,c=a||t;for("compound"===n?.type&&i.push(r(n.id,void 0!==t.output?m(t.output,u.context,e,s.self):void 0));"parallel"===a?.type&&!d.has(a)&&ot(o,a);)d.add(a),i.push(r(a.id)),c=a,a=a.parent;if(a)continue;u=zt(u,{status:"done",output:Et(u,e,s,u.machine.root,c)})}}return u}(f,n,s,h,c,i,u,o);const d=[...c];"done"===f.status&&(f=At(f,n,s,d.sort((t,e)=>e.order-t.order).flatMap(t=>t.exit),i,void 0));try{return u===e.historyValue&&function(t,e){if(t.length!==e.size)return!1;for(const s of t)if(!e.has(s))return!1;return!0}(e._nodes,c)?[f,a]:[zt(f,{_nodes:d,historyValue:u}),a]}catch(t){throw t}}finally{s.actionExecutor=c}}function Et(t,e,s,n,o){if(void 0===n.output)return;const i=r(o.id,void 0!==o.output&&o.parent?m(o.output,t.context,e,s.self):void 0);return m(n.output,t.context,i,s.self)}function kt(t,e,s,n){if(ut(t))if(e[t.id]){const o=e[t.id];for(const t of o)n.add(t),kt(t,e,s,n);for(const i of o)Tt(i,t.parent,n,e,s)}else{const o=ct(t);for(const i of o.target)n.add(i),o===t.parent?.initial&&s.add(t.parent),kt(i,e,s,n);for(const i of o.target)Tt(i,t.parent,n,e,s)}else if("compound"===t.type){const[o]=t.initial.target;ut(o)||(n.add(o),s.add(o)),kt(o,e,s,n),Tt(o,t,n,e,s)}else if("parallel"===t.type)for(const o of Y(t).filter(t=>!ut(t)))[...n].some(t=>vt(t,o))||(ut(o)||(n.add(o),s.add(o)),kt(o,e,s,n))}function $t(t,e,s,n,o){for(const i of n)if(o&&!vt(i,o)||t.add(i),"parallel"===i.type)for(const n of Y(i).filter(t=>!ut(t)))[...t].some(t=>vt(t,n))||(t.add(n),kt(n,e,s,t))}function Tt(t,e,s,n,o){$t(s,n,o,Z(t,e))}function Ot(t,e){return t.implementations.actions[e]}function jt(t,e,s,n,o,i){const{machine:r}=t;let a=t;for(const t of n){const n="function"==typeof t,c=n?t:Ot(r,"string"==typeof t?t:t.type),u={context:a.context,event:e,self:s.self,system:s.system},h=n||"string"==typeof t?void 0:"params"in t?"function"==typeof t.params?t.params({context:a.context,event:e}):t.params:void 0;if(!c||!("resolve"in c)){s.actionExecutor({type:"string"==typeof t?t:"object"==typeof t?t.type:t.name||"(anonymous)",info:u,params:h,exec:c});continue}const f=c,[d,p,l]=f.resolve(s,a,u,h,c,o);a=d,"retryResolve"in f&&i?.push([f,p]),"execute"in f&&s.actionExecutor({type:f.type,info:u,params:p,exec:f.execute.bind(null,s,p)}),l&&(a=jt(a,e,s,l,o,i))}return a}function At(t,e,s,n,o,i){const r=i?[]:void 0,a=jt(t,e,s,n,{internalQueue:o,deferredActorIds:i},r);return r?.forEach(([t,e])=>{t.retryResolve(s,a,e)}),a}function Mt(t,e,o,i){let r=t;const a=[];function c(t,e,s){o.system._sendInspectionEvent({type:"@xstate.microstep",actorRef:o.self,event:e,snapshot:t[0],_transitions:s}),a.push(t)}if(e.type===n)return r=zt(Nt(r,e,o),{status:"stopped"}),c([r,[]],e,[]),{snapshot:r,microsteps:a};let u=e;if(u.type!==s){const e=u,s=function(t){return t.type.startsWith("xstate.error.actor")}(e),n=Pt(e,r);if(s&&!n.length)return r=zt(t,{status:"error",error:e.error}),c([r,[]],e,[]),{snapshot:r,microsteps:a};const h=It(n,t,o,u,!1,i);r=h[0],c(h,e,n)}let h=!0;const f=t.machine.options?.maxIterations??1/0;let d=0;for(;"active"===r.status;){if(d++,d>f)throw new Error(`Infinite loop detected: the machine has processed more than ${f} microsteps without reaching a stable state. This usually happens when there's a cycle of transitions (e.g., eventless transitions or raised events causing state A -> B -> C -> A).`);let t=h?Ct(r,u):[];const e=t.length?r:void 0;if(!t.length){if(!i.length)break;u=i.shift(),t=Pt(u,r)}const s=It(t,r,o,u,!1,i);r=s[0],h=r!==e,c(s,u,t)}return"active"!==r.status&&Nt(r,u,o),{snapshot:r,microsteps:a}}function Nt(t,e,s){return At(t,e,s,Object.values(t.children).map(t=>z(t)),[],void 0)}function Pt(t,e){return e.machine.getTransitionData(e,t)}function Ct(t,e){const s=new Set,n=t._nodes.filter(K);for(const o of n)t:for(const n of[o].concat(Z(o,void 0)))if(n.always)for(const o of n.always)if(void 0===o.guard||H(o.guard,t.context,e,t)){s.add(o);break t}return _t(Array.from(s),new Set(t._nodes),t.historyValue)}function Rt(t){return!!t&&"object"==typeof t&&"machine"in t&&"value"in t}const Vt=function(t){return f(t,this.value)},Dt=function(t){return this.tags.has(t)},Jt=function(t){const e=this.machine.getTransitionData(this,t);return!!e?.length&&e.some(t=>void 0!==t.target||t.actions.length)},Bt=function(){const{_nodes:t,tags:e,machine:s,getMeta:n,toJSON:o,can:i,hasTag:r,matches:a,...c}=this;return{...c,tags:Array.from(e)}},Lt=function(){return this._nodes.reduce((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t),{})};function qt(t,e){return{status:t.status,output:t.output,error:t.error,machine:e,context:t.context,_nodes:t._nodes,value:nt(e.root,t._nodes),tags:new Set(t._nodes.flatMap(t=>t.tags)),children:t.children,historyValue:t.historyValue||{},matches:Vt,hasTag:Dt,can:Jt,getMeta:Lt,toJSON:Bt}}function zt(t,e={}){return qt({...t,...e},t.machine)}function Wt(t){if("object"!=typeof t||null===t)return{};const e={};for(const s in t){const n=t[s];Array.isArray(n)&&(e[s]=n.map(t=>({id:t.id})))}return e}function Ut(t){let e;for(const s in t){const n=t[s];if(n&&"object"==typeof n)if("sessionId"in n&&"send"in n&&"ref"in n)e??=Array.isArray(t)?t.slice():{...t},e[s]={xstate$$type:1,id:n.id};else{const o=Ut(n);o!==n&&(e??=Array.isArray(t)?t.slice():{...t},e[s]=o)}}return e??t}function Qt(t,{machine:e,context:s},n,o){return(i,r)=>{const a=((i,r)=>{if("string"==typeof i){const a=I(e,i);if(!a)throw new Error(`Actor logic '${i}' not implemented in machine '${e.id}'`);const c=M(a,{id:r?.id,parent:t.self,syncSnapshot:r?.syncSnapshot,input:"function"==typeof r?.input?r.input({context:s,event:n,self:t.self}):r?.input,src:i,systemId:r?.systemId});return o[c.id]=c,c}return M(i,{id:r?.id,parent:t.self,syncSnapshot:r?.syncSnapshot,input:r?.input,src:i,systemId:r?.systemId})})(i,r);return o[a.id]=a,t.defer(()=>{a._processingStatus!==O.Stopped&&a.start()}),a}}function Ft(t,e,s,n,{assignment:o}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const i={},r={context:e.context,event:s.event,spawn:Qt(t,e,s.event,i),self:t.self,system:t.system};let a={};if("function"==typeof o)a=o(r,n);else for(const t of Object.keys(o)){const e=o[t];a[t]="function"==typeof e?e(r,n):e}return[zt(e,{context:Object.assign({},e.context,a),children:Object.keys(i).length?{...e.children,...i}:e.children}),void 0,void 0]}function Gt(t){function e(t,e){}return e.type="xstate.assign",e.assignment=t,e.resolve=Ft,e}function Xt(t,e,s,n,{event:o}){return[e,{event:"function"==typeof o?o(s,n):o},void 0]}function Ht(t,{event:e}){t.defer(()=>t.emit(e))}function Kt(t){function e(t,e){}return e.type="xstate.emit",e.event=t,e.resolve=Xt,e.execute=Ht,e}function Yt(t,e,s,n,{event:o,id:i,delay:r},{internalQueue:a}){const c=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with raise; use raise({ type: "${o}" }) instead`);const u="function"==typeof o?o(s,n):o;let h;if("string"==typeof r){const t=c&&c[r];h="function"==typeof t?t(s,n):t}else h="function"==typeof r?r(s,n):r;return"number"!=typeof h&&a.push(u),[e,{event:u,id:i,delay:h},void 0]}function Zt(t,e){const{event:s,delay:n,id:o}=e;"number"!=typeof n||t.defer(()=>{const e=t.self;t.system.scheduler.schedule(e,e,s,n,o)})}function te(t,e){function s(t,e){}return s.type="xstate.raise",s.event=t,s.id=e?.id,s.delay=e?.delay,s.resolve=Yt,s.execute=Zt,s}let ee=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});function se(t,e,s,n,{to:o,event:i,id:r,delay:a},c){const u=e.machine.implementations.delays;if("string"==typeof i)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${i}" }) instead`);const h="function"==typeof i?i(s,n):i;let f;if("string"==typeof a){const t=u&&u[a];f="function"==typeof t?t(s,n):t}else f="function"==typeof a?a(s,n):a;const d="function"==typeof o?o(s,n):o;let p;if("string"==typeof d){if(p=d===ee.Parent?t.self._parent:d===ee.Internal?t.self:d.startsWith("#_")?e.children[d.slice(2)]:c.deferredActorIds?.includes(d)?d:e.children[d],!p)throw new Error(`Unable to send event to actor '${d}' from machine '${e.machine.id}'.`)}else p=d||t.self;return[e,{to:p,targetId:"string"==typeof d?d:void 0,event:h,id:r,delay:f},void 0]}function ne(t,e,s){"string"==typeof s.to&&(s.to=e.children[s.to])}function oe(t,e){t.defer(()=>{const{to:s,event:n,delay:o,id:i}=e;"number"!=typeof o?t.system._relay(t.self,s,"xstate.error"===n.type?a(t.self.id,n.data):n):t.system.scheduler.schedule(t.self,s,n,o,i)})}function ie(t,e,s){function n(t,e){}return n.type="xstate.sendTo",n.to=t,n.event=e,n.id=s?.id,n.delay=s?.delay,n.resolve=se,n.retryResolve=ne,n.execute=oe,n}function re(t,e){return ie(ee.Parent,t,e)}function ae(t,e,s,n,{collect:o}){const i=[],r=function(t){i.push(t)};return r.assign=(...t)=>{i.push(Gt(...t))},r.cancel=(...t)=>{i.push(R(...t))},r.raise=(...t)=>{i.push(te(...t))},r.sendTo=(...t)=>{i.push(ie(...t))},r.sendParent=(...t)=>{i.push(re(...t))},r.spawnChild=(...t)=>{i.push(J(...t))},r.stopChild=(...t)=>{i.push(z(...t))},r.emit=(...t)=>{i.push(Kt(...t))},o({context:s.context,event:s.event,enqueue:r,check:t=>H(t,e.context,s.event,e),self:t.self,system:t.system},n),[e,void 0,i]}function ce(t){function e(t,e){}return e.type="xstate.enqueueActions",e.collect=t,e.resolve=ae,e}function ue(t,e,s,n,{value:o,label:i}){return[e,{value:"function"==typeof o?o(s,n):o,label:i},void 0]}function he({logger:t},{value:e,label:s}){s?t(s,e):t(e)}function fe(t=({context:t,event:e})=>({context:t,event:e}),e){function s(t,e){}return s.type="xstate.log",s.value=t,s.label=e,s.resolve=ue,s.execute=he,s}function de(t,e){return{config:t,transition:(e,s,n)=>({...e,context:t(e.context,s,n)}),getInitialSnapshot:(t,s)=>({status:"active",output:void 0,error:void 0,context:"function"==typeof e?e({input:s}):e}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}}const pe=new WeakMap;const le="xstate.observable.next",ye="xstate.observable.error",ge="xstate.observable.complete";const ve="xstate.promise.resolve",me="xstate.promise.reject",_e=new WeakMap;const be=de(t=>{},void 0);const xe=new WeakMap;function Se(t,e,s){let n=xe.get(t);return n?e in n||(n[e]=s()):(n={[e]:s()},xe.set(t,n)),n[e]}const we={},Ie=t=>"string"==typeof t?{type:t}:"function"==typeof t?"resolve"in t?{type:t.type}:{type:t.name}:t;class Ee{constructor(t,e){if(this.config=t,this.key=void 0,this.id=void 0,this.type=void 0,this.path=void 0,this.states=void 0,this.history=void 0,this.entry=void 0,this.exit=void 0,this.parent=void 0,this.machine=void 0,this.meta=void 0,this.output=void 0,this.order=-1,this.description=void 0,this.tags=[],this.transitions=void 0,this.always=void 0,this.parent=e._parent,this.key=e._key,this.machine=e._machine,this.path=this.parent?this.parent.path.concat(this.key):[],this.id=this.config.id||[this.machine.id,...this.path].join("."),this.type=this.config.type||(this.config.states&&Object.keys(this.config.states).length?"compound":this.config.history?"history":"atomic"),this.description=this.config.description,this.order=this.machine.idMap.size,this.machine.idMap.set(this.id,this),this.states=this.config.states?y(this.config.states,(t,e)=>new Ee(t,{_parent:this,_key:e,_machine:this.machine})):we,"compound"===this.type&&!this.config.initial)throw new Error(`No initial state specified for compound state node "#${this.id}". Try adding { initial: "${Object.keys(this.states)[0]}" } to the state config.`);this.history=!0===this.config.history?"shallow":this.config.history||!1,this.entry=v(this.config.entry).slice(),this.exit=v(this.config.exit).slice(),this.meta=this.config.meta,this.output="final"!==this.type&&this.parent?void 0:this.config.output,this.tags=v(t.tags).slice()}_initialize(){this.transitions=function(t){const e=new Map;if(t.config.on)for(const s of Object.keys(t.config.on)){if(""===s)throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.');const n=t.config.on[s];e.set(s,b(n).map(e=>at(t,s,e)))}if(t.config.onDone){const s=`xstate.done.state.${t.id}`;e.set(s,b(t.config.onDone).map(e=>at(t,s,e)))}for(const s of t.invoke){if(s.onDone){const n=`xstate.done.actor.${s.id}`;e.set(n,b(s.onDone).map(e=>at(t,n,e)))}if(s.onError){const n=`xstate.error.actor.${s.id}`;e.set(n,b(s.onError).map(e=>at(t,n,e)))}if(s.onSnapshot){const n=`xstate.snapshot.${s.id}`;e.set(n,b(s.onSnapshot).map(e=>at(t,n,e)))}}for(const s of t.after){let t=e.get(s.eventType);t||(t=[],e.set(s.eventType,t)),t.push(s)}return e}(this),this.config.always&&(this.always=b(this.config.always).map(t=>at(this,"",t))),Object.keys(this.states).forEach(t=>{this.states[t]._initialize()})}get definition(){return{id:this.id,key:this.key,version:this.machine.version,type:this.type,initial:this.initial?{target:this.initial.target,source:this,actions:this.initial.actions.map(Ie),eventType:null,reenter:!1,toJSON:()=>({target:this.initial.target.map(t=>`#${t.id}`),source:`#${this.id}`,actions:this.initial.actions.map(Ie),eventType:null})}:void 0,history:this.history,states:y(this.states,t=>t.definition),on:this.on,transitions:[...this.transitions.values()].flat().map(t=>({...t,actions:t.actions.map(Ie)})),entry:this.entry.map(Ie),exit:this.exit.map(Ie),meta:this.meta,order:this.order||-1,output:this.output,invoke:this.invoke,description:this.description,tags:this.tags}}toJSON(){return this.definition}get invoke(){return Se(this,"invoke",()=>v(this.config.invoke).map((t,e)=>{const{src:s,systemId:n}=t,o=t.id??w(this.id,e),i="string"==typeof s?s:`xstate.invoke.${w(this.id,e)}`;return{...t,src:i,id:o,systemId:n,toJSON(){const{onDone:e,onError:s,...n}=t;return{...n,type:"xstate.invoke",src:i,id:o}}}}))}get on(){return Se(this,"on",()=>[...this.transitions].flatMap(([t,e])=>e.map(e=>[t,e])).reduce((t,[e,s])=>(t[e]=t[e]||[],t[e].push(s),t),{}))}get after(){return Se(this,"delayedTransitions",()=>rt(this))}get initial(){return Se(this,"initial",()=>function(t,e){const s="string"==typeof e?t.states[e]:e?t.states[e.target]:void 0;if(!s&&e)throw new Error(`Initial state node "${e}" not found on parent state node #${t.id}`);const n={source:t,actions:e&&"string"!=typeof e?v(e.actions):[],eventType:null,reenter:!1,target:s?[s]:[],toJSON:()=>({...n,source:`#${t.id}`,target:s?[`#${s.id}`]:[]})};return n}(this,this.config.initial))}next(t,e){const s=e.type,n=[];let o;const i=Se(this,`candidates-${s}`,()=>function(t,e){const s=t.transitions.get(e),n=[...t.transitions.keys()].filter(t=>t!==e&&E(e,t)).sort((t,e)=>e.length-t.length).flatMap(e=>t.transitions.get(e));return s?[...s,...n]:n}(this,s));for(const r of i){const{guard:i}=r,a=t.context;let c=!1;try{c=!i||H(i,a,e,t)}catch(t){const e="string"==typeof i?i:"object"==typeof i?i.type:void 0;throw new Error(`Unable to evaluate guard ${e?`'${e}' `:""}in transition for event '${s}' in state node '${this.id}':\n${t.message}`)}if(c){n.push(...r.actions),o=r;break}}return o?[o]:void 0}get events(){return Se(this,"events",()=>{const{states:t}=this,e=new Set(this.ownEvents);if(t)for(const s of Object.keys(t)){const n=t[s];if(n.states)for(const t of n.events)e.add(`${t}`)}return Array.from(e)})}get ownEvents(){const t=Object.keys(Object.fromEntries(this.transitions)),e=new Set(t.filter(t=>this.transitions.get(t).some(t=>!(!t.target&&!t.actions.length&&!t.reenter))));return Array.from(e)}}class ke{constructor(t,e){this.config=t,this.version=void 0,this.schemas=void 0,this.implementations=void 0,this.options=void 0,this.__xstatenode=!0,this.idMap=new Map,this.root=void 0,this.id=void 0,this.states=void 0,this.events=void 0,this.id=t.id||"(machine)",this.implementations={actors:e?.actors??{},actions:e?.actions??{},delays:e?.delays??{},guards:e?.guards??{}},this.version=this.config.version,this.schemas=this.config.schemas,this.options={maxIterations:1/0,...this.config.options},this.transition=this.transition.bind(this),this.getInitialSnapshot=this.getInitialSnapshot.bind(this),this.getPersistedSnapshot=this.getPersistedSnapshot.bind(this),this.restoreSnapshot=this.restoreSnapshot.bind(this),this.start=this.start.bind(this),this.root=new Ee(t,{_key:this.id,_machine:this}),this.root._initialize(),function(t){const e=[],s=n=>{Object.values(n).forEach(n=>{if(n.config.route&&n.config.id){const s=n.config.id,o=n.config.route.guard,i=({event:t})=>t.to===`#${s}`,r={...n.config.route,guard:o?G([i,o]):i,target:`#${s}`};e.push(at(t,"xstate.route",r))}n.states&&s(n.states)})};s(t.states),e.length>0&&t.transitions.set("xstate.route",e)}(this.root),this.states=this.root.states,this.events=this.root.events}provide(t){const{actions:e,guards:s,actors:n,delays:o}=this.implementations;return new ke(this.config,{actions:{...e,...t.actions},guards:{...s,...t.guards},actors:{...n,...t.actors},delays:{...o,...t.delays}})}resolveState(t){const e=(s=this.root,n=t.value,nt(s,[...tt(lt(s,n))]));var s,n;const o=tt(lt(this.root,e));return qt({_nodes:[...o],context:t.context||{},children:{},status:ot(o,this.root)?"done":t.status||"active",output:t.output,error:t.error,historyValue:t.historyValue},this)}transition(t,e,s){return Mt(t,e,s,[]).snapshot}microstep(t,e,s){return Mt(t,e,s,[]).microsteps.map(([t])=>t)}getTransitionData(t,e){return yt(this.root,t.value,t,e)||[]}_getPreInitialState(t,e,s){const{context:n}=this.config,o=qt({context:"function"!=typeof n&&n?n:{},_nodes:[this.root],children:{},status:"active"},this);if("function"==typeof n){return At(o,e,t,[Gt(({spawn:t,event:e,self:s})=>n({spawn:t,input:e.input,self:s}))],s,void 0)}return o}getInitialSnapshot(t,e){const s=c(e),n=[],o=this._getPreInitialState(t,s,n),[i]=wt(this.root,o,t,s,n),{snapshot:r}=Mt(i,s,t,n);return r}start(t){Object.values(t.children).forEach(t=>{"active"===t.getSnapshot().status&&t.start()})}getStateNodeById(t){const e=d(t),s=e.slice(1),n=it(e[0])?e[0].slice(1):e[0],o=this.idMap.get(n);if(!o)throw new Error(`Child state node '#${n}' does not exist on machine '${this.id}'`);return pt(o,s)}get definition(){return this.root.definition}toJSON(){return this.definition}getPersistedSnapshot(t,e){return function(t,e){const{_nodes:s,tags:n,machine:o,children:i,context:r,can:a,hasTag:c,matches:u,getMeta:h,toJSON:f,...d}=t,p={};for(const t in i){const s=i[t];p[t]={snapshot:s.getPersistedSnapshot(e),src:s.src,systemId:s.systemId,syncSnapshot:s._syncSnapshot}}return{...d,context:Ut(r),children:p,historyValue:Wt(d.historyValue)}}(t,e)}restoreSnapshot(t,e){const s={},n=t.children;function o(t,e){if(e instanceof Ee)return e;try{return t.machine.getStateNodeById(e.id)}catch{}}Object.keys(n).forEach(t=>{const o=n[t],i=o.snapshot,r=o.src,a="string"==typeof r?I(this,r):r;if(!a)return;const c=M(a,{id:t,parent:e.self,syncSnapshot:o.syncSnapshot,snapshot:i,src:r,systemId:o.systemId});s[t]=c});const i=function(t,e){if(!e||"object"!=typeof e)return{};const s={};for(const n in e){const i=e[n];for(const e of i){const i=o(t,e);i&&(s[n]??=[],s[n].push(i))}}return s}(this.root,t.historyValue),r=qt({...t,children:s,_nodes:Array.from(tt(lt(this.root,t.value))),historyValue:i},this),a=new Set;return function t(e,s){if(!a.has(e)){a.add(e);for(const n in e){const o=e[n];if(o&&"object"==typeof o){if("xstate$$type"in o&&1===o.xstate$$type){e[n]=s[o.id];continue}t(o,s)}}}}(r.context,s),r}}function $e(t,e){return new ke(t,e)}function Te(t){const e=M(t);return{self:e,defer:()=>{},id:"",logger:()=>{},sessionId:"",stopChild:()=>{},system:e.system,emit:()=>{},actionExecutor:()=>{}}}const Oe={timeout:1/0};t.Actor=A,t.SimulatedClock=class{constructor(){this.timeouts=new Map,this._now=0,this._id=0,this._flushing=!1,this._flushingInvalidated=!1}now(){return this._now}getId(){return this._id++}setTimeout(t,e){this._flushingInvalidated=this._flushing;const s=this.getId();return this.timeouts.set(s,{start:this.now(),timeout:e,fn:t}),s}clearTimeout(t){this._flushingInvalidated=this._flushing,this.timeouts.delete(t)}set(t){if(this._now>t)throw new Error("Unable to travel back in time");this._now=t,this.flushTimeouts()}flushTimeouts(){if(this._flushing)return void(this._flushingInvalidated=!0);this._flushing=!0;const t=[...this.timeouts].sort(([t,e],[s,n])=>{const o=e.start+e.timeout;return n.start+n.timeout>o?-1:1});for(const[e,s]of t){if(this._flushingInvalidated)return this._flushingInvalidated=!1,this._flushing=!1,void this.flushTimeouts();this.now()-s.start>=s.timeout&&(this.timeouts.delete(e),s.fn.call(null))}this._flushing=!1}increment(t){this._now+=t,this.flushTimeouts()}},t.SpecialTargets=ee,t.StateMachine=ke,t.StateNode=Ee,t.__unsafe_getAllOwnEventDescriptors=function(t){return[...new Set([...t._nodes.flatMap(t=>t.ownEvents)])]},t.and=G,t.assertEvent=function(t,e){const s=v(e);if(!s.some(e=>E(t.type,e))){const e=1===s.length?`type matching "${s[0]}"`:`one of types matching "${s.join('", "')}"`;throw new Error(`Expected event ${JSON.stringify(t)} to have ${e}`)}},t.assign=Gt,t.cancel=R,t.createActor=M,t.createEmptyActor=function(){return M(be)},t.createMachine=$e,t.emit=Kt,t.enqueueActions=ce,t.forwardTo=function(t,e){return ie(t,({event:t})=>t,e)},t.fromCallback=function(t){const e={config:t,start:(e,s)=>{const{self:n,system:o,emit:i}=s,r={receivers:void 0,dispose:void 0};pe.set(n,r),r.dispose=t({input:e.input,system:o,self:n,sendBack:t=>{"stopped"!==n.getSnapshot().status&&n._parent&&o._relay(n,n._parent,t)},receive:t=>{r.receivers??=new Set,r.receivers.add(t)},emit:i})},transition:(t,e,s)=>{const o=pe.get(s.self);return e.type===n?(t={...t,status:"stopped",error:void 0},pe.delete(s.self),o.receivers?.clear(),o.dispose?.(),t):(o.receivers?.forEach(t=>t(e)),t)},getInitialSnapshot:(t,e)=>({status:"active",output:void 0,error:void 0,input:e}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t};return e},t.fromEventObservable=function(t){const e={config:t,transition:(t,e)=>{if("active"!==t.status)return t;switch(e.type){case ye:return{...t,status:"error",error:e.data,input:void 0,_subscription:void 0};case ge:return{...t,status:"done",input:void 0,_subscription:void 0};case n:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialSnapshot:(t,e)=>({status:"active",output:void 0,error:void 0,context:void 0,input:e,_subscription:void 0}),start:(e,{self:s,system:n,emit:o})=>{"done"!==e.status&&(e._subscription=t({input:e.input,system:n,self:s,emit:o}).subscribe({next:t=>{s._parent&&n._relay(s,s._parent,t)},error:t=>{n._relay(s,s,{type:ye,data:t})},complete:()=>{n._relay(s,s,{type:ge})}}))},getPersistedSnapshot:({_subscription:t,...e})=>e,restoreSnapshot:t=>({...t,_subscription:void 0})};return e},t.fromObservable=function(t){const e={config:t,transition:(t,e)=>{if("active"!==t.status)return t;switch(e.type){case le:return{...t,context:e.data};case ye:return{...t,status:"error",error:e.data,input:void 0,_subscription:void 0};case ge:return{...t,status:"done",input:void 0,_subscription:void 0};case n:return t._subscription.unsubscribe(),{...t,status:"stopped",input:void 0,_subscription:void 0};default:return t}},getInitialSnapshot:(t,e)=>({status:"active",output:void 0,error:void 0,context:void 0,input:e,_subscription:void 0}),start:(e,{self:s,system:n,emit:o})=>{"done"!==e.status&&(e._subscription=t({input:e.input,system:n,self:s,emit:o}).subscribe({next:t=>{n._relay(s,s,{type:le,data:t})},error:t=>{n._relay(s,s,{type:ye,data:t})},complete:()=>{n._relay(s,s,{type:ge})}}))},getPersistedSnapshot:({_subscription:t,...e})=>e,restoreSnapshot:t=>({...t,_subscription:void 0})};return e},t.fromPromise=function(t){const e={config:t,transition:(t,e,s)=>{if("active"!==t.status)return t;switch(e.type){case ve:{const s=e.data;return{...t,status:"done",output:s,input:void 0}}case me:return{...t,status:"error",error:e.data,input:void 0};case n:return _e.get(s.self)?.abort(),_e.delete(s.self),{...t,status:"stopped",input:void 0};default:return t}},start:(e,{self:s,system:n,emit:o})=>{if("active"!==e.status)return;const i=new AbortController;_e.set(s,i);Promise.resolve(t({input:e.input,system:n,self:s,signal:i.signal,emit:o})).then(t=>{"active"===s.getSnapshot().status&&(_e.delete(s),n._relay(s,s,{type:ve,data:t}))},t=>{"active"===s.getSnapshot().status&&(_e.delete(s),n._relay(s,s,{type:me,data:t}))})},getInitialSnapshot:(t,e)=>({status:"active",output:void 0,error:void 0,input:e}),getPersistedSnapshot:t=>t,restoreSnapshot:t=>t};return e},t.fromTransition=de,t.getInitialMicrosteps=function(t,...[e]){const s=Te(t),n=c(e),o=[],i=t._getPreInitialState(s,n,o),r=wt(t.root,i,s,n,o),{microsteps:a}=Mt(r[0],n,s,o);return[r,...a]},t.getInitialSnapshot=function(t,...[e]){const s=Te(t);return t.getInitialSnapshot(s,e)},t.getMicrosteps=function(t,e,s){const n=Te(t),{microsteps:o}=Mt(e,s,n,[]);return o},t.getNextSnapshot=function(t,e,s){const n=Te(t);return n.self._snapshot=e,t.transition(e,s,n)},t.getNextTransitions=function(t){const e=[],s=t._nodes.filter(K),n=new Set;for(const t of s)for(const s of[t].concat(Z(t,void 0)))if(!n.has(s.id)){n.add(s.id);for(const[,t]of s.transitions)e.push(...t);s.always&&e.push(...s.always)}return e},t.getStateNodes=lt,t.initialTransition=function(t,...[e]){const s=[],n=Te(t);return n.actionExecutor=t=>{s.push(t)},[t.getInitialSnapshot(n,e),s]},t.interpret=N,t.isMachineSnapshot=Rt,t.log=fe,t.mapState=function(t,e){const s=[],n=(t,e)=>{let s=t;for(const t of e){if(!s?.states)return;const e=s.states;if(!(t in e))return;s=e[t]}return s},o=new Set;for(const i of t._nodes.filter(K)){let r=i;for(;r&&!o.has(r);){o.add(r);const i=n(e,r.path);i?.map&&s.push({stateNode:r,result:i.map(t)}),r=r.parent}}return s},t.matchesState=f,t.not=function(t){function e(t,e){return!1}return e.check=Q,e.guards=[t],e},t.or=function(t){function e(t,e){return!1}return e.check=X,e.guards=t,e},t.pathToStateValue=l,t.raise=te,t.sendParent=re,t.sendTo=ie,t.setup=function t({schemas:e,actors:s,actions:n,guards:o,delays:i}){return{assign:Gt,sendTo:ie,raise:te,log:fe,cancel:R,stopChild:z,enqueueActions:ce,emit:Kt,spawnChild:J,createStateConfig:t=>t,createAction:t=>t,createMachine:t=>$e({...t,schemas:e},{actors:s,actions:n,guards:o,delays:i}),extend:r=>t({schemas:e,actors:s,actions:{...n,...r.actions},guards:{...o,...r.guards},delays:{...i,...r.delays}})}},t.spawnChild=J,t.stateIn=function(t){function e(){return!1}return e.check=U,e.stateValue=t,e},t.stop=W,t.stopChild=z,t.toObserver=S,t.toPromise=function(t){return new Promise((e,s)=>{t.subscribe({complete:()=>{e(t.getSnapshot().output)},error:s})})},t.transition=function(t,e,s){const n=[],o=Te(t);return o.actionExecutor=t=>{n.push(t)},[t.transition(e,s,o),n]},t.waitFor=function(t,e,s){const n={...Oe,...s};return new Promise((s,o)=>{const{signal:i}=n;if(i?.aborted)return void o(i.reason);let r=!1;const a=n.timeout===1/0?void 0:setTimeout(()=>{c(),o(new Error(`Timeout of ${n.timeout} ms exceeded`))},n.timeout),c=()=>{clearTimeout(a),r=!0,f?.unsubscribe(),h&&i.removeEventListener("abort",h)};function u(t){e(t)&&(c(),s(t))}let h,f;u(t.getSnapshot()),r||(i&&(h=()=>{c(),o(i.reason)},i.addEventListener("abort",h)),f=t.subscribe({next:u,error:t=>{c(),o(t)},complete:()=>{c(),o(new Error("Actor terminated without satisfying predicate"))}}),r&&f.unsubscribe())})},Object.defineProperty(t,"__esModule",{value:!0})});
1
+ !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).XState={})}(this,function(t){"use strict";class e{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const n="@xstate.init",s="@xstate.stop";function o(t,e){return{type:`xstate.after.${t}.${e}`}}function i(t){return{type:`xstate.timeout.actor.${t}`}}function r(t,e){return{type:`xstate.done.state.${t}`,output:e}}function c(t){return{type:n,input:t}}function a(t){setTimeout(()=>{throw t})}const u="function"==typeof Symbol&&Symbol.observable||"@@observable",f={"@xstate.start":t=>{t.start()},"@xstate.raise":(t,e,n)=>{t.system.scheduler.schedule(t.self,t.self,e,n?.delay??0,n?.id)},"@xstate.sendTo":(t,e,n,s)=>{if("string"==typeof n)throw new Error("Only event objects may be used with sendTo");void 0!==s?.delay?t.system.scheduler.schedule(t.self,e,n,s?.delay??0,s?.id):t.defer(()=>{t.system._relay(t.self,e,n)})},"@xstate.cancel":(t,e)=>{t.system.scheduler.cancel(t.self,e)},"@xstate.stop":(t,e)=>{t.stopChild(e)}},h=new WeakMap;function d(t){const e=(t,e)=>[{status:"active",output:void 0,error:void 0,input:t},[]];return{start:(e,{self:n,system:s})=>{const o=e.input.actor;if(!o||"stopped"===o.getSnapshot().status)return;const i=t(e.input,{self:n,system:s});if(!i)return;let r,c=!1;const a=()=>{c||(c=!0,i.unsubscribe(),r?.unsubscribe())},u=n._parent;u&&(r=u.subscribe({complete:a,error:a})),h.set(n,{unsubscribe:a})},transition:(t,e,{self:n})=>e.type===s?(h.get(n)?.unsubscribe(),h.delete(n),[{...t,status:"stopped",error:void 0},[]]):[t,[]],initialTransition:e,getInitialSnapshot:(t,n)=>e(n)[0],getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}}function l(t,e,n){"stopped"!==t.getSnapshot().status&&t._parent&&e._relay(t,t._parent,n())}function p(){return d(({actor:t,eventType:e,mapper:n},{self:s,system:o})=>{const i="*"!==e&&e.endsWith(".*");return t.on(i?"*":e,t=>{i&&!Ct(t.type,e)||l(s,o,()=>n(t))})})}const y=p();function g(){return d(({actor:t,mappers:e},{self:n,system:s})=>{const{done:o,error:i,snapshot:r}=e;return t.subscribe({next:t=>{"done"===t.status&&o?l(n,s,()=>o(t.output)):"error"===t.status&&i?l(n,s,()=>i(t.error)):"active"===t.status&&r&&l(n,s,()=>r(t))},error:t=>{i&&l(n,s,()=>i(t))},complete:()=>{}})})}const m=g();function v(t,e){return{...t,...e}}function _(t,e,...n){t.push({action:e,args:n})}function x(t,e,n){_(t,f["@xstate.start"],e),t.push(function(t,e){return Object.assign(function(n){return{children:{...n.children,[e]:t}}},{_special:!0})}(e,n??e.id))}function b(t,e,n,s=!1){const o={cancel:n=>{_(e,f["@xstate.cancel"],t,n)},emit:t=>{e.push(t)},log:(...n)=>{_(e,t.logger,...n)},raise:(s,o)=>{if("string"==typeof s)throw new Error("Only event objects may be used with raise");void 0!==o?.delay?_(e,f["@xstate.raise"],t,s,o):n.push(s)},spawn:(n,s)=>{const o=te(n,{...s,parent:t.self});return x(e,o,s?.id),o},sendTo:(n,s,o)=>{n&&_(e,f["@xstate.sendTo"],t,n,s,o)},stop:n=>{n&&(_(e,f["@xstate.stop"],t,n),e.push(function(t){return Object.assign(function(e){const n={...e.children};for(const e of Object.keys(n))n[e]===t&&delete n[e];return{children:n}},{_special:!0})}(n)))}};return s&&Object.assign(o,{listen:(n,s,o)=>{const i=te(y,{input:{actor:n,eventType:s,mapper:o},parent:t.self});return _(e,f["@xstate.start"],i),i},subscribeTo:(n,s)=>{const o=te(m,{input:{actor:n,mappers:"function"==typeof s?{snapshot:s}:s},parent:t.self});return _(e,f["@xstate.start"],o),o}}),E(o,(t,...n)=>{_(e,t,...n)})}function S(t,e){switch(t){case f["@xstate.start"]:{const[t]=e;return{actor:t,id:t.id,logic:t.logic,src:t.src,input:t.options?.input}}case f["@xstate.raise"]:{const[,t,n]=e;return{event:t,id:n?.id,delay:n?.delay}}case f["@xstate.sendTo"]:{const[,t,n,s]=e;return{target:t,event:n,id:s?.id,delay:s?.delay}}case f["@xstate.cancel"]:{const[,t]=e;return{id:t}}case f["@xstate.stop"]:{const[,t]=e;return{actor:t}}default:return}}function w(t,e,n,s){let o=t;const i=[];for(const r of s){const s="function"==typeof r?r:"object"==typeof r&&"action"in r&&"function"==typeof r.action&&r.action.bind(null,...r.args),c={context:o.context,event:e,output:Ot(e),self:n.self,system:n.system,children:o.children,parent:n.self._parent,actions:t.machine.implementations.actions,actorSources:t.machine.implementations.actorSources};let a;if("object"==typeof r&&null!==r){const{type:t,...e}=r;a=e}if(s&&"_special"in s){i.push({type:"object"==typeof r?"action"in r&&"function"==typeof r.action?r.action.name??"(anonymous)":r.type??"(anonymous)":r.name||"(anonymous)",params:a,args:[],exec:void 0});const t=s(c,k);t&&("context"in t||"children"in t)&&(o=bt(o,{...void 0!==t.context?{context:v(o.context,t.context)}:{},..."children"in t?{children:t.children}:{}}));continue}if(!s||!("resolve"in s)){const t="object"==typeof r&&null!==r&&"action"in r&&"function"==typeof r.action?S(r.action,r.args):void 0;i.push({type:"object"==typeof r?"action"in r&&"function"==typeof r.action?r.action.name??"(anonymous)":r.type:r.name||"(anonymous)",params:a,args:"object"==typeof r&&"action"in r?r.args:[],exec:s||("object"==typeof r&&null!==r?()=>n.defer(()=>n.emit(r)):void 0),...t});continue}}return[o,i]}function E(t,e){const n=(t,...n)=>{e(t,...n)};return Object.assign(n,{cancel:()=>{},emit:()=>{},log:()=>{},raise:()=>{},spawn:()=>({}),sendTo:()=>{},stop:()=>{},listen:()=>({}),subscribeTo:()=>({}),...t}),n}const k=E({},()=>{});function $(t){const e=t.trim(),n=e.match(/^(\d+)ms$/i);if(n)return parseInt(n[1],10);const s=e.match(/^(\d*)(\.?)(\d*)s$/i);if(s){const t=s[1]?parseInt(s[1],10):0,e=!!s[2],n=s[3]?parseInt(s[3].padEnd(3,"0").slice(0,3),10):0;return 1e3*t+(e?n:0)}const o=e.match(/^P(?:(?<weeks>\d+(?:[.,]\d+)?)W)?(?:(?<days>\d+(?:[.,]\d+)?)D)?(?:T(?:(?<hours>\d+(?:[.,]\d+)?)H)?(?:(?<minutes>\d+(?:[.,]\d+)?)M)?(?:(?<seconds>\d+(?:[.,]\d+)?)S)?)?$/i);if(!o?.groups)return;const{weeks:i,days:r,hours:c,minutes:a,seconds:u}=o.groups;if(!(i||r||c||a||u))return;const f=t=>t?Number(t.replace(",",".")):0;return 7*f(i)*24*60*60*1e3+24*f(r)*60*60*1e3+60*f(c)*60*1e3+60*f(a)*1e3+1e3*f(u)}function I(t){if(void 0!==t)return"number"==typeof t?t:$(t)}function j(t,e,n){if("function"==typeof t)return t(n);const s=function(t,e){if("string"!=typeof t)return t;const n=e[t];return void 0!==n?n:$(t)??t}(t,e);return"function"==typeof s?s(n):s}function T(t){return"atomic"===t.type||"final"===t.type||"choice"===t.type}function A(t){return Object.values(t.states).filter(t=>"history"!==t.type)}function O(t,e){const n=[];if(e===t)return n;let s=t.parent;for(;s&&s!==e;)n.push(s),s=s.parent;return n}function M(t){const e=new Set(t),n=new Set;for(const t of e)t.parent&&n.add(t.parent);for(const t of e)if("compound"!==t.type||n.has(t)){if("parallel"===t.type)for(const n of A(t))if(!e.has(n)){const t=W(n);for(const n of t)e.add(n)}}else for(const n of W(t))e.add(n);for(const t of e){let n=t.parent;for(;n&&!e.has(n);)e.add(n),n=n.parent}return e}function N(t,e){const n=M(e),s=new Map;for(const t of n)s.has(t)||s.set(t,[]),t.parent&&(s.has(t.parent)||s.set(t.parent,[]),s.get(t.parent).push(t));const o=t=>{const e=s.get(t);if(!e)return{};if("compound"===t.type){const t=e[0];if(!t)return{};if(T(t))return t.key}const n={};for(const t of e)n[t.key]=o(t);return n};return o(t)}function D(t,e){return"compound"===e.type?A(e).some(e=>"final"===e.type&&t.has(e)):"parallel"===e.type?A(e).every(e=>D(t,e)):"final"===e.type}const P=t=>"#"===t[0];function R(t,e,n){const s=e.type,o=t.entry;t.entry=(t,i)=>(i.raise(e,{id:s,delay:n(t)}),"function"==typeof o?o(t,i):void 0);const i=t.exit;return t.exit=(t,e)=>(e.cancel(s),"function"==typeof i?i(t,e):void 0),s}function C(t,e,n){const s=Dt(n.target),o=n.reenter??!1,i=L(t,s),r={...n,target:i,source:t,reenter:o,eventType:e,toJSON:()=>({...r,source:`#${t.id}`,target:i?i.map(t=>`#${t.id}`):void 0})};return r}function L(t,e){if(void 0!==e)return e.map(e=>{if("string"!=typeof e)return e;if(P(e))return t.machine.getStateNodeById(e);const n="."===e[0];if(n&&!t.parent)return B(t,e.slice(1));const s=n?t.key+e:e;if(!t.parent)throw new Error(`Invalid target: "${e}"`);try{return B(t.parent,s)}catch(e){throw new Error(`Invalid transition definition for state node '${t.id}':\n${e.message}`)}})}function z(t){const e=Dt(t.config.target);return e?{target:e.map(e=>"string"==typeof e?B(t.parent,e):e),source:t,reenter:!1,eventType:""}:t.parent.initial}function K(t){return"history"===t.type}function W(t){const e=new Set;!function t(n){if(!e.has(n))if(e.add(n),"compound"===n.type)t(n.initial.target[0]);else if("parallel"===n.type)for(const e of A(n))t(e)}(t);for(const n of e)for(const s of O(n,t))e.add(s);return e}function V(t,e){if(P(e))return t.machine.getStateNodeById(e);if(!t.states)throw new Error(`Unable to retrieve child state '${e}' from '${t.id}'; no child states exist.`);const n=t.states[e];if(!n)throw new Error(`Child state '${e}' does not exist on '${t.id}'`);return n}function B(t,e){if("string"==typeof e&&P(e))try{return t.machine.getStateNodeById(e)}catch{}const n=kt(e).slice();let s=t;for(;n.length;){const t=n.shift();if(!t.length)break;s=V(s,t)}return s}function J(t,e){if("string"==typeof e){const n=t.states[e];if(!n)throw new Error(`State '${e}' does not exist on '${t.id}'`);return[t,n]}const n=Object.keys(e),s=new Array(n.length),o=[t.machine.root,t];for(let e=0;e<n.length;e++){const i=V(t,n[e]);s[e]=i,o.push(i)}for(let t=0;t<n.length;t++)o.push(...J(s[t],e[n[t]]));return o}function q(t,e,n,s,o){if("string"==typeof e){const i=V(t,e).next(n,s,o);return i&&i.length?i:t.next(n,s,o)}const i=Object.keys(e),r=i[0];if(1===i.length){const i=q(V(t,r),e[r],n,s,o);return i&&i.length?i:t.next(n,s,o)}const c=[];for(const r of i){const i=e[r];if(!i)continue;const a=q(V(t,r),i,n,s,o);a&&c.push(...a)}return c.length?c:t.next(n,s,o)}function F(t,e){let n=t;for(;n.parent&&n.parent!==e;)n=n.parent;return n.parent===e}function U(t,e){for(const n of t)if(F(n,e))return!0;return!1}function G(t,e){const n=t instanceof Set?t.size:Array.isArray(t)?t.length:void 0,s=e instanceof Set?e.size:Array.isArray(e)?e.length:void 0;void 0!==n&&void 0!==s&&s<n&&([t,e]=[e,t]);const o=t instanceof Set?t:new Set(t);for(const t of e)if(o.has(t))return!0;return!1}function X(t,e,n,s,o){const i=new Set,r=new Map,c=t=>{let i=r.get(t);return i||(i=Q([t],e,n,s,o),r.set(t,i)),i};for(const e of t){let t=!1;const n=new Set;for(const s of i)if(G(c(e),c(s))){if(!F(e.source,s.source)){t=!0;break}n.add(s)}if(!t){for(const t of n)i.delete(t);i.add(e)}}return Array.from(i)}function Z(t,e,n,s){const o=e.historyValue,{targets:i}=nt(t,e,n,s);if(!i)return[];const r=new Set;for(const t of i)if(K(t))if(o[t.id])for(const e of o[t.id])r.add(e);else for(const o of Z(z(t),e,n,s))r.add(o);else r.add(t);return[...r]}function H(t,e,n,s){const o=Z(t,e,n,s),{reenter:i}=nt(t,e,n,s);if(!i&&o.every(e=>e===t.source||F(e,t.source)))return t.source;const[r,...c]=o.concat(t.source);for(const t of O(r,void 0))if(c.every(e=>F(e,t)))return t;return i?void 0:t.source.machine.root}function Q(t,e,n,s,o){const i=new Set;for(const r of t){const{targets:t}=nt(r,n,s,o);if(t?.length){const t=H(r,n,s,o);r.reenter&&r.source===t&&i.add(t);for(const n of e)F(n,t)&&i.add(n)}}return[...i]}function Y(t,e,n,s,o){return et([{target:[...W(t)],source:t,reenter:!0,eventType:null,toJSON:null}],e,n,s,!0,o)}function tt(t,e){return{...t,...e}}function et(t,e,n,s,o,i){const c=[];if(!t.length)return[e,c];{const a=new Set(e._nodes);let u=e.historyValue;const h=e.context,d=X(t,a,e,s,n),l=t=>nt(t,e,s,n),p=(t,o,i,r)=>{if(2===t.length){const c=[],a=[];let u;const f=b(n,c,a,!0),h=t({context:o,event:s,parent:n.self._parent,self:n.self,children:i,system:n.system,actions:e.machine.implementations.actions,actorSources:e.machine.implementations.actorSources,guards:e.machine.implementations.guards,delays:e.machine.implementations.delays,input:r},f);return void 0!==h?.context&&(u=tt(o,h.context)),[c,u,a]}return[[Object.assign((e,n)=>t({...e,input:r},n),"_special"in t?{_special:!0}:{})],void 0,void 0]};let y=e;const g=()=>{const t=Q(d,a,e,s,n);let o;t.sort((t,e)=>e.order-t.order);const r=[...a];for(const e of t)for(const t of Object.values(e.states)){if("history"!==t.type)continue;const n="deep"===t.history?t=>T(t)&&F(t,e):t=>t.parent===e;o??={...u},o[t.id]=r.filter(n)}for(const o of t){const t=e._stateInputs?.[o.id],[r,u,f]=o.exit?p(o.exit,y.context,e.children,t):[[]];f?.length&&i.push(...f),u&&(y=bt(y,{context:u}));const[h,d]=w(y,s,n,r);y=h,c.push(...d);for(const t of o.invoke){const e=y.children[t.id];e&&!e._isExternal&&n.stopChild(e),delete y.children[t.id]}a.delete(o)}u=o||u};o||g();let m=y.context;const v=[],_=[];for(const t of d){t.actions&&v.push(...Tt(t.actions));const e=l(t);void 0!==e.context&&(m=tt(m,e.context)),e.actions&&v.push(...e.actions),e.internalEvents&&_.push(...e.internalEvents)}_.length&&i.push(..._);const x=()=>{const t=t=>{const e=y.machine.root;if(void 0===e.output)return;let o;if(void 0!==t.output&&t.parent)o=At(t.output,y.context,s,n.self);else if("parallel"===t.type){const e=`xstate.done.state.${t.id}`,n=i.find(t=>t.type===e);o=n?.output}return At(e.output,y.context,r(t.id,o),n.self)},h=new Set,g=new Set,m=(t,e)=>{for(const n of t)if(e&&!F(n,e)||h.add(n),"parallel"===n.type)for(const t of A(n))U(h,t)||(h.add(t),v(t))},v=t=>{if(K(t))if(u[t.id]){const e=u[t.id];for(const t of e)h.add(t),v(t);for(const n of e)m(O(n,t.parent),void 0)}else{const e=z(t),{targets:n}=l(e);for(const s of n??[])h.add(s),e===t.parent?.initial&&g.add(t.parent),v(s);for(const e of n??[])m(O(e,t.parent),void 0)}else{if("compound"===t.type){const[e]=l(t.initial).targets;return K(e)||(h.add(e),g.add(e)),v(e),void m(O(e,t),void 0)}if("parallel"===t.type)for(const e of A(t))U(h,e)||(h.add(e),g.add(e),v(e))}};for(const t of d){const o=H(t,e,s,n),{targets:i,reenter:r}=l(t);for(const e of i??[])K(e)||t.source===e&&t.source===o&&!r||(h.add(e),g.add(e)),v(e);const c=Z(t,e,s,n);for(const e of c){const n=O(e,o);"parallel"===o?.type&&n.push(o),m(n,!t.source.parent&&r?void 0:o)}}o&&g.add(e.machine.root);const _={...e._stateInputs};for(const t of d){const{targets:e,input:n}=l(t);if(n&&e)for(const t of e)_[t.id]=n}const x=new Set,b={...e.children};for(const o of[...h].sort((t,e)=>t.order-e.order)){a.add(o);const u=[];let h=!1;for(const t of o.invoke){h=!0;let o=t.logic;"function"==typeof o&&(o=o({actorSources:e.machine.implementations.actorSources,context:y.context,event:s,self:n.self}));const i="string"==typeof o?Rt(e.machine,o):o;if(!i)throw new Error(`Actor logic '${"string"==typeof o?o:"inline"}' not implemented in machine '${e.machine.id}'`);const r="function"==typeof t.input?t.input({self:n.self,context:y.context,event:s,output:Ot(s)}):t.input,c=te(i,{...t,input:r,parent:n.self,syncSnapshot:!!t.onSnapshot});u.push({action:f["@xstate.start"],args:[c]}),t.id&&(b[t.id]=c)}h&&(y=bt(y,{children:b}));let d=y.context,l=!1;const m=_[o.id];if(o.entry){const[t,e,n]=p(o.entry,d,b,m);u.push(...t),n?.length&&i.push(...n),e&&(d=e,l=!0)}if(l&&(y.context=d),g.has(o)){const{actions:t,input:e}=nt(o.initial,y,s,n);if(t&&u.push(...t),e&&o.initial?.target)for(const t of o.initial.target)_[t.id]=e}const[v,S]=w(y,s,n,u);if(y=v,u.length=0,c.push(...S),"final"!==o.type)continue;const E=o.parent;let k="parallel"===E?.type?E:E?.parent,$=k||o;for("compound"===E?.type&&i.push(r(E.id,void 0!==o.output?At(o.output,y.context,s,n.self):void 0));"parallel"===k?.type&&!x.has(k)&&D(a,k);){x.add(k);const t={};for(const e of A(k)){if("final"===e.type){t[e.key]=void 0!==e.output?At(e.output,y.context,s,n.self):void 0;continue}if("parallel"===e.type){const n=`xstate.done.state.${e.id}`,s=i.find(t=>t.type===n);t[e.key]=s?.output;continue}const o=A(e).find(t=>"final"===t.type&&a.has(t));t[e.key]=void 0!==o?.output?At(o.output,y.context,s,n.self):void 0}i.push(r(k.id,t)),$=k,k=k.parent}k||(y=bt(y,{status:"done",output:t($)}))}JSON.stringify(_)!==JSON.stringify(e._stateInputs||{})&&(y=bt(y,{_stateInputs:_}))},[S,E]=w(y,s,n,v);y=S,c.push(...E),m&&m!==e.context&&(y=bt(y,{context:m})),x();const k=[...a];if("done"===y.status){const t=[];k.sort((t,e)=>e.order-t.order).forEach(e=>{if(e.exit){const n=y._stateInputs?.[e.id],[s,,o]=p(e.exit,y.context,y.children,n);t.push(...s),o?.length&&i.push(...o)}});const[e,o]=w(y,s,n,t);y=e,c.push(...o)}return u===e.historyValue&&e._nodes.length===a.size&&e._nodes.every(t=>a.has(t))?y.context!==h?[bt(y),c]:[y,c]:[bt(y,{_nodes:k,historyValue:u}),c]}}function nt(t,e,n,s){if(t.to){const o=[],i=[],r=b(s,o,i),c=t.to({context:e.context,event:n,output:Ot(n),value:e.value,children:e.children,system:s.system,parent:s.self._parent,self:s.self,actions:e.machine.implementations.actions,actorSources:e.machine.implementations.actorSources,guards:e.machine.implementations.guards,delays:e.machine.implementations.delays},r),a=c?.target?L(t.source,Tt(c.target)):void 0,u="function"==typeof t.input?t.input({context:e.context,event:n,output:Ot(n)}):t.input;return{targets:a,context:c?.context,reenter:c?.reenter,actions:o,internalEvents:i,input:u}}const o="function"==typeof t.input?t.input({context:e.context,event:n,output:Ot(n)}):t.input;return{targets:t.target,context:t.context,reenter:t.reenter,actions:void 0,internalEvents:void 0,input:o}}function st(t,e,o,i){let r=t;const c=[];function a(t,e){const n=o.self._collectedMicrosteps||[];n.push(...e),o.self._collectedMicrosteps=n,c.push(t)}if(e.type===s)return r=bt(rt(r,o),{status:"stopped"}),a([r,[]],[]),{snapshot:r,microsteps:c};let u=e;if(u.type!==n){const e=u,n=function(t){return t.type.startsWith("xstate.error.actor")}(e),s=r.machine.getTransitionData(r,e,o.self);if(n&&!s.length)return r=bt(t,{status:"error",error:e.error}),a([r,[]],[]),{snapshot:r,microsteps:c};const f=et(s,t,o,u,!1,i);r=f[0],a(f,s)}let f=!0;const h=t.machine.options?.maxIterations??1/0;let d=0,l=0;for(;"active"===r.status;){if(l++,l>1e3)throw new Error("Microstep count exceeded 1000");if(d++,d>h)throw new Error(`Infinite loop detected (>${h} microsteps)`);let t=f?ct(r,u,o):[];const e=t.length?r:void 0;if(!t.length){if(!i.length)break;u=i.shift(),t=r.machine.getTransitionData(r,u,o.self)}const n=et(t,r,o,u,!1,i);r=n[0],f=r!==e,a(n,t)}return"active"!==r.status&&r.children&&rt(r,o),{snapshot:r,microsteps:c}}function ot(t,e,n,s,o){return!!t.to&&it(t.to,e,n,s,o,s.machine.implementations)}function it(t,e,n,s,o,i){let r,c=!1;try{const a=()=>{throw c=!0,new Error("Effect triggered")};r=t({context:e,event:n,output:Ot(n),self:o,system:o.system,value:s.value,children:s.children,parent:{send:a},actions:i.actions,actorSources:i.actorSources,guards:i.guards,delays:i.delays},function(t,e){const n=(t,...n)=>{e(t,...n)};return Object.assign(n,{cancel:()=>{},emit:()=>{},log:()=>{},raise:()=>{},spawn:()=>({}),sendTo:()=>{},stop:()=>{},listen:()=>({}),subscribeTo:()=>({}),...t}),n}({emit:a,cancel:a,log:a,raise:a,spawn:a,sendTo:a,stop:a},a))}catch(t){if(c)return!0;throw t}return void 0!==r}function rt(t,e){let n;if(!t.children||0===(n=Object.values(t.children).filter(Boolean)).length)return t;for(const t of n)e.stopChild(t);return bt(t,{children:{}})}function ct(t,e,n){const s=new Set,o=t._nodes.filter(T);for(const i of o)t:for(let o=i;o;o=o.parent)if(o.always)for(const i of o.always)if(at(i,e,t,o,n.self)){s.add(i);break t}return X(Array.from(s),new Set(t._nodes),t,e,n)}function at(t,e,n,s,o){if(t.guard){const i={context:n.context,event:e,output:Ot(e),self:o,parent:o._parent,children:n.children,actions:s.machine.implementations.actions,actorSources:s.machine.implementations.actorSources,guards:s.machine.implementations.guards,delays:s.machine.implementations.delays,_snapshot:n};if(!t.guard(i))return!1}return!t.to||it(t.to,n.context,e,n,o,s.machine.implementations)}function ut(t){return!!t&&"object"==typeof t&&"machine"in t&&"value"in t}let ft,ht;function dt(){return ft??=ue()}const lt=function(t){return Et(t,this.value)},pt=function(t){return this.tags.has(t)},yt=function(t){const e=dt(),n=function(){if(ht)return ht;const t=dt();return ht={self:t,logger:()=>{},id:"",sessionId:"",defer:()=>{},system:t.system,stopChild:()=>{},emit:()=>{},actionExecutor:()=>{}},ht}(),s=this.machine.getTransitionData(this,t,e);if(!s?.length)return!1;for(const o of s){if(void 0!==o.target)return!0;const s=nt(o,this,t,n);if(s.targets?.length||s.context||ot(o,this.context,t,this,e))return!0}return!1},gt=function(){const{_nodes:t,_stateInputs:e,tags:n,machine:s,getMeta:o,getInputs:i,toJSON:r,can:c,hasTag:a,matches:u,...f}=this;return{...f,tags:Array.from(n)}},mt=function(){const t={};for(const e of this._nodes)void 0!==e.meta&&(t[e.id]=e.meta);return t},vt=function(){return this._stateInputs};function _t(t){const e=new Set;for(const n of t)for(const t of n.tags)e.add(t);return e}function xt(t,e){return{status:t.status,output:t.output,error:t.error,machine:e,context:t.context,_nodes:t._nodes,value:t.value??N(e.root,t._nodes),tags:_t(t._nodes),children:t.children,historyValue:t.historyValue||{},_stateInputs:t._stateInputs||{},matches:lt,hasTag:pt,can:yt,getMeta:mt,getInputs:vt,toJSON:gt}}function bt(t,e={}){const n={...t,...e};return(e._nodes??t._nodes)===t._nodes?{status:n.status,output:n.output,error:n.error,machine:t.machine,context:n.context,_nodes:t._nodes,value:t.value,tags:t.tags,children:n.children,historyValue:n.historyValue||{},_stateInputs:n._stateInputs||{},matches:lt,hasTag:pt,can:yt,getMeta:mt,getInputs:vt,toJSON:gt}:xt({...n,value:void 0},t.machine)}function St(t){const e={};for(const n in t){const s=t[n];Array.isArray(s)&&(e[n]=s.map(t=>({id:t.id})))}return e}function wt(t){let e;for(const n in t){const s=t[n];if(s&&"object"==typeof s)if("sessionId"in s&&"send"in s&&"ref"in s)e??=Array.isArray(t)?t.slice():{...t},e[n]={xstate$$type:Ut,id:s.id};else{const o=wt(s);o!==s&&(e??=Array.isArray(t)?t.slice():{...t},e[n]=o)}}return e??t}function Et(t,e){const n=$t(t),s=$t(e);return"string"==typeof s?"string"==typeof n&&s===n:"string"==typeof n?n in s:Object.keys(n).every(t=>t in s&&Et(n[t],s[t]))}function kt(t){if(Mt(t))return t;const e=[];let n="";for(let s=0;s<t.length;s++){switch(t.charCodeAt(s)){case 92:n+=t[s+1],s++;continue;case 46:e.push(n),n="";continue}n+=t[s]}return e.push(n),e}function $t(t){if(ut(t))return t.value;if("string"!=typeof t)return t;return It(kt(t))}function It(t){if(1===t.length)return t[0];const e={};let n=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)n[t[e]]=t[e+1];else{const s=n;n={},s[t[e]]=n}return e}function jt(t){return Mt(t)?t:[t]}function Tt(t){return void 0===t?[]:jt(t)}function At(t,e,n,s){if("function"==typeof t){return t({context:e,event:n,output:Ot(n),self:s})}return t}function Ot(t){if(function(t){return t.type.startsWith("xstate.done.actor.")||t.type.startsWith("xstate.done.state.")}(t)){return t.output}}function Mt(t){return Array.isArray(t)}function Nt(t){return jt(t).map(t=>void 0===t||"string"==typeof t?{target:t}:"function"==typeof t?{to:t}:t)}function Dt(t){if(void 0!==t&&""!==t)return Tt(t)}function Pt(t,e,n){const s="object"==typeof t,o=s?t:void 0;return{next:(s?t.next:t)?.bind(o),error:(s?t.error:e)?.bind(o),complete:(s?t.complete:n)?.bind(o)}}function Rt(t,e){const n=e.match(/^xstate\.invoke\.(\d+)\.(.*)/);if(!n)return t.implementations.actorSources[e];const[,s,o]=n,i=t.getStateNodeById(o).config.invoke,r=(Array.isArray(i)?i[s]:i).src;return"string"==typeof r?t.implementations.actorSources[r]:r}function Ct(t,e){if(e===t)return!0;if("*"===e)return!0;if(!e.endsWith(".*"))return!1;const n=e.split("."),s=t.split(".");for(let t=0;t<n.length;t++){const e=n[t],o=s[t];if("*"===e){return t===n.length-1}if(e!==o)return!1}return!0}function Lt(t,e){return`${t.sessionId}.${e}`}const zt=new WeakMap,Kt="xstate.logic.effect.resolve",Wt="xstate.logic.effect.reject",Vt="xstate.logic.effect.start";function Bt(t){let e=zt.get(t);return e||(e=new Map,zt.set(t,e)),e}function Jt(t,e){if(t?.length)for(const n of t)switch(n.type){case"emit":e.emit(n.event);break;case"sendBack":{const t=e.self._parent;t&&e.system._relay(e.self,t,n.event);break}case"raise":e.system._relay(e.self,e.self,n.event);break;case"effect":{if(!n.key){const t=n.exec();"function"==typeof t&&Bt(e.self).set(Symbol(),{cleanup:t});break}const t=Bt(e.self);if(t.has(n.key))break;const s=n.exec();t.set(n.key,{cleanup:"function"==typeof s?s:void 0});break}case"cleanupEffects":{const t=zt.get(e.self);if(!t)break;for(const{cleanup:e}of t.values())e?.();zt.delete(e.self);break}}}function qt(t){const e=(e,n,o)=>{if("active"!==e.status)return[e,[]];if(n.type===s)return[{...e,status:"stopped",input:void 0},[{type:"cleanupEffects"}]];if(n.type===Vt)return[{...e,effects:{...e.effects,[n.key]:{status:"active"}}},[]];if(n.type===Kt)return[{...e,effects:{...e.effects,[n.key]:{status:"done",output:n.output}}},[]];if(n.type===Wt)return[{...e,effects:{...e.effects,[n.key]:{status:"error",error:n.error}}},[]];const i=[],r={},c={emit:t=>{i.push({type:"emit",event:t})},sendBack:t=>{i.push({type:"sendBack",event:t})},raise:t=>{i.push({type:"raise",event:t})},effect:(t,n)=>{if("string"==typeof t)return s=t,o=n,void(e.effects?.[s]||(i.push({type:"effect",key:s,exec:o}),r[s]={status:"active"}));var s,o;i.push({type:"effect",exec:t})}},a=t.run({context:e.context,event:n,input:e.input,system:o.system,self:o.self,emit:o.emit},c),u={...e,...a||{}};return("context"in e||void 0!==a?.context)&&(u.context=a?.context??e.context),(a?.effects||Object.keys(r).length)&&(u.effects={...e.effects,...a?.effects,...r}),[u,i]},n={id:t.id,config:t,transition:e,start:(t,n)=>{const[s,o]=e(t,c(t.input),n);Object.assign(t,s),Jt(o,n)},initialTransition:(e,n)=>{const s=function(t,e){return"function"==typeof t?t({input:e}):t}(t.context,e),o={status:"active",output:void 0,error:void 0,input:e};return void 0!==s&&(o.context=s),[{...o},[]]},getInitialSnapshot:(t,e)=>n.initialTransition(e,t)[0],getPersistedSnapshot:t=>t,restoreSnapshot:t=>t};return n}let Ft=!1;const Ut=1;let Gt=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const Xt={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console)};function Zt(t){return"object"==typeof t&&null!==t&&"args"in t&&"exec"in t}function Ht(t,e){if(!t?.length)return[];const n=[];for(const s of t)Zt(s)?e.actionExecutor(s):n.push(s);return n}class Qt{constructor(t,n){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this._boundProcess=this._process.bind(this),this.mailbox=new e(this._boundProcess),this.observers=new Set,this.eventListeners=new Map,this.logger=void 0,this._processingStatus=Gt.NotStarted,this._forceDeferredActions=!1,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this._lastSourceRef=void 0,this._collectedMicrosteps=[],this._collectedActions=[],this._collectedSent=[],this._initialEffects=void 0,this.registryKey=void 0,this.sessionId=void 0,this.system=void 0,this.trigger=void 0,this.src=void 0,this._deferred=[],this._pendingEffects=void 0;const s={...Xt,...n},{clock:o,logger:i,parent:r,syncSnapshot:c,id:u,registryKey:f,inspect:h}=s;this.system=r?r.system:s._systemRef?.current??function(t,e){let n=0;const s=new Map,o=new Map,i=new WeakMap,r=new Set,c={},{clock:a,logger:u}=e,f=(t,e,n,s,o)=>{r.size&&t&&(t._collectedSent??=[]).push({targetRef:e,targetId:e.id,event:n,delay:s,id:o})},h={schedule:(t,e,n,s,o=Math.random().toString(36).slice(2))=>{f(t,e,n,s,o);const i={source:t,target:e,event:n,delay:s,id:o,startedAt:Date.now()},r=Lt(t,o);l._snapshot._scheduledEvents[r]=i;const u=a.setTimeout(()=>{delete c[r],delete l._snapshot._scheduledEvents[r],d(t,e,n)},s);c[r]=u},cancel:(t,e)=>{const n=Lt(t,e),s=c[n];delete c[n],delete l._snapshot._scheduledEvents[n],void 0!==s&&a.clearTimeout(s)},cancelAll:t=>{for(const e in l._snapshot._scheduledEvents){const n=l._snapshot._scheduledEvents[e];n.source===t&&h.cancel(t,n.id)}}},d=(t,e,n)=>{const s=e.logic;if("function"==typeof s?.isInternalEventType&&s.isInternalEventType(n.type)&&t!==e)throw new Error(`Internal event "${n.type}" cannot be sent to actor "${e.id}" from outside.`);e._lastSourceRef=t,e._send(n)},l={children:s,reverseKeyedActors:i,keyedActors:o,_snapshot:{_scheduledEvents:(e?.snapshot&&e.snapshot.scheduler)??{}},_bookId:()=>"x:"+n++,_register:(t,e)=>(s.set(t,e),t),_unregister:t=>{s.delete(t.sessionId);const e=i.get(t);void 0!==e&&(o.delete(e),i.delete(t))},get:t=>o.get(t),getAll:()=>Object.fromEntries(o.entries()),_set:(t,e)=>{const n=o.get(t);if(n&&n!==e)throw new Error(`Actor with registry key '${t}' already exists.`);o.set(t,e),i.set(e,t)},inspect:t=>{const e=Pt(t);return r.add(e),{unsubscribe(){r.delete(e)}}},_sendInspectionEvent:e=>{if(!r.size)return;const n={...e,rootId:t.sessionId};r.forEach(t=>t.next?.(n))},_relay:(t,e,n)=>{f(t,e,n),d(t,e,n)},scheduler:h,getSnapshot:()=>({_scheduledEvents:{...l._snapshot._scheduledEvents}}),start:()=>{const t=l._snapshot._scheduledEvents;l._snapshot._scheduledEvents={};for(const e in t){const{source:n,target:s,event:o,delay:i,id:r}=t[e];h.schedule(n,s,o,i,r)}},_clock:a,_logger:u,_timerStrategy:e.timers};return l}(this,{clock:o,logger:i,timers:s.timers}),r||!s._systemRef||s._systemRef.current||(s._systemRef.current=this.system),h&&!r&&this.system.inspect(Pt(h)),this.sessionId=this.system._bookId(),this.id=u??this.sessionId,this.logger=n?.logger??this.system._logger,this.clock=n?.clock??this.system._clock,this._parent=r,this._syncSnapshot=c,this.options=s,this.src=s.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()},emit:t=>{const e=this.eventListeners.get(t.type),n=this.eventListeners.get("*");if(e||n){if(e)for(const n of e)try{n(t)}catch(t){a(t)}if(n)for(const e of n)try{e(t)}catch(t){a(t)}}},actionExecutor:t=>{const e=()=>{if(this._collectedActions.push({type:t.type,params:t.params}),!t.exec)return;const e=Ft;try{Ft=!0,t.exec()}finally{Ft=e}};this._processingStatus!==Gt.Running||this._forceDeferredActions?this._deferred.push(e):e()}},this.send=this.send.bind(this),this.trigger=new Proxy({},{get:(t,e)=>t=>{this.send({...t,type:e})}});const d=f;d&&(this.registryKey=d,this.system._set(d,this)),this._collectedMicrosteps=[];let l=n?.snapshot??n?.state;if(l&&"object"==typeof l&&"_pendingEffects"in l){const{_pendingEffects:t,...e}=l;this._pendingEffects=t,l=e}try{if(l)this._snapshot=this.logic.restoreSnapshot?this.logic.restoreSnapshot(l,this._actorScope):l;else{const[t,e]=this.logic.initialTransition(this.options?.input,this._actorScope);this._snapshot=t,this._initialEffects=e}}catch(t){this._snapshot={status:"error",output:void 0,error:t},this._deferred.length=0}d&&"active"!==this._snapshot.status&&this.system._unregister(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this,parentRef:this._parent,id:this.id,src:this.src,snapshot:this._snapshot})}_setErrorSnapshot(t,e=this._snapshot){this._snapshot={...e,status:"error",error:t}}_next(t){for(const e of this.observers)try{e.next?.(t)}catch(t){a(t)}}update(t,e){this._snapshot=t;for(let e=0;e<this._deferred.length;e++){const n=this._deferred[e];try{n()}catch(e){this._deferred.length=0,this._setErrorSnapshot(e,t);break}}switch(this._deferred.length=0,this._snapshot.status){case"active":this._next(t);break;case"done":{this._next(t),this._stopProcedure(),this._complete();const e=(n=this.id,s=this._snapshot.output,{type:`xstate.done.actor.${n}`,output:s,actorId:n});this._parent&&this.system._relay(this,this._parent,e);break}case"error":this._error(this._snapshot.error)}var n,s;this.system._sendInspectionEvent({type:"@xstate.transition",actorRef:this,event:e,sourceRef:this._lastSourceRef,targetRef:this,snapshot:t,microsteps:this._collectedMicrosteps,actions:this._collectedActions,sent:this._collectedSent,eventType:e.type}),this._collectedMicrosteps=[],this._collectedActions=[],this._collectedSent=[]}_flushInitialEffects(){if(!this._initialEffects)return!0;this._forceDeferredActions=!0;try{return Jt(Ht(this._initialEffects,this._actorScope),this._actorScope),this._initialEffects=void 0,!0}catch(t){return this._initialEffects=void 0,this._deferred.length=0,this._setErrorSnapshot(t),this._error(t),!1}finally{this._forceDeferredActions=!1}}subscribe(t,e,n){const s=Pt(t,e,n);if(this._processingStatus!==Gt.Stopped)this.observers.add(s);else switch(this._snapshot.status){case"done":try{s.complete?.()}catch(t){a(t)}break;case"error":{const t=this._snapshot.error;if(s.error)try{s.error(t)}catch(t){a(t)}else a(t);break}}return{unsubscribe:()=>{this.observers.delete(s)}}}on(t,e){let n=this.eventListeners.get(t);return n||(n=new Set,this.eventListeners.set(t,n)),n.add(e),{unsubscribe:()=>{n.delete(e)}}}select(t,e=Object.is){return{subscribe:(n,s,o)=>{const i=Pt(n,s,o);let r=t(this.getSnapshot());return this.subscribe({next:n=>{const s=t(n);e(r,s)||(r=s,i.next?.(s))},error:i.error,complete:i.complete})},get:()=>t(this.getSnapshot())}}start(){if(this._processingStatus===Gt.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this.registryKey&&this.system._set(this.registryKey,this),this._processingStatus=Gt.Running;const t=c(this.options.input);this._lastSourceRef=this._parent;switch(this._snapshot.status){case"done":return this._flushInitialEffects()?(this.update(this._snapshot,t),this):this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._setErrorSnapshot(t),this._error(t),this}if(!this._flushInitialEffects())return this;if(this.update(this._snapshot,t),this._pendingEffects){const t=this.options.timers??this.system._timerStrategy??"resume";for(const e of this._pendingEffects)"@xstate.raise"===e.type&&this.system.scheduler.schedule(this,this,e.event,Yt(t,e),e.id);this._pendingEffects=void 0}return this.mailbox.start(),this}_process(t){let e,n;try{e=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){n={err:t}}if(n){const{err:t}=n;return this._setErrorSnapshot(t),void this._error(t)}try{const[n,s]=e,o=Ht(s,this._actorScope);this.update(n,t),Jt(o,this._actorScope)}catch(t){return this._setErrorSnapshot(t),void this._error(t)}t.type===s&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===Gt.Stopped?this:(this.mailbox.clear(),this._processingStatus===Gt.NotStarted?(this._processingStatus=Gt.Stopped,this):(this.mailbox.enqueue({type:s}),this.system._unregister(this),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){a(t)}this.observers.clear(),this.eventListeners.clear()}_error(t){if(this._stopProcedure(),this.observers.size){let e=!1;for(const n of this.observers){const s=n.error;e||=!s;try{s?.(t)}catch(t){a(t)}}this.observers.clear(),this.eventListeners.clear(),e&&a(t)}else this._parent||a(t),this.eventListeners.clear();var e;this._parent&&this.system._relay(this,this._parent,{type:`xstate.error.actor.${e=this.id}`,error:t,actorId:e})}_stopProcedure(){this._processingStatus===Gt.Running&&(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new e(this._boundProcess),this._processingStatus=Gt.Stopped,this.system._unregister(this))}_send(t){this._processingStatus!==Gt.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}toJSON(){return{xstate$$type:Ut,id:this.id}}getPersistedSnapshot(t){const e=this.logic.getPersistedSnapshot(this._snapshot,t),n=this.system._snapshot._scheduledEvents;let s;const o=Date.now();for(const t in n){const e=n[t];e.source===this&&e.target===this&&(s??=[]).push({type:"@xstate.raise",event:e.event,id:e.id,delay:e.delay,startedAt:e.startedAt,elapsed:Math.max(0,o-e.startedAt)})}return s??=this._pendingEffects,s?{...e,_pendingEffects:s}:e}[u](){return this}getSnapshot(){return this._snapshot}}function Yt(t,e){if("function"==typeof t)return t(e);switch(t){case"restart":return e.delay;case"absolute":return Math.max(0,e.startedAt+e.delay-Date.now());default:return Math.max(0,e.delay-e.elapsed)}}function te(t,e){return new Qt(t,e)}const ee=new WeakMap;const ne="xstate.observable.next",se="xstate.observable.error",oe="xstate.observable.complete";const ie="xstate.async.resolve",re="xstate.async.reject";class ce extends Error{constructor(t){super(`Async logic timed out after ${t}.`),this.name="TimeoutError"}}const ae=qt({context:void 0,run:()=>{}});function ue(){return te(ae)}function fe(t,{machine:e,context:n},s,o){return(i,r)=>{const c=((i,r)=>{if("string"==typeof i){const c=Rt(e,i);if(!c)throw new Error(`Actor logic '${i}' not implemented in machine '${e.id}'`);const a=te(c,{id:r?.id,parent:t.self,syncSnapshot:r?.syncSnapshot,input:"function"==typeof r?.input?r.input({context:n,event:s,self:t.self}):r?.input,src:i,registryKey:r?.registryKey});return o[a.id]=a,a}return te(i,{id:r?.id,parent:t.self,syncSnapshot:r?.syncSnapshot,input:r?.input,src:i,registryKey:r?.registryKey})})(i,r);return o[c.id]=c,t.defer(()=>{c._processingStatus!==Gt.Stopped&&c.start()}),c}}const he=new WeakMap;function de(t,e,n){let s=he.get(t);return s?e in s||(s[e]=n()):(s={[e]:n()},he.set(t,s)),s[e]}const le={},pe=["invoke","after","on","entry","exit","always","states","initial","onDone","timeout","onTimeout","history","target","output"];class ye{constructor(t,e){this.config=t,this.key=void 0,this.id=void 0,this.type=void 0,this.path=void 0,this.states=void 0,this.history=void 0,this.entry=void 0,this.exit=void 0,this.parent=void 0,this.machine=void 0,this.meta=void 0,this.output=void 0,this.order=-1,this.description=void 0,this.tags=[],this.transitions=void 0,this.always=void 0,this.invoke=void 0,this.on=void 0,this.after=void 0,this.events=void 0,this.ownEvents=void 0,this.parent=e._parent,this.key=e._key,this.machine=e._machine,this.path=this.parent?this.parent.path.concat(this.key):[];const n=this.config.states?Object.keys(this.config.states)[0]:void 0;if(this.id=this.config.id||[this.machine.id,...this.path].join("."),this.type=this.config.type||(void 0!==n?"compound":this.config.history?"history":"atomic"),this.description=this.config.description,function(t){const e=t.config;if("choice"!==t.type){if(void 0!==e.choice)throw new Error(`State "${t.id}" has \`choice\`, but \`choice\` can only be used with \`type: 'choice'\`.`);return}if("function"!=typeof e.choice)throw new Error(`Choice state "${t.id}" must declare a \`choice\` function.`);for(const n of pe)if(void 0!==e[n])throw new Error(`Choice state "${t.id}" cannot declare \`${n}\`.`)}(this),this.order=this.machine.idMap.size,this.machine.idMap.set(this.id,this),this.states=this.config.states?function(t,e){const n={},s=Object.keys(t);for(let o=0;o<s.length;o++){const i=s[o];n[i]=e(t[i],i,t,o)}return n}(this.config.states,(t,e)=>new ye(t,{_parent:this,_key:e,_machine:this.machine})):le,"compound"===this.type&&!this.config.initial)throw new Error(`No initial state specified for compound state node "#${this.id}".`);this.history=!0===this.config.history?"shallow":this.config.history||!1,this.entry=this.config.entry,this.exit=this.config.exit,this.entry&&(this.entry._special=!0),this.exit&&(this.exit._special=!0),this.meta=this.config.meta,this.output="final"!==this.type&&this.parent?void 0:this.config.output,this.tags=Tt(t.tags).slice(),this.invoke=Tt(this.config.invoke).map((t,e)=>{const{src:n,registryKey:s}=t,o=(i=this.id,`${e}.${i}`);var i;const r=t.id??o,c="string"==typeof n?n:`xstate.invoke.${o}`;return{...t,src:c,logic:n,id:r,registryKey:s}})}_initialize(){this.after=function(t){const e=t.config.after,n=t.config.timeout,s=t.config.onTimeout,r=t.invoke.filter(t=>void 0!==t.timeout);if(!e&&void 0===n&&0===r.length)return[];const c=[];if(e)for(const n of Object.keys(e)){const s=Number.isNaN(+n)?n:+n;c.push({event:o(s,t.id),delay:s,transitions:e[n],fromDelaysMap:!0})}var a;void 0!==n&&s&&c.push({event:(a=t.id,{type:`xstate.timeout.${a}`}),delay:n,transitions:s,fromDelaysMap:!0});for(const t of r)c.push({event:i(t.id),delay:t.timeout,transitions:t.onTimeout,fromDelaysMap:!1});const u=[];for(const{event:e,delay:n,transitions:s,fromDelaysMap:o}of c){const i=R(t,e,e=>j(n,o?e.delays:{},{context:e.context,event:e.event,stateNode:t}));for(const t of Nt(s))u.push({...t,event:i,delay:n})}return u.map(e=>({...C(t,e.event,e),delay:e.delay}))}(this),this.transitions=function(t){const e=new Map;if(t.config.on)for(const n of Object.keys(t.config.on)){if(""===n)throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.');const s=t.config.on[n];e.set(n,ge(s,e=>C(t,n,e)))}if(t.config.onDone){const n=`xstate.done.state.${t.id}`;e.set(n,ge(t.config.onDone,e=>C(t,n,e)))}const n=(e,n)=>C(t,e,{to:(t,e)=>(e.cancel(n),{})}),s=(e,n,s)=>{const{target:o,to:i,reenter:r,...c}=n;return C(t,e,{...c,reenter:r,to:(t,e)=>{if(i){let n=!1;const o=new Proxy(e,{apply:(t,e,s)=>(n=!0,Reflect.apply(t,e,s)),get(t,e,s){const o=Reflect.get(t,e,s);return"function"!=typeof o?o:(...e)=>(n=!0,o.apply(t,e))}}),r=i(t,o);return(void 0!==r||n)&&e.cancel(s),r}return e.cancel(s),{target:o,reenter:r}}})};for(const o of t.invoke){const r=void 0!==o.timeout?i(o.id).type:void 0;if(o.onDone){const i=`xstate.done.actor.${o.id}`,c=ge(o.onDone,e=>r?s(i,e,r):C(t,i,e));r&&c.push(n(i,r)),e.set(i,c)}else if(r){const t=`xstate.done.actor.${o.id}`;e.set(t,[n(t,r)])}if(o.onError){const n=`xstate.error.actor.${o.id}`;e.set(n,ge(o.onError,e=>r?s(n,e,r):C(t,n,e)))}if(o.onSnapshot){const n=`xstate.snapshot.${o.id}`;e.set(n,ge(o.onSnapshot,e=>C(t,n,e)))}}for(const n of t.after){let t=e.get(n.eventType);t||(t=[],e.set(n.eventType,t)),t.push(n)}return e}(this),"choice"===this.type?this.always=function(t){const e=t.config.choice,n=e=>{if(!e||void 0===e.target)throw new Error(`Choice state "${t.id}" must resolve to a target.`);for(const n of["actions","to","context"])if(void 0!==e[n])throw new Error(`Choice state "${t.id}" cannot declare \`${n}\` on a choice.`);return e};return[C(t,"",{to:t=>n(e(t))})]}(this):this.config.always&&(this.always=ge(this.config.always,t=>C(this,"",t)));for(const t of Object.keys(this.states))this.states[t]._initialize();this._refreshEventMetadata()}_refreshEventMetadata(){const t={},e=[];for(const[n,s]of this.transitions)t[n]=s.slice(),s.some(t=>t.target||t.reenter||t.to)&&e.push(n);this.on=t,this.ownEvents=e;const n=new Set(e);for(const t of Object.values(this.states))for(const e of t.events)n.add(e);this.events=Array.from(n)}get initial(){return de(this,"initial",()=>function(t,e){const n="object"==typeof e&&null!==e?e.target:e,s="object"==typeof e&&null!==e?e.input:void 0,o="string"==typeof n?t.states[n]:void 0;if(!o&&n)throw new Error(`Initial state node "${n}" not found on parent state node #${t.id}`);const i={source:t,target:o?[o]:void 0,input:s};return i}(this,this.config.initial))}next(t,e,n){const s=e.type,o=de(this,`candidates-${s}`,()=>function(t,e){const n=t.transitions.get(e),s=[...t.transitions.keys()].filter(t=>t!==e&&Ct(e,t)).sort((t,e)=>e.length-t.length).flatMap(e=>t.transitions.get(e));return n?[...n,...s]:s}(this,s));for(const s of o){if(at(s,e,t,this,n))return[s]}}}function ge(t,e){const n=Nt(t),s=new Array(n.length);for(let t=0;t<n.length;t++)s[t]=e(n[t]);return s}class me{constructor(t,e){this.config=t,this.version=void 0,this.schemas=void 0,this.implementations=void 0,this.options=void 0,this.idMap=new Map,this.root=void 0,this.id=void 0,this.states=void 0,this.events=void 0,this.internalEventDescriptors=void 0,this._json=void 0,this.id=t.id||"(machine)",this.implementations={actorSources:t.actorSources??{},actions:t.actions??{},delays:t.delays??{},guards:t.guards??{},...e},this.version=this.config.version,this.schemas=this.config.schemas,this.internalEventDescriptors=this.config.internalEvents??[],this.options={maxIterations:1/0,...this.config.options},this.transition=this.transition.bind(this),this.initialTransition=this.initialTransition.bind(this),this.getInitialSnapshot=this.getInitialSnapshot.bind(this),this.getPersistedSnapshot=this.getPersistedSnapshot.bind(this),this.restoreSnapshot=this.restoreSnapshot.bind(this),this.start=this.start.bind(this),this.root=new ye(t,{_key:this.id,_machine:this}),this.root._initialize(),function(t){const e=[],n=s=>{Object.values(s).forEach(s=>{if(s.config.route&&s.config.id){const o=s.config.id,i=s.config.route,r=({event:t})=>t.to===`#${o}`;if("function"==typeof i)return e.push(C(t,"xstate.route",{guard:r,to:t=>{const e=i(t);if(e)return{...!0===e?{}:e,target:`#${o}`}}})),void(s.states&&n(s.states));if("$unserializable"in i)throw new Error(`State "${s.id}" has a route that is not serializable. Re-provide the route function when reviving this machine.`);const{guard:c,...a}=i,u={...a,guard:r,target:`#${o}`};e.push(C(t,"xstate.route",u))}s.states&&n(s.states)})};n(t.states),e.length>0&&t.transitions.set("xstate.route",e)}(this.root),this.root._refreshEventMetadata(),this.states=this.root.states,this.events=this.root.events}provide(t){const{actions:e,guards:n,actorSources:s,delays:o}=this.implementations,i=new me(this.config,{actions:{...e,...t.actions},guards:{...n,...t.guards},actorSources:{...s,...t.actorSources},delays:{...o,...t.delays}});return i._json=this._json,i}resolveState(t){const e=(n=this.root,s=t.value,N(n,M(J(n,s))));var n,s;const o=M(J(this.root,e));return xt({_nodes:[...o],value:e,context:t.context||{},children:{},status:D(o,this.root)?"done":t.status||"active",output:t.output,error:t.error,historyValue:t.historyValue},this)}transition(t,e,n){const{snapshot:s,microsteps:o}=st(t,e,n,[]);return[s,o.flatMap(([,t])=>t)]}microstep(t,e,n){const{microsteps:s}=st(t,e,n,[]),o=new Array(s.length);for(let t=0;t<s.length;t++)o[t]=s[t][0];return o}getTransitionData(t,e,n){return q(this.root,t.value,t,e,n)||[]}isInternalEventType(t){for(const e of this.internalEventDescriptors)if(Ct(t,e))return!0;return!1}_getPreInitialState(t,e){const{context:n}=this.config,s=xt({context:"function"!=typeof n&&n?n:{},_nodes:[this.root],children:{},status:"active"},this);if("function"==typeof n){const o={},i=n({spawn:fe(t,s,e,o),input:e.input,self:t.self,actorSources:this.implementations.actorSources}),[r]=w(s,e,t,[]);return i&&(r.context=i),Object.keys(o).length>0&&(r.children={...r.children,...o}),r}return s}getInitialSnapshot(t,e){return this.initialTransition(e,t)[0]}initialTransition(t,e){const n=c(t),s=[],o=this._getPreInitialState(e,n),[i,r]=Y(this.root,o,e,n,s),{snapshot:a,microsteps:u}=st(i,n,e,s);return[a,[...r,...u.flatMap(([,t])=>t)]]}start(t){if(t?.children)for(const e of Object.values(t.children))e._rehydrated&&"active"===e.getSnapshot?.().status&&e.start()}getStateNodeById(t){const e=kt(t),n=e.slice(1),s=P(e[0])?e[0].slice(1):e[0],o=this.idMap.get(s);if(!o)throw new Error(`Child state node '#${s}' does not exist on machine '${this.id}'`);return B(o,n)}getPersistedSnapshot(t,e){return function(t,e){const{_nodes:n,_stateInputs:s,tags:o,machine:i,children:r,context:c,can:a,hasTag:u,matches:f,getMeta:h,getInputs:d,toJSON:l,...p}=t,y={};for(const t in r){const n=r[t];y[t]={snapshot:n.getPersistedSnapshot(e),src:n.src,registryKey:n.registryKey,syncSnapshot:n._syncSnapshot}}const g={...p,context:wt(c),children:y,historyValue:St(p.historyValue)};return void 0!==i.version&&(g.version=i.version),g}(t,e)}restoreSnapshot(t,e){const n=t.version;if(n!==this.version){const e=this.config.migrate;if("function"!=typeof e)throw new Error(`Persisted snapshot version '${n}' does not match machine version '${this.version}'.`);t=e(t,n)}const s=t,o={},i=s.children;for(const t of Object.keys(i)){const n=i[t],s=n.snapshot,r=n.src,c="string"==typeof r?Rt(this,r):r;if(!c)continue;const a=te(c,{id:t,parent:e.self,syncSnapshot:n.syncSnapshot,snapshot:s,src:r,registryKey:n.registryKey});a._rehydrated=!0,o[t]=a}const r=(t=>{if(!t||"object"!=typeof t)return{};const e={};for(const n of Object.keys(t)){const s=t[n];for(const t of s){let s;if(t instanceof ye)s=t;else try{s=this.root.machine.getStateNodeById(t.id)}catch{}s&&(e[n]??=[],e[n].push(s))}}return e})(s.historyValue),c=Array.from(M(J(this.root,s.value))),{version:a,...u}=t,f=xt({...u,children:o,_nodes:c,value:s.value,historyValue:r},this),h=new WeakSet;return function t(e){if(!h.has(e)){h.add(e);for(const n of Object.keys(e)){const s=e[n];if(s&&"object"==typeof s){if("xstate$$type"in s&&s.xstate$$type===Ut){e[n]=o[s.id];continue}t(s)}}}}(f.context),f}}function ve(t){return new me(t)}function _e(t){const e=I(t);return void 0!==e?e:"string"==typeof t?parseFloat(t)||0:t}function xe(t){return!!t&&"object"==typeof t&&"$unserializable"in t&&"string"==typeof t.$unserializable}function be(t,e="$"){if(xe(t)){if("actor"===t.$unserializable&&e.endsWith(".src"))return;throw new Error(`Unresolved ${t.$unserializable} at ${e}`)}if(t&&"object"==typeof t)if(Array.isArray(t))t.forEach((t,n)=>be(t,`${e}[${n}]`));else for(const n of Object.keys(t)){const s=t[n];"$"===e&&("actions"===n||"guards"===n||"actorSources"===n||"delays"===n||"schemas"===n)||be(s,`${e}.${n}`)}}function Se(t){const e={};if(!t)return e;for(const n of Object.keys(t)){const s=t[n];"number"==typeof s&&(e[n]=s)}return e}let we="session_"+Math.random().toString(36).slice(2),Ee="";function ke(t,e,n,s){const o=`\nwith (context) {\n return (${e});\n}\n `.trim(),i=new Function("context","_event","_sessionid","_name","_ioprocessors","In",o),r=n&&n._scxmlExternal,c=n&&"@xstate.init"===n.type,a=n&&"string"==typeof n.type&&n.type.startsWith("xstate.done."),u=n&&n._scxmlEventType;return i(t,n&&!c?{name:n.type,type:u||(r?"external":"internal"),sendid:n._scxmlSendId,origin:r?"#_scxml_session":void 0,origintype:r?"http://www.w3.org/TR/scxml/#SCXMLEventProcessor":void 0,invokeid:n._scxmlInvokeId,data:a?n.output:void 0!==n._scxmlEventData?n._scxmlEventData:n}:void 0,we,Ee,{scxml:{location:`#_scxml_${we}`}},t=>{if(!s)return!1;try{const e=s.getSnapshot?.();if(e?._nodes){const n=t.replace(/\./g,"$");return e._nodes.some(t=>t.id===n||t.id.endsWith("."+n))}}catch{}return!1})}function $e(t,e){const n=Object.keys(t),s=n.map(t=>`let ${t} = context.${t};`).join("\n"),o=n.map(t=>`updates.${t} = ${t};`).join("\n");return new Function("context","updates",`\n${s}\n${e}\n${o}\nreturn updates;\n `)(t,{})}function Ie(t){return t.type.startsWith("@xstate.")}function je(t,e){const n={$unserializable:t};return e&&(n.id=e),n}function Te(t){return{"@type":"code",lang:"ts",expr:t.toString()}}function Ae(t){if("function"==typeof t)return Te(t);if(null===t||"object"!=typeof t)return"bigint"==typeof t||"symbol"==typeof t?je("value"):t;if(function(t){return!!t&&"object"==typeof t&&"function"==typeof t.transition&&"function"==typeof t.getInitialSnapshot}(t))return je("actor",t.id);if(function(t){return!!t&&"object"==typeof t&&"~standard"in t}(t))return je("schema");if(Array.isArray(t))return t.map(Ae);if(t.constructor!==Object&&void 0!==t.constructor)return je("value");const e={};for(const n of Object.keys(t)){const s=t[n];void 0!==s&&(e[n]=Ae(s))}return e}function Oe(t){if(Array.isArray(t))return t.map(Oe);const e=t,n={};for(const t of Object.keys(e)){const s=e[t];void 0!==s&&(n[t]="src"===t&&"string"!=typeof s?je("actor",s.id):Ae(s))}return n}function Me(t,e){if(!t)return;const n={};for(const s of Object.keys(t)){const o=t[s];n[s]="schema"===e?je("schema",s):"function"==typeof o?Te(o):"actor"===e&&o&&"object"==typeof o?je("actor",s):Ae(o)}return n}function Ne(t){const e={};for(const n of Object.keys(t)){if("states"===n||"invoke"===n)continue;const s=t[n];void 0!==s&&(e[n]=Ae(s))}if(void 0!==t.invoke&&(e.invoke=Oe(t.invoke)),t.states){const n={};for(const e of Object.keys(t.states))n[e]=Ne(t.states[e]);e.states=n}return e}function De(t){const e=Ne(t);if(void 0!==t.internalEvents&&(e.internalEvents=Ae(t.internalEvents)),t.schemas){const n={};for(const e of Object.keys(t.schemas)){const s=t.schemas[e];n[e]=s&&"object"==typeof s&&!("~standard"in s)?Me(s,"schema"):je("schema",e)}e.schemas=n}for(const n of["actions","guards","actorSources","delays"])t[n]&&(e[n]=Me(t[n],"actorSources"===n?"actor":"function"));return e}function Pe(t={}){const{states:e={},schemas:n,actions:s,actorSources:o,guards:i,delays:r}=t;return{extend:e=>Pe(function(t,e){return{...t,...e,schemas:Ce(t.schemas,e.schemas),states:Re(t.states,e.states),actions:Re(t.actions,e.actions),actorSources:Re(t.actorSources,e.actorSources),guards:Re(t.guards,e.guards),delays:Re(t.delays,e.delays)}}(t,e)),createMachine(t){const e=Ce(t.schemas,n),c=Re(s,t.actions),a=Re(o,t.actorSources),u=Re(i,t.guards),f=Re(r,t.delays);return new me({...t,...e?{schemas:e}:void 0,...c?{actions:c}:void 0,...a?{actorSources:a}:void 0,...u?{guards:u}:void 0,...f?{delays:f}:void 0})},createStateConfig:t=>t,states:e}}function Re(t,e){return t||e?{...t,...e}:void 0}function Ce(t,e){if(t||e)return{...t,...e,events:Re(t?.events,e?.events),emitted:Re(t?.emitted,e?.emitted),children:Re(t?.children,e?.children)}}function Le(t){const e=te(t);return{self:e,defer:()=>{},id:"",logger:()=>{},sessionId:"",stopChild:()=>{},system:e.system,emit:()=>{},actionExecutor:()=>{}}}let ze=function(t){return t.Internal="#_internal",t}({});const Ke={timeout:1/0};t.Actor=Qt,t.SimulatedClock=class{constructor(){this.timeouts=new Map,this._now=0,this._id=0,this._flushing=!1,this._flushingInvalidated=!1}now(){return this._now}getId(){return this._id++}setTimeout(t,e){this._flushingInvalidated=this._flushing;const n=this.getId();return this.timeouts.set(n,{start:this.now(),timeout:e,fn:t}),n}clearTimeout(t){this._flushingInvalidated=this._flushing,this.timeouts.delete(t)}set(t){if(this._now>t)throw new Error("Unable to travel back in time");this._now=t,this.flushTimeouts()}flushTimeouts(){if(this._flushing)return void(this._flushingInvalidated=!0);this._flushing=!0;const t=[...this.timeouts].sort(([t,e],[n,s])=>{const o=e.start+e.timeout;return s.start+s.timeout>o?-1:1});for(const[e,n]of t){if(this._flushingInvalidated)return this._flushingInvalidated=!1,this._flushing=!1,void this.flushTimeouts();this.now()-n.start>=n.timeout&&(this.timeouts.delete(e),n.fn.call(null))}this._flushing=!1,this._flushingInvalidated&&(this._flushingInvalidated=!1,this.flushTimeouts())}increment(t){this._now+=t,this.flushTimeouts()}},t.SpecialTargets=ze,t.StateMachine=me,t.StateNode=ye,t.TimeoutError=ce,t.__unsafe_getAllOwnEventDescriptors=function(t){return[...new Set([...t._nodes.flatMap(t=>t.ownEvents)])]},t.assertEvent=function(t,e){const n=Tt(e);if(!n.some(e=>Ct(t.type,e))){const e=1===n.length?`type matching "${n[0]}"`:`one of types matching "${n.join('", "')}"`;throw new Error(`Expected event ${JSON.stringify(t)} to have ${e}`)}},t.checkStateIn=function(t,e){if("string"==typeof e&&P(e)){const n=t.machine.getStateNodeById(e);return t._nodes.some(t=>t===n)}return t.matches(e)},t.createActor=te,t.createAsyncLogic=function(t){const e=t;return qt({id:e.id,schemas:e.schemas,context:void 0,run:({event:t,input:s,self:o,system:i},r)=>{switch(t.type){case ie:{const e=t.data??t.output;return{status:"done",output:e,input:void 0,effects:{async:{status:"done",output:e}}}}case re:{const e=t.data??t.error;return{status:"error",error:e,input:void 0,effects:{async:{status:"error",error:e}}}}}if(t.type===n)return r.effect(()=>{const t=o,n=new AbortController,c=e.timeout,a=I(c),u=void 0===a?void 0:i._clock.setTimeout(()=>{"active"===o.getSnapshot().status&&(n.abort(),i._relay(t,t,{type:re,data:new ce(c)}))},a),f=()=>{void 0!==u&&i._clock.clearTimeout(u)};return Promise.resolve(e.run({input:s,system:i,self:o,signal:n.signal},{emit:r.emit,step:async(e,n)=>{const s=o.getSnapshot().effects?.[e];if("done"===s?.status)return s.output;if("error"===s?.status)throw s.error;if("active"===s?.status)return function(t,e){return new Promise((n,s)=>{const o=t.subscribe(t=>{const i=t.effects?.[e];"done"===i?.status?(o.unsubscribe(),n(i.output)):"error"===i?.status&&(o.unsubscribe(),s(i.error))})})}(o,e);i._relay(t,t,{type:Vt,key:e});try{const s=await n();return i._relay(t,t,{type:Kt,key:e,output:s}),s}catch(n){throw i._relay(t,t,{type:Wt,key:e,error:n}),n}}})).then(e=>{f(),"active"===o.getSnapshot().status&&i._relay(t,t,{type:ie,data:e})},e=>{f(),"active"===o.getSnapshot().status&&i._relay(t,t,{type:re,data:e})}),()=>{n.abort(),f()}}),{effects:{async:{status:"active"}}}}})},t.createCallbackLogic=function(t){const e="function"==typeof t?t:t.run;return qt({schemas:"function"==typeof t?void 0:t.schemas,context:void 0,run:(t,n)=>{const{event:s,input:o,self:i,system:r}=t,c=t.emit,a=ee.get(i);a?.receivers?.forEach(t=>t(s)),n.effect("callback",()=>{const t={receivers:void 0,dispose:void 0};return ee.set(i,t),t.dispose=e({input:o,system:r,self:i,sendBack:t=>{if("stopped"===i.getSnapshot().status)return;const e=i._parent;e&&r._relay(i,e,t)},receive:e=>{t.receivers??=new Set,t.receivers.add(e)},emit:c}),()=>{ee.delete(i),t.receivers?.clear(),t.dispose?.()}})}})},t.createEmptyActor=ue,t.createEventObservableLogic=function(t){const e="function"==typeof t?t:t.run,n={config:e,transition:(t,e)=>{if("active"!==t.status)return[t,[]];switch(e.type){case se:return[{...t,status:"error",error:e.data,input:void 0,_subscription:void 0},[]];case oe:return[{...t,status:"done",input:void 0,_subscription:void 0},[]];case s:return t._subscription.unsubscribe(),[{...t,status:"stopped",input:void 0,_subscription:void 0},[]];default:return[t,[]]}},initialTransition:(t,e)=>[{status:"active",output:void 0,error:void 0,context:void 0,input:t,_subscription:void 0},[]],getInitialSnapshot:(t,e)=>n.initialTransition(e,t)[0],start:(t,{self:n,system:s,emit:o})=>{"done"!==t.status&&(t._subscription=e({input:t.input,system:s,self:n,emit:o}).subscribe({next:t=>{n._parent&&s._relay(n,n._parent,t)},error:t=>{s._relay(n,n,{type:se,data:t})},complete:()=>{s._relay(n,n,{type:oe})}}))},getPersistedSnapshot:({_subscription:t,...e})=>e,restoreSnapshot:t=>({...t,_subscription:void 0})};return n},t.createListenerLogic=p,t.createLogic=qt,t.createMachine=ve,t.createMachineFromConfig=function t(e,n={}){!function(t,e){for(const n of["actions","guards","actorSources","delays"]){const s=t[n];if(s)for(const t of Object.keys(s))if(xe(s[t])&&!e[n]?.[t])throw new Error(`Missing ${n}.${t}`)}}(e,n),be(e);const s=function(t,e){return{actions:e.actions??{},guards:e.guards??{},actorSources:e.actorSources??{},delays:{...Se(t.delays),...e.delays??{}}}}(e,n),o={},i=[];let r=null;const c=[];function a(t){if(t)return n=>{const s=n.guards?.[t.type];if(!s)throw new Error((o=t.type,i=n.guards??{},`Guard '${o}' is not implemented in machine '${e.id??"(machine)"}'. Available guards: ${Object.keys(i).map(t=>`'${t}'`).join(", ")||"(none)"}.`));var o,i;return s(n,t.params)}}function u(t){if(!t||"function"==typeof t)return t;const{guard:e,...n}=t;if(!e)return n;const s=a({type:e});return t=>s(t)?n:void 0}function f(e){return(Array.isArray(e)?e:[e]).map(e=>{const n=e;let o;if(n._nestedMachineJSON)o=t(n._nestedMachineJSON,s);else if(xe(e.src)){if(o=e.src.id?s.actorSources[e.src.id]:void 0,!o)throw new Error(`Missing actorSources.${e.src.id??""}`)}else o=e.src;return{src:o,id:e.id,registryKey:e.registryKey,input:e.input,onDone:e.onDone?l(e.onDone):void 0,onError:e.onError?l(e.onError):void 0,onSnapshot:e.onSnapshot?l(e.onSnapshot):void 0,timeout:e.timeout,onTimeout:e.onTimeout?l(e.onTimeout):void 0}})}function h(t,e,n){const s=n instanceof Error?n.message:"string"==typeof n?n:"unknown error";t.enq.raise({type:"error.execution",_scxmlEventType:"platform",_scxmlEventData:{tagname:e,message:s,line:NaN,column:NaN,reason:s}}),t.errored=!0}function d(t,e,n,s){const o=s??{enq:n,errored:!1};let i;for(const s of t){if(o.errored)break;if(Ie(s))switch(s.type){case"@xstate.raise":{const t=void 0!==s.delay,e=t||void 0!==s.id?{...s.event,...t?{_scxmlExternal:!0}:{},...void 0!==s.id?{_scxmlSendId:s.id}:{}}:s.event;n.raise(e,{id:s.id,delay:s.delay});break}case"@xstate.cancel":n.cancel(s.id);break;case"@xstate.log":n.log(...s.args);break;case"@xstate.emit":n.emit(s.event);break;case"@xstate.assign":i??={},Object.assign(i,s.context);break;default:throw new Error(`Unknown built-in action: ${s.type}`)}else if("scxml.assign"===s.type){const t=s,n={...e.context,...i};let r;try{r=ke(n,t.expr,e.event,e.self)}catch(t){h(o,"assign",t);continue}if(t.location.includes(".")){h(o,"assign",new Error(`Invalid assign location: ${t.location}`));continue}i??={},i[t.location]=r}else if("scxml.raise"===s.type){const t=s,r={...e.context,...i};let c,a,u,f;try{if(c=t.eventexpr?ke(r,t.eventexpr,e.event,e.self):t.event||"unknown",a={type:c},t.params)for(const n of t.params)a[n.name]=ke(r,n.expr,e.event,e.self);u=t.targetexpr?ke(r,t.targetexpr,e.event,e.self):t.target;const n="#_internal"===u;f=t.delayexpr?_e(ke(r,t.delayexpr,e.event,e.self)):n?void 0:t.delay}catch(t){h(o,"send",t);continue}const d="#_internal"===u,l="#_parent"===u;if("string"==typeof u&&u.length>0&&!u.startsWith("#_")){h(o,"send",new Error(`Invalid send target: ${u}`));continue}if(void 0!==t.id&&(a._scxmlSendId=t.id),l&&e.parent)n.sendTo(e.parent,a,void 0!==f?{delay:f}:void 0);else if("string"!=typeof u||!u.startsWith("#_")||l||d)void 0!==f&&(a._scxmlExternal=!0),n.raise(a,{id:t.id,delay:f});else{const s=u.slice(2),o=e.children?.[s];o?n.sendTo(o,a):(void 0!==f&&(a._scxmlExternal=!0),n.raise(a,{id:t.id,delay:f}))}}else if("scxml.cancel"===s.type){const t=s,o={...e.context,...i};try{const s=ke(o,t.sendidexpr,e.event,e.self);s&&n.cancel(s)}catch{}}else if("scxml.script"===s.type){const t=s,n={...e.context,...i};try{const e=$e(n,t.code);i??={},Object.assign(i,e)}catch(t){h(o,"script",t)}}else if("scxml.foreach"===s.type){const t=s,r={...e.context,...i},c=t=>/^[$_a-zA-Z][$_a-zA-Z0-9]*$/.test(t);if(!c(t.item)||t.index&&!c(t.index)){h(o,"foreach",new Error("foreach item/index is not a legal location"));continue}let a;try{a=ke(r,t.array,e.event,e.self)}catch(t){h(o,"foreach",t);continue}if(!Array.isArray(a)){h(o,"foreach",new Error("foreach array is not iterable"));continue}i??={};for(let s=0;s<a.length;s++){i[t.item]=a[s],t.index&&(i[t.index]=s);const r={...e,context:{...e.context,...i}},c=d(t.actions,r,n,o);if(c.context&&Object.assign(i,c.context),o.errored)break}}else if("scxml.block"===s.type){const t=s,o={...e,context:{...e.context,...i}},r=d(t.actions,o,n);if(r.context){i??={};for(const t of Object.keys(r.context))r.context[t]!==e.context[t]&&(i[t]=r.context[t])}}else if("scxml.if"===s.type){const t=s,r={...e.context,...i};for(const s of t.branches){let t;if(s.cond)try{t=!!ke(r,s.cond,e.event,e.self)}catch(e){h(o,"if",e),t=!1}else t=!0;if(t){if(s.actions.length){const t={...e,context:r},c=d(s.actions,t,n,o);if(c.context){i??={};for(const t of Object.keys(c.context))c.context[t]!==e.context[t]&&(i[t]=c.context[t])}}break}}}else n(e.actions[s.type],s.params)}return{context:i?{...e.context,...i}:void 0,errored:o.errored}}function l(t){return(Array.isArray(t)?t:[t]).map(t=>{const e=Array.isArray(t.target)?t.target[0]:t.target,n=t.target,s=a(t.guard);return s||t.actions?.length?s&&!t.actions?.length?{target:n,guard:s,description:t.description,reenter:t.reenter,meta:t.meta,input:t.input}:e?{guard:s,description:t.description,reenter:t.reenter,meta:t.meta,input:t.input,to:(s,r)=>{if(t.actions?.length){const n=e.replace(/^#/,"");o[n]=t.actions,i.includes(t.actions)||i.push(t.actions)}return{target:n,reenter:t.reenter}}}:{guard:s,description:t.description,reenter:t.reenter,meta:t.meta,input:t.input,to:(e,n)=>{if(t.actions?.length){i.includes(t.actions)||i.push(t.actions),r??=e.context;const s=d(t.actions,e,n);if(s.context)return{context:s.context}}return{}}}:{target:n,description:t.description,reenter:t.reenter,meta:t.meta,input:t.input}})}const p=function t(e,n){const s=e.entry,a=e.id||n;var h,p;return{id:e.id,initial:e.initial,type:e.type,history:e.history,target:e.target,description:e.description,tags:e.tags,input:e.input,timeout:e.timeout,states:e.states?Object.entries(e.states).reduce((e,[n,s])=>(e[n]=t(s,n),e),{}):void 0,on:e.on?Object.entries(e.on).reduce((t,[e,n])=>(t[e]=l(n),t),{}):void 0,always:e.always?l(e.always):void 0,choice:e.choice?l(e.choice):void 0,route:u(e.route),after:e.after?Object.entries(e.after).reduce((t,[e,n])=>(t[e]=l(n),t),{}):void 0,onTimeout:e.onTimeout?l(e.onTimeout):void 0,entry:(t,e)=>{for(;c.length;){const t=c.shift();e.raise({type:"error.execution",_scxmlEventType:"platform",_scxmlEventData:t})}let n;if(r&&i.length>0&&Object.keys(o).length>0){let s=r;for(const n of i){const o=d(n,{...t,context:s},e);o.context&&(s=o.context)}i.length=0,r=null;for(const t of Object.keys(o))delete o[t];n=s}else{const s=a?o[a]:void 0;if(s){delete o[a];const i=d(s,t,e);i.context&&(n=i.context)}r=null,i.length=0}if(s?.length){const o=n?{...t,context:n}:t,i=d(s,o,e);i.context&&(n=i.context)}return{context:n}},exit:e.exit?(p=e.exit,(t,e)=>d(p,t,e)):void 0,invoke:e.invoke?f(e.invoke):void 0,meta:e.meta,output:e._scxmlDonedata?(h=e._scxmlDonedata,({context:t,event:e,self:n})=>{try{if(h.params&&h.params.length){const s={};for(const o of h.params)s[o.name]=ke(t,o.expr,e,n);return s}if(void 0!==h.contentExpr)return ke(t,h.contentExpr,e,n);if(void 0!==h.contentText)try{return ke(t,h.contentText,e,n)}catch{return h.contentText}}catch(t){const e=t instanceof Error?t.message:"string"==typeof t?t:"unknown error";c.push({tagname:"donedata",message:e,line:NaN,column:NaN,reason:e})}}):e.output}}(e),y=e.context?{context:e.context}:{};Ee=e.id||"",we="session_"+Math.random().toString(36).slice(2);const g={"scxml.cond":({context:t,event:e,self:n},s)=>{const o=s?.expr;if(!o)return!0;try{return!!ke(t,o,e,n)}catch(t){const e=t instanceof Error?t.message:"string"==typeof t?t:"unknown error";return c.push({tagname:"cond",message:e,line:NaN,column:NaN,reason:e}),!1}},"xstate.stateIn":(t,e)=>{const n=e?.stateId;if(!n)return!1;const s=n.replace(/^#/,""),o=t._snapshot;return!!o?._nodes&&o._nodes.some(t=>t.id===s)},"xstate.not":(t,e)=>{const n=e?.guard,s=n&&t.guards?.[n.type];if(!s)throw new Error(`Guard '${n?.type}' referenced by 'xstate.not' is not implemented.`);return!s(t,n.params)}},m=ve({...p,...y,version:e.version}).provide({actions:s.actions,actorSources:s.actorSources,guards:{...g,...s.guards},delays:s.delays});return m._json=e,m},t.createObservableLogic=function(t){const e="function"==typeof t?t:t.run;return qt({schemas:"function"==typeof t?void 0:t.schemas,context:void 0,run:(t,n)=>{const{event:s,input:o,self:i,system:r}=t,c=t.emit;switch(s.type){case ne:return{context:s.data};case se:return{status:"error",error:s.data,input:void 0,effects:{observable:{status:"error",error:s.data}}};case oe:return{status:"done",input:void 0,effects:{observable:{status:"done"}}}}n.effect("observable",()=>{const t=i,n=e({input:o,system:r,self:i,emit:c}).subscribe({next:e=>{r._relay(t,t,{type:ne,data:e})},error:e=>{r._relay(t,t,{type:se,data:e})},complete:()=>{r._relay(t,t,{type:oe})}});return()=>n.unsubscribe()})}})},t.createStateConfig=function(t){return t},t.createSubscriptionLogic=g,t.createSystem=function(t={}){const e={},n=[];return{createActor(t,s){const o=te(t,{...s,_systemRef:e});return(()=>{const t=e.current;if(t)for(const e of n)e.active&&!e.subscription&&(e.subscription=t.inspect(e.observer))})(),o},get:t=>e.current?.get(t),getAll:()=>e.current?.getAll()??{},inspect(t){const s=e.current;if(s)return s.inspect(t);const o={observer:t,active:!0};return n.push(o),{unsubscribe(){o.active=!1,o.subscription?.unsubscribe()}}},setup:t=>t?Pe(t):Pe()}},t.getInitialMicrosteps=function(t,...[e]){const n=Le(t),s=c(e),o=[],i=t._getPreInitialState(n,s),r=Y(t.root,i,n,s,o),{microsteps:a}=st(r[0],s,n,o);return[r,...a]},t.getInitialSnapshot=function(t,...[e]){const n=Le(t);return t.initialTransition(e,n)[0]},t.getMicrosteps=function(t,e,n){const s=Le(t),{microsteps:o}=st(e,n,s,[]);return o},t.getNextSnapshot=function(t,e,n){const s=Le(t);s.self._snapshot=e;const o=t.transition(e,n,s);return Array.isArray(o)?o[0]:o},t.getNextTransitions=function(t){const e=[],n=t._nodes.filter(T),s=new Set;for(const t of n)for(const n of[t].concat(O(t,void 0)))if(!s.has(n.id)){s.add(n.id);for(const[,t]of n.transitions.entries())e.push(...t);n.always&&e.push(...n.always)}return e},t.getStateNodes=J,t.initialTransition=function(t,...[e]){const n=Le(t),[s,o]=t.initialTransition(e,n);return[s,o]},t.isBuiltInExecutableAction=function(t){return Object.prototype.hasOwnProperty.call(f,t.type)},t.isMachineSnapshot=ut,t.isTypeSchema=function(t){return!!t&&"object"==typeof t&&"~standard"in t&&"xstate.types"===t["~standard"].vendor},t.listenerLogic=y,t.machineConfigToJSON=De,t.mapState=function(t,e){const n=[],s=(t,e)=>{let n=t;for(const t of e){if(!n?.states)return;const e=n.states;if(!(t in e))return;n=e[t]}return n},o=new Set;for(const i of t._nodes.filter(T)){let r=i;for(;r&&!o.has(r);){o.add(r);const i=s(e,r.path);i?.map&&n.push({stateNode:r,result:i.map(t)}),r=r.parent}}return n},t.matchesState=Et,t.pathToStateValue=It,t.serializeMachine=function(t){return t._json??De(t.config)},t.setup=Pe,t.subscriptionLogic=m,t.toObserver=Pt,t.toPromise=function(t){return new Promise((e,n)=>{t.subscribe({complete:()=>{e(t.getSnapshot().output)},error:n})})},t.transition=function(t,e,n){const s=Le(t),[o,i]=t.transition(e,n,s);return[o,i]},t.types=function(){return{"~standard":{version:1,vendor:"xstate.types",validate:t=>({value:t})}}},t.waitFor=function(t,e,n){const s={...Ke,...n};return new Promise((n,o)=>{const{signal:i}=s;if(i?.aborted)return void o(i.reason);let r=!1;const c=s.timeout===1/0?void 0:setTimeout(()=>{a(),o(new Error(`Timeout of ${s.timeout} ms exceeded`))},s.timeout),a=()=>{clearTimeout(c),r=!0,h?.unsubscribe(),f&&i.removeEventListener("abort",f)};function u(t){e(t)&&(a(),n(t))}let f,h;u(t.getSnapshot()),r||(i&&(f=()=>{a(),o(i.reason)},i.addEventListener("abort",f)),h=t.subscribe({next:u,error:t=>{a(),o(t)},complete:()=>{a(),o(new Error("Actor terminated without satisfying predicate"))}}),r&&h.unsubscribe())})},Object.defineProperty(t,"__esModule",{value:!0})});
2
2
  //# sourceMappingURL=xstate.umd.min.js.map