xstate 5.0.0-beta.11 → 5.0.0-beta.12

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 (29) hide show
  1. package/actions/dist/xstate-actions.cjs.js +1 -1
  2. package/actions/dist/xstate-actions.development.cjs.js +1 -1
  3. package/actions/dist/xstate-actions.development.esm.js +1 -1
  4. package/actions/dist/xstate-actions.esm.js +1 -1
  5. package/actions/dist/xstate-actions.umd.min.js +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +1 -1
  8. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  9. package/actors/dist/xstate-actors.development.esm.js +1 -1
  10. package/actors/dist/xstate-actors.esm.js +1 -1
  11. package/actors/dist/xstate-actors.umd.min.js +1 -1
  12. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  13. package/dist/{actions-b82e841e.esm.js → actions-b34f6ce7.esm.js} +7 -21
  14. package/dist/{actions-cff79077.development.esm.js → actions-c8b9504d.development.esm.js} +7 -21
  15. package/dist/{actions-acbe7aa1.development.cjs.js → actions-d9c19f35.development.cjs.js} +7 -21
  16. package/dist/{actions-26f9aa9d.cjs.js → actions-e4c704f3.cjs.js} +7 -21
  17. package/dist/declarations/src/interpreter.d.ts +1 -6
  18. package/dist/declarations/src/types.d.ts +1 -0
  19. package/dist/xstate.cjs.js +1 -1
  20. package/dist/xstate.development.cjs.js +1 -1
  21. package/dist/xstate.development.esm.js +2 -2
  22. package/dist/xstate.esm.js +2 -2
  23. package/dist/xstate.umd.min.js +1 -1
  24. package/dist/xstate.umd.min.js.map +1 -1
  25. package/guards/dist/xstate-guards.cjs.js +1 -1
  26. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  27. package/guards/dist/xstate-guards.development.esm.js +1 -1
  28. package/guards/dist/xstate-guards.esm.js +1 -1
  29. package/package.json +1 -1
@@ -1178,25 +1178,6 @@ class Interpreter {
1178
1178
  };
1179
1179
  }
1180
1180
 
1181
- /**
1182
- * Adds a state listener that is notified when the statechart has reached its final state.
1183
- * @param listener The state listener
1184
- */
1185
- onDone(listener) {
1186
- if (this.status === ActorStatus.Stopped && this._doneEvent) {
1187
- listener(this._doneEvent);
1188
- } else {
1189
- this.observers.add({
1190
- complete: () => {
1191
- if (this._doneEvent) {
1192
- listener(this._doneEvent);
1193
- }
1194
- }
1195
- });
1196
- }
1197
- return this;
1198
- }
1199
-
1200
1181
  /**
1201
1182
  * Starts the interpreter from the initial state
1202
1183
  */
@@ -2951,6 +2932,9 @@ function invoke(invokeDef) {
2951
2932
 
2952
2933
  function createSpawner(self, machine, context, event, mutCapturedActions) {
2953
2934
  return (src, options = {}) => {
2935
+ const {
2936
+ systemId
2937
+ } = options;
2954
2938
  if (isString(src)) {
2955
2939
  const referenced = resolveReferencedActor(machine.options.actors[src]);
2956
2940
  if (referenced) {
@@ -2974,7 +2958,8 @@ function createSpawner(self, machine, context, event, mutCapturedActions) {
2974
2958
  // TODO
2975
2959
  ref: actorRef,
2976
2960
  meta: undefined,
2977
- input
2961
+ input,
2962
+ systemId
2978
2963
  }));
2979
2964
  return actorRef; // TODO: fix types
2980
2965
  }
@@ -2986,7 +2971,8 @@ function createSpawner(self, machine, context, event, mutCapturedActions) {
2986
2971
  const actorRef = interpret(src, {
2987
2972
  id: options.id || 'anonymous',
2988
2973
  parent: self,
2989
- input: options.input
2974
+ input: options.input,
2975
+ systemId
2990
2976
  });
2991
2977
  mutCapturedActions.push(invoke({
2992
2978
  // @ts-ignore TODO: fix types
@@ -1188,25 +1188,6 @@ class Interpreter {
1188
1188
  };
1189
1189
  }
1190
1190
 
1191
- /**
1192
- * Adds a state listener that is notified when the statechart has reached its final state.
1193
- * @param listener The state listener
1194
- */
1195
- onDone(listener) {
1196
- if (this.status === ActorStatus.Stopped && this._doneEvent) {
1197
- listener(this._doneEvent);
1198
- } else {
1199
- this.observers.add({
1200
- complete: () => {
1201
- if (this._doneEvent) {
1202
- listener(this._doneEvent);
1203
- }
1204
- }
1205
- });
1206
- }
1207
- return this;
1208
- }
1209
-
1210
1191
  /**
1211
1192
  * Starts the interpreter from the initial state
1212
1193
  */
@@ -2986,6 +2967,9 @@ function invoke(invokeDef) {
2986
2967
 
2987
2968
  function createSpawner(self, machine, context, event, mutCapturedActions) {
2988
2969
  return (src, options = {}) => {
2970
+ const {
2971
+ systemId
2972
+ } = options;
2989
2973
  if (isString(src)) {
2990
2974
  const referenced = resolveReferencedActor(machine.options.actors[src]);
2991
2975
  if (referenced) {
@@ -3009,7 +2993,8 @@ function createSpawner(self, machine, context, event, mutCapturedActions) {
3009
2993
  // TODO
3010
2994
  ref: actorRef,
3011
2995
  meta: undefined,
3012
- input
2996
+ input,
2997
+ systemId
3013
2998
  }));
3014
2999
  return actorRef; // TODO: fix types
3015
3000
  }
@@ -3021,7 +3006,8 @@ function createSpawner(self, machine, context, event, mutCapturedActions) {
3021
3006
  const actorRef = interpret(src, {
3022
3007
  id: options.id || 'anonymous',
3023
3008
  parent: self,
3024
- input: options.input
3009
+ input: options.input,
3010
+ systemId
3025
3011
  });
3026
3012
  mutCapturedActions.push(invoke({
3027
3013
  // @ts-ignore TODO: fix types
@@ -1190,25 +1190,6 @@ class Interpreter {
1190
1190
  };
1191
1191
  }
1192
1192
 
1193
- /**
1194
- * Adds a state listener that is notified when the statechart has reached its final state.
1195
- * @param listener The state listener
1196
- */
1197
- onDone(listener) {
1198
- if (this.status === ActorStatus.Stopped && this._doneEvent) {
1199
- listener(this._doneEvent);
1200
- } else {
1201
- this.observers.add({
1202
- complete: () => {
1203
- if (this._doneEvent) {
1204
- listener(this._doneEvent);
1205
- }
1206
- }
1207
- });
1208
- }
1209
- return this;
1210
- }
1211
-
1212
1193
  /**
1213
1194
  * Starts the interpreter from the initial state
1214
1195
  */
@@ -2988,6 +2969,9 @@ function invoke(invokeDef) {
2988
2969
 
2989
2970
  function createSpawner(self, machine, context, event, mutCapturedActions) {
2990
2971
  return (src, options = {}) => {
2972
+ const {
2973
+ systemId
2974
+ } = options;
2991
2975
  if (isString(src)) {
2992
2976
  const referenced = resolveReferencedActor(machine.options.actors[src]);
2993
2977
  if (referenced) {
@@ -3011,7 +2995,8 @@ function createSpawner(self, machine, context, event, mutCapturedActions) {
3011
2995
  // TODO
3012
2996
  ref: actorRef,
3013
2997
  meta: undefined,
3014
- input
2998
+ input,
2999
+ systemId
3015
3000
  }));
3016
3001
  return actorRef; // TODO: fix types
3017
3002
  }
@@ -3023,7 +3008,8 @@ function createSpawner(self, machine, context, event, mutCapturedActions) {
3023
3008
  const actorRef = interpret(src, {
3024
3009
  id: options.id || 'anonymous',
3025
3010
  parent: self,
3026
- input: options.input
3011
+ input: options.input,
3012
+ systemId
3027
3013
  });
3028
3014
  mutCapturedActions.push(invoke({
3029
3015
  // @ts-ignore TODO: fix types
@@ -1180,25 +1180,6 @@ class Interpreter {
1180
1180
  };
1181
1181
  }
1182
1182
 
1183
- /**
1184
- * Adds a state listener that is notified when the statechart has reached its final state.
1185
- * @param listener The state listener
1186
- */
1187
- onDone(listener) {
1188
- if (this.status === ActorStatus.Stopped && this._doneEvent) {
1189
- listener(this._doneEvent);
1190
- } else {
1191
- this.observers.add({
1192
- complete: () => {
1193
- if (this._doneEvent) {
1194
- listener(this._doneEvent);
1195
- }
1196
- }
1197
- });
1198
- }
1199
- return this;
1200
- }
1201
-
1202
1183
  /**
1203
1184
  * Starts the interpreter from the initial state
1204
1185
  */
@@ -2953,6 +2934,9 @@ function invoke(invokeDef) {
2953
2934
 
2954
2935
  function createSpawner(self, machine, context, event, mutCapturedActions) {
2955
2936
  return (src, options = {}) => {
2937
+ const {
2938
+ systemId
2939
+ } = options;
2956
2940
  if (isString(src)) {
2957
2941
  const referenced = resolveReferencedActor(machine.options.actors[src]);
2958
2942
  if (referenced) {
@@ -2976,7 +2960,8 @@ function createSpawner(self, machine, context, event, mutCapturedActions) {
2976
2960
  // TODO
2977
2961
  ref: actorRef,
2978
2962
  meta: undefined,
2979
- input
2963
+ input,
2964
+ systemId
2980
2965
  }));
2981
2966
  return actorRef; // TODO: fix types
2982
2967
  }
@@ -2988,7 +2973,8 @@ function createSpawner(self, machine, context, event, mutCapturedActions) {
2988
2973
  const actorRef = interpret(src, {
2989
2974
  id: options.id || 'anonymous',
2990
2975
  parent: self,
2991
- input: options.input
2976
+ input: options.input,
2977
+ systemId
2992
2978
  });
2993
2979
  mutCapturedActions.push(invoke({
2994
2980
  // @ts-ignore TODO: fix types
@@ -1,7 +1,7 @@
1
1
  import { symbolObservable } from './symbolObservable.ts';
2
2
  import { AreAllImplementationsAssumedToBeProvided, MissingImplementationsError } from './typegenTypes.ts';
3
3
  import type { ActorSystem, AnyActorBehavior, AnyStateMachine, EventFromBehavior, InterpreterFrom, PersistedStateFrom, RaiseActionObject, SnapshotFrom } from './types.ts';
4
- import { ActorRef, DoneEvent, EventObject, InteropSubscribable, InterpreterOptions, Observer, SendActionObject, Subscription } from './types.ts';
4
+ import { ActorRef, EventObject, InteropSubscribable, InterpreterOptions, Observer, SendActionObject, Subscription } from './types.ts';
5
5
  export type SnapshotListener<TBehavior extends AnyActorBehavior> = (state: SnapshotFrom<TBehavior>) => void;
6
6
  export type EventListener<TEvent extends EventObject = EventObject> = (event: TEvent) => void;
7
7
  export type Listener = () => void;
@@ -61,11 +61,6 @@ export declare class Interpreter<TBehavior extends AnyActorBehavior, TEvent exte
61
61
  private update;
62
62
  subscribe(observer: Observer<SnapshotFrom<TBehavior>>): Subscription;
63
63
  subscribe(nextListener?: (state: SnapshotFrom<TBehavior>) => void, errorListener?: (error: any) => void, completeListener?: () => void): Subscription;
64
- /**
65
- * Adds a state listener that is notified when the statechart has reached its final state.
66
- * @param listener The state listener
67
- */
68
- onDone(listener: EventListener<DoneEvent>): this;
69
64
  /**
70
65
  * Starts the interpreter from the initial state
71
66
  */
@@ -79,6 +79,7 @@ export interface ActionMeta<TEvent extends EventObject, TAction extends Paramete
79
79
  export type Spawner = <T extends ActorBehavior<any, any> | string>(// TODO: read string from machine behavior keys
80
80
  behavior: T, options?: Partial<{
81
81
  id: string;
82
+ systemId?: string;
82
83
  input: any;
83
84
  }>) => T extends ActorBehavior<infer TActorEvent, infer TActorEmitted> ? ActorRef<TActorEvent, TActorEmitted> : ActorRef<any, any>;
84
85
  export interface AssignMeta<TExpressionEvent extends EventObject, _TEvent extends EventObject> extends StateMeta<TExpressionEvent> {
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var actors_dist_xstateActors = require('./actions-26f9aa9d.cjs.js');
5
+ var actors_dist_xstateActors = require('./actions-e4c704f3.cjs.js');
6
6
  require('../dev/dist/xstate-dev.cjs.js');
7
7
 
8
8
  const EMPTY_OBJECT = {};
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var actors_dist_xstateActors = require('./actions-acbe7aa1.development.cjs.js');
5
+ var actors_dist_xstateActors = require('./actions-d9c19f35.development.cjs.js');
6
6
  require('../dev/dist/xstate-dev.development.cjs.js');
7
7
 
8
8
  const EMPTY_OBJECT = {};
@@ -1,5 +1,5 @@
1
- import { m as mapValues, t as toActionObjects, a as toArray, f as formatTransitions, b as toTransitionConfigArray, N as NULL_EVENT, c as formatTransition, d as memo, e as evaluateGuard, g as flatten, h as createInvokeId, i as isString, j as invoke, k as getDelayedTransitions, l as formatInitialTransition, n as getCandidates, o as toInvokeConfig, p as createSpawner, S as STATE_DELIMITER, q as getConfiguration, r as getStateNodes, s as resolveStateValue, u as isInFinalState, v as State, w as isErrorEvent, x as macrostep, y as transitionNode, z as getInitialConfiguration, A as createInitEvent, B as resolveActionsAndContext, C as microstep, D as error, E as isStateId, F as getStateNodeByPath, G as getPersistedState, H as resolveReferencedActor, I as interpret, J as initEvent, K as matchesState } from './actions-cff79077.development.esm.js';
2
- export { a9 as ActionTypes, P as Interpreter, Q as InterpreterStatus, aa as SpecialTargets, v as State, a7 as and, T as assign, U as cancel, V as choose, R as doneInvoke, O as forwardTo, a2 as fromCallback, a3 as fromEventObservable, a1 as fromObservable, a0 as fromPromise, a4 as fromTransition, r as getStateNodes, I as interpret, W as log, K as matchesState, a6 as not, a8 as or, _ as pathToStateValue, X as pure, Y as raise, M as sendParent, L as sendTo, a5 as stateIn, Z as stop, $ as toObserver } from './actions-cff79077.development.esm.js';
1
+ import { m as mapValues, t as toActionObjects, a as toArray, f as formatTransitions, b as toTransitionConfigArray, N as NULL_EVENT, c as formatTransition, d as memo, e as evaluateGuard, g as flatten, h as createInvokeId, i as isString, j as invoke, k as getDelayedTransitions, l as formatInitialTransition, n as getCandidates, o as toInvokeConfig, p as createSpawner, S as STATE_DELIMITER, q as getConfiguration, r as getStateNodes, s as resolveStateValue, u as isInFinalState, v as State, w as isErrorEvent, x as macrostep, y as transitionNode, z as getInitialConfiguration, A as createInitEvent, B as resolveActionsAndContext, C as microstep, D as error, E as isStateId, F as getStateNodeByPath, G as getPersistedState, H as resolveReferencedActor, I as interpret, J as initEvent, K as matchesState } from './actions-c8b9504d.development.esm.js';
2
+ export { a9 as ActionTypes, P as Interpreter, Q as InterpreterStatus, aa as SpecialTargets, v as State, a7 as and, T as assign, U as cancel, V as choose, R as doneInvoke, O as forwardTo, a2 as fromCallback, a3 as fromEventObservable, a1 as fromObservable, a0 as fromPromise, a4 as fromTransition, r as getStateNodes, I as interpret, W as log, K as matchesState, a6 as not, a8 as or, _ as pathToStateValue, X as pure, Y as raise, M as sendParent, L as sendTo, a5 as stateIn, Z as stop, $ as toObserver } from './actions-c8b9504d.development.esm.js';
3
3
  import '../dev/dist/xstate-dev.development.esm.js';
4
4
 
5
5
  const EMPTY_OBJECT = {};
@@ -1,5 +1,5 @@
1
- import { m as mapValues, t as toActionObjects, a as toArray, f as formatTransitions, b as toTransitionConfigArray, N as NULL_EVENT, c as formatTransition, d as memo, e as evaluateGuard, g as flatten, h as createInvokeId, i as isString, j as invoke, k as getDelayedTransitions, l as formatInitialTransition, n as getCandidates, o as toInvokeConfig, p as createSpawner, S as STATE_DELIMITER, q as getConfiguration, r as getStateNodes, s as resolveStateValue, u as isInFinalState, v as State, w as isErrorEvent, x as macrostep, y as transitionNode, z as getInitialConfiguration, A as createInitEvent, B as resolveActionsAndContext, C as microstep, D as error, E as isStateId, F as getStateNodeByPath, G as getPersistedState, H as resolveReferencedActor, I as interpret, J as initEvent, K as matchesState } from './actions-b82e841e.esm.js';
2
- export { a9 as ActionTypes, P as Interpreter, Q as InterpreterStatus, aa as SpecialTargets, v as State, a7 as and, T as assign, U as cancel, V as choose, R as doneInvoke, O as forwardTo, a2 as fromCallback, a3 as fromEventObservable, a1 as fromObservable, a0 as fromPromise, a4 as fromTransition, r as getStateNodes, I as interpret, W as log, K as matchesState, a6 as not, a8 as or, _ as pathToStateValue, X as pure, Y as raise, M as sendParent, L as sendTo, a5 as stateIn, Z as stop, $ as toObserver } from './actions-b82e841e.esm.js';
1
+ import { m as mapValues, t as toActionObjects, a as toArray, f as formatTransitions, b as toTransitionConfigArray, N as NULL_EVENT, c as formatTransition, d as memo, e as evaluateGuard, g as flatten, h as createInvokeId, i as isString, j as invoke, k as getDelayedTransitions, l as formatInitialTransition, n as getCandidates, o as toInvokeConfig, p as createSpawner, S as STATE_DELIMITER, q as getConfiguration, r as getStateNodes, s as resolveStateValue, u as isInFinalState, v as State, w as isErrorEvent, x as macrostep, y as transitionNode, z as getInitialConfiguration, A as createInitEvent, B as resolveActionsAndContext, C as microstep, D as error, E as isStateId, F as getStateNodeByPath, G as getPersistedState, H as resolveReferencedActor, I as interpret, J as initEvent, K as matchesState } from './actions-b34f6ce7.esm.js';
2
+ export { a9 as ActionTypes, P as Interpreter, Q as InterpreterStatus, aa as SpecialTargets, v as State, a7 as and, T as assign, U as cancel, V as choose, R as doneInvoke, O as forwardTo, a2 as fromCallback, a3 as fromEventObservable, a1 as fromObservable, a0 as fromPromise, a4 as fromTransition, r as getStateNodes, I as interpret, W as log, K as matchesState, a6 as not, a8 as or, _ as pathToStateValue, X as pure, Y as raise, M as sendParent, L as sendTo, a5 as stateIn, Z as stop, $ as toObserver } from './actions-b34f6ce7.esm.js';
3
3
  import '../dev/dist/xstate-dev.esm.js';
4
4
 
5
5
  const EMPTY_OBJECT = {};
@@ -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";let e=function(t){return t.Stop="xstate.stop",t.Raise="xstate.raise",t.Send="xstate.send",t.Cancel="xstate.cancel",t.Assign="xstate.assign",t.After="xstate.after",t.DoneState="done.state",t.DoneInvoke="done.invoke",t.Log="xstate.log",t.Init="xstate.init",t.Invoke="xstate.invoke",t.ErrorExecution="error.execution",t.ErrorCommunication="error.communication",t.ErrorPlatform="error.platform",t.ErrorCustom="xstate.error",t.Pure="xstate.pure",t.Choose="xstate.choose",t}({}),n=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});const s=e.Stop,i=e.Raise,o=e.Send,r=e.Cancel,a=e.Assign;e.After,e.DoneState;const c=e.Log,u=e.Init,h=e.Invoke,d=e.ErrorExecution,f=e.ErrorPlatform,p=e.ErrorCustom,l=e.Choose,y=e.Pure,v=".",m="",g="",x="*";function b(t,e,n=v){const s=S(t,n),i=S(e,n);return C(i)?!!C(s)&&i===s:C(s)?s in i:Object.keys(s).every((t=>t in i&&b(s[t],i[t])))}function _(t,e){try{return O(t)?t:t.toString().split(e)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}function S(t,e){if("object"==typeof(n=t)&&"value"in n&&"context"in n&&"event"in n)return t.value;var n;if(O(t))return w(t);if("string"!=typeof t)return t;return w(_(t,e))}function w(t){if(1===t.length)return t[0];const e={};let n=e;for(let e=0;e<t.length-1;e++)e===t.length-2?n[t[e]]=t[e+1]:(n[t[e]]={},n=n[t[e]]);return e}function $(t,e){const n={},s=Object.keys(t);for(let i=0;i<s.length;i++){const o=s[i];n[o]=e(t[o],o,t,i)}return n}function E(t){return[].concat(...t)}function k(t){return O(t)?t:[t]}function I(t){return void 0===t?[]:k(t)}function T(t,e,n){if(j(t))return t({context:e,event:n});const s={},i={context:e,event:n};for(const e of Object.keys(t)){const n=t[e];j(n)?s[e]=n(i):s[e]=n}return s}function O(t){return Array.isArray(t)}function j(t){return"function"==typeof t}function C(t){return"string"==typeof t}function A(t,e){return k(e).map((e=>void 0===e||"string"==typeof e?{target:e,event:t}:{...e,event:t}))}function P(t){if(void 0!==t&&t!==m)return I(t)}function M(t,e,n){const s=()=>{},i="object"==typeof t,o=i?t:null;return{next:((i?t.next:t)||s).bind(o),error:((i?t.error:e)||s).bind(o),complete:((i?t.complete:n)||s).bind(o)}}function N(t){return t?"transition"in t?{src:t,input:void 0}:t:void 0}function D(t,e){return{type:t.type,params:t.params,resolve:e}}function R(t){return"object"==typeof t&&null!==t&&"resolve"in t}function J(t,e){return D({type:o,params:{to:e?e.to:void 0,delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:j(t)?t.name:t.type}},((s,{actorContext:i,state:r})=>{const a={to:e?e.to:void 0,delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:j(t)?t.name:t.type},c={context:r.context,event:s,self:i?.self??null,system:i?.system},u=r.machine.options.delays;if("string"==typeof t)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${t}" }) instead`);const h=j(t)?t(c):t;let d;if(C(a.delay)){const t=u&&u[a.delay];d=j(t)?t(c):t}else d=j(a.delay)?a.delay(c):a.delay;const f=j(a.to)?a.to(c):a.to;let l;if("string"==typeof f){if(l=f===n.Parent?i?.self._parent:f===n.Internal?i?.self:f.startsWith("#_")?r.children[f.slice(2)]:r.children[f],!l)throw new Error(`Unable to send event to actor '${f}' from machine '${r.machine.id}'.`)}else l=f||i?.self;const y={type:o,params:{...a,to:l,event:h,delay:d,internal:f===n.Internal},execute:t=>{const e=y;if("number"!=typeof e.params.delay){const n=e.params.to,s=e.params.event;t.defer((()=>{n.send(s.type===p?{type:`${Yt(t.self.id)}`,data:s.data}:e.params.event)}))}else t.self.delaySend(e)}};return[r,y]}))}class Q{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)}prepend(t){this._current?this._current={value:t,next:this._current}:this.enqueue(t)}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),t===this._current&&(this._current=this._current.next)}this._last=null}}const V="function"==typeof Symbol&&Symbol.observable||"@@observable";const B="xstate.init",z="xstate.stop";function U(t){return t===B||t===z}function W(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const q=t=>{const e=W();e&&e.register(t)};let L=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const F={deferEvents:!0,clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class G{constructor(t,e){this.behavior=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new Q(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=L.NotStarted,this._parent=void 0,this.ref=void 0,this._actorContext=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={...F,...e},{clock:s,logger:i,parent:o,id:r,systemId:a}=n;this.system=o?.system??function(){let t=0;const e=new Map,n=new Map,s=new WeakMap;return{_bookId:()=>"x:"+t++,_register:(t,n)=>(e.set(t,n),t),_unregister:t=>{e.delete(t.sessionId);const i=s.get(t);void 0!==i&&(n.delete(i),s.delete(t))},get:t=>n.get(t),_set:(t,e)=>{const i=n.get(t);if(i&&i!==e)throw new Error(`Actor with system ID '${t}' already exists.`);n.set(t,e),s.set(e,t)}}}(),a&&(this._systemId=a,this.system._set(a,this)),this.sessionId=this.system._bookId(),this.id=r??this.sessionId,this.logger=i,this.clock=s,this._parent=o,this.options=n,this.src=n.src,this.ref=this,this._actorContext={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()}},this.send=this.send.bind(this),this._initState()}_initState(){this._state=this.options.state?this.behavior.restoreState?this.behavior.restoreState(this.options.state,this._actorContext):this.options.state:this.behavior.getInitialState(this._actorContext,this.options?.input)}update(t){this._state=t;const e=this.getSnapshot();let n;for(;n=this._deferred.shift();)n(t);for(const t of this.observers)t.next?.(e);const s=this.behavior.getStatus?.(t);switch(s?.status){case"done":this._stopProcedure(),this._doneEvent=Kt(this.id,s.data),this._parent?.send(this._doneEvent),this._complete();break;case"error":this._stopProcedure(),this._parent?.send(Yt(this.id,s.data)),this._error(s.data)}}subscribe(t,e,n){const s=M(t,e,n);return this.observers.add(s),this.status===L.Stopped&&(s.complete?.(),this.observers.delete(s)),{unsubscribe:()=>{this.observers.delete(s)}}}onDone(t){return this.status===L.Stopped&&this._doneEvent?t(this._doneEvent):this.observers.add({complete:()=>{this._doneEvent&&t(this._doneEvent)}}),this}start(){return this.status===L.Running||(this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=L.Running,this.behavior.start&&this.behavior.start(this._state,this._actorContext),this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start()),this}_process(t){try{const e=this.behavior.transition(this._state,t,this._actorContext);this.update(e),t.type===z&&(this._stopProcedure(),this._complete())}catch(t){if(!(this.observers.size>0))throw t;this.observers.forEach((e=>{e.error?.(t)})),this.stop()}}_stop(){return this.status===L.Stopped?this:(this.mailbox.clear(),this.status===L.NotStarted?(this.status=L.Stopped,this):(this.mailbox.enqueue({type:z}),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)t.complete?.();this.observers.clear()}_error(t){for(const e of this.observers)e.error?.(t);this.observers.clear()}_stopProcedure(){if(this.status!==L.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new Q(this._process.bind(this)),this.status=L.Stopped,this.system._unregister(this),this}send(t){if("string"==typeof t)throw new Error(`Only event objects may be sent to actors; use .send({ type: "${t}" }) instead`);if(this.status!==L.Stopped){if(this.status!==L.Running&&!this.options.deferEvents)throw new Error(`Event "${t.type}" was sent to uninitialized actor "${this.id}". Make sure .start() is called for this actor, or set { deferEvents: true } in the actor options.\nEvent: ${JSON.stringify(t)}`);this.mailbox.enqueue(t)}}delaySend(t){this.delayedEventsMap[t.params.id]=this.clock.setTimeout((()=>{"to"in t.params&&t.params.to?t.params.to.send(t.params.event):this.send(t.params.event)}),t.params.delay)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:q)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.behavior.getPersistedState?.(this._state)}[V](){return this}getSnapshot(){return this.behavior.getSnapshot?this.behavior.getSnapshot(this._state):this._state}}function X(t,e){return new G(t,e)}function H(t){const e=t;return D({type:s,params:{actor:e}},((t,{state:n})=>{const s=j(e)?e({context:n.context,event:t}):e,i="string"==typeof s?n.children[s]:s;return[n,{type:"xstate.stop",params:{actor:i},execute:t=>{i&&(i.status===L.Running?t.defer((()=>{t.stopChild(i)})):t.stopChild(i))}}]}))}const K=({context:t,event:e})=>({context:t,event:e});function Y(t){return D({type:r,params:{sendId:t}},((e,{state:n,actorContext:s})=>{const i=j(t)?t({context:n.context,event:e,self:s?.self??{},system:s?.system}):t;return[n,{type:"xstate.cancel",params:{sendId:i},execute:t=>{t.self.cancel(i)}}]}))}const Z=new WeakMap;function tt(t,e,n){let s=Z.get(t);return s?e in s||(s[e]=n()):(s={[e]:n()},Z.set(t,s)),s[e]}function et(t,e,n,s){const{machine:i}=s,o=i?.options?.guards?.[t.type]??t.predicate;if(!o)throw new Error(`Guard '${t.type}' is not implemented.'.`);return o({context:e,event:n,state:s,guard:t,evaluate:et})}function nt(t,e){return C(t)?{type:t,predicate:e?.(t)||void 0,params:{type:t}}:j(t)?{type:t.name,predicate:t,params:{type:t.name,name:t.name}}:{type:t.type,params:t.params||t,children:t.children?.map((t=>nt(t,e))),predicate:e?.(t.type)||t.predicate}}const st=t=>"atomic"===t.type||"final"===t.type;function it(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function ot(t,e){const n=[];let s=t.parent;for(;s&&s!==e;)n.push(s),s=s.parent;return n}function rt(t){const e=new Set(t),n=new Set(t),s=ct(n);for(const t of e)if("compound"!==t.type||s.get(t)&&s.get(t).length){if("parallel"===t.type)for(const e of it(t))if("history"!==e.type&&!n.has(e))for(const t of mt(e))n.add(t)}else mt(t).forEach((t=>n.add(t)));for(const t of n){let e=t.parent;for(;e;)n.add(e),e=e.parent}return n}function at(t,e){const n=e.get(t);if(!n)return{};if("compound"===t.type){const t=n[0];if(!t)return{};if(st(t))return t.key}const s={};for(const t of n)s[t.key]=at(t,e);return s}function ct(t){const e=new Map;for(const n of t)e.has(n)||e.set(n,[]),n.parent&&(e.has(n.parent)||e.set(n.parent,[]),e.get(n.parent).push(n));return e}function ut(t,e){return at(t,ct(rt(e)))}function ht(t,e=t[0].machine.root){return"compound"===e.type?it(e).some((e=>"final"===e.type&&t.includes(e))):"parallel"===e.type&&it(e).every((e=>ht(t,e)))}const dt=t=>"#"===t[0];function ft(t){const n=t.config.after;if(!n)return[];const s=(n,s)=>{const i=function(t,n){const s=n?`#${n}`:"";return`${e.After}(${t})${s}`}(j(n)?`${t.id}:delay[${s}]`:n,t.id);return t.entry.push(Lt({type:i},{delay:n})),t.exit.push(Y(i)),i};return(O(n)?n.map(((t,e)=>{const n=s(t.delay,e);return{...t,event:n}})):Object.keys(n).flatMap(((t,e)=>{const i=n[t],o=C(i)?{target:i}:i,r=isNaN(+t)?t:+t,a=s(r,e);return I(o).map((t=>({...t,event:a,delay:r})))}))).map((e=>{const{delay:n}=e;return{...pt(t,e),delay:n}}))}function pt(t,e){const n=P(e.target),s=e.reenter??!1,{guards:i}=t.machine.options,o=lt(t,n),r={...e,actions:Xt(I(e.actions)),guard:e.guard?nt(e.guard,(t=>i[t])):void 0,target:o,source:t,reenter:s,eventType:e.event,toJSON:()=>({...r,source:`#${t.id}`,target:o?o.map((t=>`#${t.id}`)):void 0})};return r}function lt(t,e){if(void 0!==e)return e.map((e=>{if(!C(e))return e;if(dt(e))return t.machine.getStateNodeById(e);const n=e[0]===t.machine.delimiter;if(n&&!t.parent)return xt(t,e.slice(1));const s=n?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 xt(t.parent,s)}catch(e){throw new Error(`Invalid transition definition for state node '${t.id}':\n${e.message}`)}}))}function yt(t){const e=P(t.target);return e?e.map((e=>"string"==typeof e?xt(t.parent,e):e)):t.parent.initial.target}function vt(t){return"history"===t.type}function mt(t){const e=new Set;return function n(s){if(!e.has(s))if(e.add(s),"compound"===s.type)for(const i of s.initial.target){for(const n of ot(i,t))e.add(n);n(i)}else if("parallel"===s.type)for(const t of it(s))n(t)}(t),[...e]}function gt(t,e){if(dt(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 xt(t,e){if("string"==typeof e&&dt(e))try{return t.machine.getStateNodeById(e)}catch(t){}const n=_(e,t.machine.delimiter).slice();let s=t;for(;n.length;){const t=n.shift();if(!t.length)break;s=gt(s,t)}return s}function bt(t,e){const n=e instanceof zt?e.value:S(e,t.machine.delimiter);if(C(n))return[t,t.states[n]];const s=Object.keys(n),i=s.map((e=>gt(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,s.reduce(((e,s)=>{const i=gt(t,s);if(!i)return e;const o=bt(i,n[s]);return e.concat(o)}),[]))}function _t(t,e,n,s){return C(e)?function(t,e,n,s){const i=gt(t,e).next(n,s);return i&&i.length?i:t.next(n,s)}(t,e,n,s):1===Object.keys(e).length?function(t,e,n,s){const i=Object.keys(e),o=_t(gt(t,i[0]),e[i[0]],n,s);return o&&o.length?o:t.next(n,s)}(t,e,n,s):function(t,e,n,s){const i=[];for(const o of Object.keys(e)){const r=e[o];if(!r)continue;const a=_t(gt(t,o),r,n,s);a&&i.push(...a)}return i.length?i:t.next(n,s)}(t,e,n,s)}function St(t){return Object.keys(t.states).map((e=>t.states[e])).filter((t=>"history"===t.type))}function wt(t,e){let n=t;for(;n.parent&&n.parent!==e;)n=n.parent;return n.parent===e}function $t(t){const e=[];let n=t.parent;for(;n;)e.unshift(n),n=n.parent;return e}function Et(t,e){const n=new Set(t),s=new Set(e);for(const t of n)if(s.has(t))return!0;for(const t of s)if(n.has(t))return!0;return!1}function kt(t,e,n){const s=new Set;for(const i of t){let t=!1;const o=new Set;for(const r of s)if(Et(Ot([i],e,n),Ot([r],e,n))){if(!wt(i.source,r.source)){t=!0;break}o.add(r)}if(!t){for(const t of o)s.delete(t);s.add(i)}}return Array.from(s)}function It(t,e){if(!t.target)return[];const n=new Set;for(const s of t.target)if(vt(s))if(e[s.id])for(const t of e[s.id])n.add(t);else for(const t of It({target:yt(s)},e))n.add(t);else n.add(s);return[...n]}function Tt(t,e){const n=It(t,e);if(!n)return null;if(!t.reenter&&"parallel"!==t.source.type&&n.every((e=>wt(e,t.source))))return t.source;return function(t){const[e]=t;let n=$t(e),s=[];for(const e of t){const t=$t(e);s=n.filter((e=>t.includes(e))),n=s,s=[]}return n[n.length-1]}(n.concat(t.source))}function Ot(t,e,n){const s=new Set;for(const i of t)if(i.target?.length){const t=Tt(i,n);for(const n of e)wt(n,t)&&s.add(n)}return[...s]}function jt(t,e,n,i){const{machine:o}=e,r=e._initial||t.length>0,a=new Set(e.configuration);if(!e._initial&&!r){const t=Ut(e,{event:i,actions:[],transitions:[]});return t.changed=!1,t}const c=function(t,e,n,s,i){const o=[],r={...e.historyValue},a=kt(t,n,r),c=[...e._internalQueue];e._initial||function(t,e,n,s){const i=Ot(t,e,n);i.sort(((t,e)=>e.order-t.order));for(const t of i)for(const s of St(t)){let i;i="deep"===s.history?e=>st(e)&&wt(e,t):e=>e.parent===t,n[s.id]=Array.from(e).filter(i)}for(const t of i)s.push(...t.exit.flat(),...t.invoke.map((t=>H(t.id)))),e.delete(t)}(a,n,r,o);o.push(...a.flatMap((t=>t.actions))),Ct(a,n,o,c,e,r);const u=[...n],h=ht(u);if(h){const t=u.sort(((t,e)=>e.order-t.order)).flatMap((t=>t.exit));o.push(...t)}try{const{nextState:t}=Nt(o,s,e,i),n=h?function(t,e,n){const s=t[0].machine,i=t.find((t=>"final"===t.type&&t.parent===s.root));return i&&i.output?T(i.output,e,n):void 0}(u,t.context,s):void 0;return c.push(...t._internalQueue),Ut(e,{actions:t.actions,configuration:u,historyValue:r,_internalQueue:c,context:t.context,event:s,done:h,output:n,children:t.children})}catch(t){throw t}}(e._initial?[{target:[...e.configuration].filter(st),source:o.root,reenter:!0,actions:[],eventType:null,toJSON:null}]:t,e,a,i,n),{context:u,actions:d}=c,f=function(t,e){const n={...t.children};for(const t of e)if(t.type===h&&t.params.ref){const e=t.params.ref;e&&(n[e.id]=e)}else if(t.type===s){const e=t.params.actor;e&&delete n[e.id]}return n}(e,d),p=Ut(c,{value:{},transitions:t,children:f});return p.changed=e._initial?void 0:!Bt(p.value,e.value)||p.actions.length>0||u!==e.context,p}function Ct(t,e,n,s,i,o){const r=new Set,a=new Set;At(t,o,a,r),i._initial&&a.add(i.machine.root);for(const t of[...r].sort(((t,e)=>t.order-e.order))){e.add(t);for(const e of t.invoke)n.push(Wt(e));if(n.push(...t.entry),a.has(t))for(const t of a){const e=t.initial.actions;n.push(...e)}if("final"===t.type){const n=t.parent;if(!n.parent)continue;if(s.push(Ht(n.id,t.output?T(t.output,i.context,i.event):void 0)),n.parent){const t=n.parent;"parallel"===t.type&&it(t).every((t=>ht([...e],t)))&&s.push(Ht(t.id))}}}}function At(t,e,n,s){for(const i of t){for(const t of i.target||[])Pt(t,e,n,s);const t=Tt(i,e),o=It(i,e);for(const i of o)Mt(i,t,s,e,n)}}function Pt(t,e,n,s){if(vt(t))if(e[t.id]){const i=e[t.id];for(const t of i)Pt(t,e,n,s);for(const o of i){Mt(o,t.parent,s,e,n);for(const t of n)n.add(t)}}else{const i=yt(t);for(const t of i)Pt(t,e,n,s);for(const o of i){Mt(o,t,s,e,n);for(const t of n)n.add(t)}}else if(s.add(t),"compound"===t.type){n.add(t);const i=t.initial.target;for(const t of i)Pt(t,e,n,s);for(const o of i)Mt(o,t,s,e,n)}else if("parallel"===t.type)for(const i of it(t).filter((t=>!vt(t))))[...s].some((t=>wt(t,i)))||Pt(i,e,n,s)}function Mt(t,e,n,s,i){const o=ot(t,e);for(const t of o)if(n.add(t),"parallel"===t.type)for(const e of it(t).filter((t=>!vt(t))))[...n].some((t=>wt(t,e)))||Pt(e,s,i,n)}function Nt(t,e,n,s){const{machine:r}=n,a=[],c=[];let u=n;function h(t){a.push(t),s?.self.status===L.Running&&(t.execute?.(s),delete t.execute)}function d(t){const n=function(t,e){if(R(t))return t;const n=e[t.type];return"function"==typeof n?D({type:"xstate.function",params:t.params??{}},((e,{state:s})=>{const i={type:t.type,params:t.params,execute:t=>n({context:s.context,event:e,action:i,system:t.system,self:t.self})};return[s,i]})):n||t}(t,r.options.actions);if(R(n)){const[r,a]=n.resolve(e,{state:u,action:t,actorContext:s}),f=a.params?.actions;return u=r,(a.type===i||a.type===o&&a.params.internal)&&"number"!=typeof a.params.delay&&c.push(a),a.type!==y&&h(a),void Xt(f).forEach(d)}h(n)}for(const e of t)d(e);return{nextState:Ut(u,{actions:a,_internalQueue:c.map((t=>t.params.event))})}}function Dt(t,e,n){let s=t;const i=[];if(e.type===z)return s=Rt(e,s,n),i.push(s),{state:s,microstates:i};if(e.type!==u){s=jt(Jt(e,s),t,n,e),i.push(s)}for(;!s.done;){let t=Qt(s);if(0===t.length){if(s.configuration.some((t=>t.always))&&(s.transitions=[]),!s._internalQueue.length)break;{const t=s.actions,e=s._internalQueue[0];s=jt(Jt(e,s),s,n,e),s._internalQueue.shift(),s.actions.unshift(...t),i.push(s)}}if(t.length){const e=s.actions;s=jt(t,s,n,s.event),s.actions.unshift(...e),i.push(s)}}return s.done&&Rt(s.event,s,n),{state:s,microstates:i}}function Rt(t,e,n){const s=[];for(const t of e.configuration.sort(((t,e)=>e.order-t.order)))s.push(...t.exit);for(const t of Object.values(e.children))s.push(H(t));const{nextState:i}=Nt(s,t,e,n);return i}function Jt(t,e){return e.machine.getTransitionData(e,t)}function Qt(t){const e=new Set,n=t.configuration.filter(st);for(const s of n)t:for(const n of[s].concat(ot(s,null)))if(n.always)for(const s of n.always)if(void 0===s.guard||et(s.guard,t.context,t.event,t)){e.add(s);break t}return kt(Array.from(e),new Set(t.configuration),t.historyValue)}function Vt(t,e){return ut(t,[...rt(bt(t,e))])}function Bt(t,e){if(t===e)return!0;if(void 0===t||void 0===e)return!1;if(C(t)||C(e))return t===e;const n=Object.keys(t),s=Object.keys(e);return n.length===s.length&&n.every((n=>Bt(t[n],e[n])))}class zt{static from(t,e={},n){if(t instanceof zt)return t.context!==e?new zt({value:t.value,context:e,event:t.event,actions:[],meta:{},configuration:[],transitions:[],children:{}},n):t;const s=Zt({}),i=rt(bt(n.root,t));return new zt({value:t,context:e,event:s,actions:[],meta:void 0,configuration:Array.from(i),transitions:[],children:{}},n)}constructor(t,e){this.machine=e,this.tags=void 0,this.value=void 0,this.done=void 0,this.output=void 0,this.context=void 0,this.historyValue={},this.actions=[],this.event=void 0,this._internalQueue=void 0,this._initial=!1,this.changed=void 0,this.configuration=void 0,this.transitions=void 0,this.children=void 0,this.context=t.context,this._internalQueue=t._internalQueue??[],this.event=t.event,this.historyValue=t.historyValue||{},this.actions=t.actions??[],this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=t.configuration??Array.from(rt(bt(e.root,t.value))),this.transitions=t.transitions,this.children=t.children,this.value=ut(e.root,this.configuration),this.tags=new Set(E(this.configuration.map((t=>t.tags)))),this.done=t.done??!1,this.output=t.output}toStrings(t=this.value,e="."){if(C(t))return[t];const n=Object.keys(t);return n.concat(...n.map((n=>this.toStrings(t[n],e).map((t=>n+e+t)))))}toJSON(){const{configuration:t,transitions:e,tags:n,machine:s,...i}=this;return{...i,tags:Array.from(n),meta:this.meta}}matches(t){return b(t,this.value)}hasTag(t){return this.tags.has(t)}can(t){const e=this.machine.getTransitionData(this,t);return!!e?.length&&e.some((t=>void 0!==t.target||t.actions.length))}get nextEvents(){return tt(this,"nextEvents",(()=>[...new Set(E([...this.configuration.map((t=>t.ownEvents))]))]))}get meta(){return this.configuration.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})}}function Ut(t,e={}){return new zt({...t,...e},t.machine)}function Wt(t){return D({type:h,params:t},((e,{state:n,actorContext:s})=>{const i=h,{id:o,src:r}=t;let a;if((c=r)&&"object"==typeof c&&"function"==typeof c.send)a={type:i,params:{...t,ref:r}};else{const c=N(n.machine.options.actors[r]);if(c){const u="input"in t?t.input:c.input,h=X(c.src,{id:o,src:r,parent:s?.self,systemId:t.systemId,input:"function"==typeof u?u({context:n.context,event:e,self:s?.self}):u});a={type:i,params:{...t,ref:h}}}else a={type:i,params:t}}var c;const u=a.params.ref,d=Ut(n,{children:{...n.children,[o]:u}});return a.execute=t=>{const e=t.self,{id:n,ref:s}=a.params;s&&t.defer((()=>{if(u.status!==L.Stopped)try{u.start?.()}catch(t){return void e.send(Yt(n,t))}}))},[d,a]}))}function qt(t,e,n,s,i){return(o,r={})=>{if(C(o)){const a=N(e.options.actors[o]);if(a){const e=r.id??"anon",o="input"in r?r.input:a.input,c=X(a.src,{id:e,parent:t,input:"function"==typeof o?o({context:n,event:s,self:t}):o});return i.push(Wt({id:c.id,src:c,ref:c,meta:void 0,input:o})),c}throw new Error(`Behavior '${o}' not implemented in machine '${e.id}'`)}{const e=X(o,{id:r.id||"anonymous",parent:t,input:r.input});return i.push(Wt({src:e,ref:e,id:e.id,meta:void 0,input:r.input})),e}}}function Lt(t,e){return D({type:i,params:{delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:"function"==typeof t?t.name:t.type}},((n,{state:s,actorContext:o})=>{const r={delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:"function"==typeof t?t.name:t.type},a={context:s.context,event:n,self:o?.self??{},system:o?.system},c=s.machine.options.delays;if("string"==typeof t)throw new Error(`Only event objects may be used with raise; use raise({ type: "${t}" }) instead`);const u="function"==typeof t?t(a):t;let h;if("string"==typeof r.delay){const t=c&&c[r.delay];h="function"==typeof t?t(a):t}else h="function"==typeof r.delay?r.delay(a):r.delay;const d={type:i,params:{...r,event:u,delay:h},execute:t=>{"number"!=typeof d.params.delay||t.self.delaySend(d)}};return[s,d]}))}const Ft={type:u};function Gt(t){if(R(t))return t;if("string"==typeof t)return{type:t,params:{}};if("function"==typeof t){const e="xstate.function";return D({type:e,params:{}},((n,{state:s})=>{const i={type:e,params:{function:t},execute:e=>t({context:s.context,event:n,action:i,self:e.self,system:e.system})};return[s,i]}))}return t}const Xt=t=>{if(!t)return[];return(O(t)?t:[t]).map(Gt)};function Ht(t,n){const s=`${e.DoneState}.${t}`,i={type:s,output:n,toString:()=>s};return i}function Kt(t,n){const s=`${e.DoneInvoke}.${t}`,i={type:s,output:n,toString:()=>s};return i}function Yt(t,n){const s=`${e.ErrorPlatform}.${t}`,i={type:s,data:n,toString:()=>s};return i}function Zt(t){return{type:u,input:t}}const te={};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.machine.delimiter),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?$(this.config.states,((t,e)=>new ee(t,{_parent:this,_key:e,_machine:this.machine}))):te,"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=Xt(this.config.entry),this.exit=Xt(this.config.exit),this.meta=this.config.meta,this.output="final"===this.type?this.config.output:void 0,this.tags=I(t.tags)}_initialize(){this.transitions=function(t){const e=[];if(Array.isArray(t.config.on))e.push(...t.config.on);else if(t.config.on){const{[x]:n=[],...s}=t.config.on;for(const t of Object.keys(s)){if(t===g)throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.');const n=A(t,s[t]);e.push(...n)}e.push(...A(x,n))}const n=t.config.onDone?A(String(Ht(t.id)),t.config.onDone):[],s=t.invoke.flatMap((t=>{const e=[];return t.onDone&&e.push(...A(`done.invoke.${t.id}`,t.onDone)),t.onError&&e.push(...A(`error.platform.${t.id}`,t.onError)),t.onSnapshot&&e.push(...A(`xstate.snapshot.${t.id}`,t.onSnapshot)),e})),i=t.after,o=[...n,...s,...e].flatMap((e=>I(e).map((e=>pt(t,e)))));for(const t of i)o.push(t);return o}(this),this.config.always&&(this.always=A(g,this.config.always).map((t=>pt(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,eventType:null,reenter:!1,toJSON:()=>({target:this.initial.target.map((t=>`#${t.id}`)),source:`#${this.id}`,actions:this.initial.actions,eventType:null})}:void 0,history:this.history,states:$(this.states,(t=>t.definition)),on:this.on,transitions:this.transitions,entry:this.entry,exit:this.exit,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 tt(this,"invoke",(()=>I(this.config.invoke).map(((t,e)=>{const n=`${this.id}:invocation[${e}]`;const s=function(t,e){if("object"==typeof t){if("src"in t)return t;if("transition"in t)return{id:e,src:t}}return{id:e,src:t}}(t,n),i=s.id||n,{src:o,systemId:r}=s,a=C(o)||"type"in o?o:i;return this.machine.options.actors[i]||"string"==typeof o||"type"in o||(this.machine.options.actors={...this.machine.options.actors,[i]:o}),{type:h,...s,src:a,id:i,systemId:r,toJSON(){const{onDone:t,onError:e,...n}=s;return{...n,type:h,src:a,id:i}}}}))))}get on(){return tt(this,"on",(()=>this.transitions.reduce(((t,e)=>(t[e.eventType]=t[e.eventType]||[],t[e.eventType].push(e),t)),{})))}get after(){return tt(this,"delayedTransitions",(()=>ft(this)))}get initial(){return tt(this,"initial",(()=>function(t,e){if(C(e)||O(e)){const n=I(e).map((e=>{const n=C(e)?dt(e)?t.machine.getStateNodeById(e):t.states[e]:e;if(!n)throw new Error(`Initial state node "${e}" not found on parent state node #${t.id}`);if(!wt(n,t))throw new Error(`Invalid initial target: state node #${n.id} is not a descendant of #${t.id}`);return n})),s=lt(t,n),i={source:t,actions:[],eventType:null,reenter:!1,target:s,toJSON:()=>({...i,source:`#${t.id}`,target:s?s.map((t=>`#${t.id}`)):void 0})};return i}return pt(t,{target:I(e.target).map((e=>C(e)?dt(e)?e:`${t.machine.delimiter}${e}`:e)),actions:e.actions,event:null})}(this,this.config.initial||[])))}next(t,e){const n=e.type,s=[];let i;const o=tt(this,`candidates-${n}`,(()=>{return t=n,this.transitions.filter((e=>{const{eventType:n}=e;if(n===t)return!0;if(n===x)return!0;if(!n.endsWith(".*"))return!1;const s=n.split("."),i=t.split(".");for(let t=0;t<s.length;t++){const e=s[t],n=i[t];if("*"===e)return t===s.length-1;if(e!==n)return!1}return!0}));var t}));for(const r of o){const{guard:o}=r,a=t.context;let c=!1;try{c=!o||et(o,a,e,t)}catch(t){throw new Error(`Unable to evaluate guard '${o.type}' in transition for event '${n}' in state node '${this.id}':\n${t.message}`)}if(c){s.push(...r.actions),i=r;break}}return i?[i]:void 0}get target(){if("history"===this.type){return this.config.target}}get stateIds(){const t=E(Object.keys(this.states).map((t=>this.states[t].stateIds)));return[this.id].concat(t)}get events(){return tt(this,"events",(()=>{const{states:t}=this,e=new Set(this.ownEvents);if(t)for(const n of Object.keys(t)){const s=t[n];if(s.states)for(const t of s.events)e.add(`${t}`)}return Array.from(e)}))}get ownEvents(){const t=new Set(this.transitions.filter((t=>!(!t.target&&!t.actions.length&&!t.reenter))).map((t=>t.eventType)));return Array.from(t)}}class ne{getContext(t){return this.getContextAndActions(void 0,t)[0]}getContextAndActions(t,e){const n=[],{context:s}=this.config;return[("function"==typeof s?s({spawn:qt(t?.self,this,void 0,Zt(e),n),input:e}):s)||{},n]}constructor(t,e){this.config=t,this.version=void 0,this.delimiter=void 0,this.options=void 0,this.types=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.__TContext=void 0,this.__TEvent=void 0,this.__TAction=void 0,this.__TActorMap=void 0,this.__TResolvedTypesMeta=void 0,this.id=t.id||"(machine)",this.options=Object.assign({actions:{},actors:{},delays:{},guards:{},context:{}},e),this.delimiter=this.config.delimiter||v,this.version=this.config.version,this.types=this.config.types??{},this.transition=this.transition.bind(this),this.root=new ee(t,{_key:this.id,_machine:this}),this.root._initialize(),this.states=this.root.states,this.events=this.root.events}provide(t){const{actions:e,guards:n,actors:s,delays:i}=this.options;return new ne(this.config,{actions:{...e,...t.actions},guards:{...n,...t.guards},actors:{...s,...t.actors},delays:{...i,...t.delays}})}resolveState(t){const e=rt(bt(this.root,t.value)),n=Array.from(e);return this.createState({...t,value:Vt(this.root,t.value),configuration:n,done:ht(n)})}resolveStateValue(t){const e=Vt(this.root,t),n=this.getContext();return this.resolveState(zt.from(e,n,this))}transition(t=this.initialState,e,n){const s=t instanceof zt?t:this.resolveStateValue(t);if(function(t){return"string"==typeof t.type&&(t.type===d||t.type.startsWith(f))}(e)&&!s.nextEvents.some((t=>t===e.type)))throw e.data;const{state:i}=Dt(s,e,n);return i}microstep(t=this.initialState,e,n){return Dt(t,e,n).microstates}getTransitionData(t,e){return _t(this.root,t.value,t,e)||[]}getPreInitialState(t,e){const[n,s]=this.getContextAndActions(t,e),i=function(t){const e=[],n=t.initial,s=new Set;At([n],{},new Set([t]),s);for(const t of[...s].sort(((t,e)=>t.order-e.order)))e.push(t);return e}(this.root),o=this.resolveState(this.createState({value:{},context:n,event:Zt({}),actions:[],meta:void 0,configuration:i,transitions:[],children:{}}));if(o._initial=!0,o.actions.unshift(...s),t){const{nextState:e}=Nt(s,Ft,o,t);o.children=e.children,o.actions=e.actions}return o}get initialState(){return this.getInitialState()}getInitialState(t,e){const n=Zt(e),s=this.getPreInitialState(t,e),i=jt([],s,t,n);i.actions.unshift(...s.actions);const{state:o}=Dt(i,n,t);return o}start(t,e){t.actions.forEach((t=>{t.execute?.(e)})),Object.values(t.children).forEach((t=>{if(0===t.status)try{t.start?.()}catch(n){e.self.send(Yt(t.id,n))}}))}getStateNodeById(t){const e=t.split(this.delimiter),n=e.slice(1),s=dt(e[0])?e[0].slice(1):e[0],i=this.idMap.get(s);if(!i)throw new Error(`Child state node '#${s}' does not exist on machine '${this.id}'`);return xt(i,n)}get definition(){return{context:this.getContext(),...this.root.definition}}toJSON(){return this.definition}getPersistedState(t){return function(t){const{configuration:e,transitions:n,tags:s,machine:i,children:o,...r}=t,a={};for(const t in o)a[t]={state:o[t].getPersistedState?.(),src:o[t].src};return{...r,children:a}}(t)}createState(t){const e=t instanceof zt?t:new zt(t,this),{nextState:n}=Nt(e.actions,e.event,e,void 0);return n}getStatus(t){return t.done?{status:"done",data:t.output}:{status:"active"}}restoreState(t,e){const n={};Object.keys(t.children).forEach((s=>{const i=t.children[s],o=i.state,r=i.src,a=r?N(this.options.actors[r])?.src:void 0;if(!a)return;const c=a.restoreState?.(o,e),u=X(a,{id:s,state:c});n[s]=u}));const s=this.createState(new zt({...t,children:n},this));return s.configuration.forEach((t=>{t.invoke&&t.invoke.forEach((t=>{const{id:s,src:i}=t;if(n[s])return;const o=N(this.options.actors[i]);if(o){const i=X(o.src,{id:s,parent:e?.self,input:"input"in t?t.input:o.input});n[s]=i}}))})),s.actions=[],s}}const se={timeout:1e4};t.ActionTypes=e,t.Interpreter=G,t.InterpreterStatus=L,t.SimulatedClock=class{constructor(){this.timeouts=new Map,this._now=0,this._id=0}now(){return this._now}getId(){return this._id++}setTimeout(t,e){const n=this.getId();return this.timeouts.set(n,{start:this.now(),timeout:e,fn:t}),n}clearTimeout(t){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(){[...this.timeouts].sort((([t,e],[n,s])=>{const i=e.start+e.timeout;return s.start+s.timeout>i?-1:1})).forEach((([t,e])=>{this.now()-e.start>=e.timeout&&(this.timeouts.delete(t),e.fn.call(null))}))}increment(t){this._now+=t,this.flushTimeouts()}},t.SpecialTargets=n,t.State=zt,t.StateMachine=ne,t.StateNode=ee,t.and=function(t){return{type:"xstate.boolean",params:{op:"and"},children:t.map((t=>nt(t))),predicate:({evaluate:t,guard:e,context:n,event:s,state:i})=>e.children.every((e=>t(e,n,s,i)))}},t.assign=function(t){return D({type:a,params:{assignment:t}},((e,{state:n,action:s,actorContext:i})=>{const o=[];if(!n.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const r={context:n.context,event:e,action:s,spawn:qt(i?.self,n.machine,n.context,e,o),self:i?.self??{},system:i?.system};let c={};if(j(t))c=t(r);else for(const e of Object.keys(t)){const n=t[e];c[e]=j(n)?n(r):n}const u=Object.assign({},n.context,c);return[Ut(n,{context:u}),{type:a,params:{context:u,actions:o}}]}))},t.cancel=Y,t.choose=function(t){return D({type:l,params:{guards:t}},((e,{state:n})=>{const s=t.find((t=>{const s=t.guard&&nt(t.guard,(t=>n.machine.options.guards[t]));return!s||et(s,n.context,e,n)}))?.actions;return[n,{type:l,params:{actions:Xt(s)}}]}))},t.createMachine=function(t,e){return new ne(t,e)},t.doneInvoke=Kt,t.forwardTo=function(t,e){return J((({event:t})=>t),{...e,to:t})},t.fromCallback=function(t){return{config:t,start:(t,{self:e})=>{e.send({type:B})},transition:(e,n,{self:s,id:i})=>{if(n.type===B){const n=t=>{e.canceled||s._parent?.send(t)},r=t=>{e.receivers.add(t)};return e.dispose=t(n,r,{input:e.input}),((o=e.dispose)instanceof Promise||null!==o&&(j(o)||"object"==typeof o)&&j(o.then))&&e.dispose.then((t=>{s._parent?.send(Kt(i,t)),e.canceled=!0}),(t=>{e.canceled=!0,s._parent?.send(Yt(i,t))})),e}var o;return n.type===z?(e.canceled=!0,j(e.dispose)&&e.dispose(),e):(U(n.type)||U(n.type)||e.receivers.forEach((t=>t(n))),e)},getInitialState:(t,e)=>({canceled:!1,receivers:new Set,dispose:void 0,input:e}),getSnapshot:()=>{},getPersistedState:({input:t})=>t}},t.fromEventObservable=function(t){const e="$$xstate.error",n="$$xstate.complete";return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case e:return{...t,status:"error",input:void 0,data:s.data,subscription:void 0};case n:return{...t,status:"done",input:void 0,subscription:void 0};case z:return t.subscription.unsubscribe(),{...t,status:"canceled",input:void 0,subscription:void 0};default:return t}},getInitialState:(t,e)=>({subscription:void 0,status:"active",data:void 0,input:e}),start:(s,{self:i})=>{"done"!==s.status&&(s.subscription=t({input:s.input}).subscribe({next:t=>{i._parent?.send(t)},error:t=>{i.send({type:e,data:t})},complete:()=>{i.send({type:n})}}))},getSnapshot:t=>{},getPersistedState:({status:t,data:e,input:n})=>({status:t,data:e,input:n}),getStatus:t=>t,restoreState:t=>({...t,subscription:void 0})}},t.fromObservable=function(t){const e="$$xstate.next",n="$$xstate.error",s="$$xstate.complete";return{config:t,transition:(t,i,{self:o,id:r,defer:a})=>{if("active"!==t.status)return t;switch(i.type){case e:return a((()=>{o._parent?.send({type:`xstate.snapshot.${r}`,data:i.data})})),{...t,data:i.data};case n:return{...t,status:"error",input:void 0,data:i.data,subscription:void 0};case s:return{...t,status:"done",input:void 0,subscription:void 0};case z:return t.subscription.unsubscribe(),{...t,status:"canceled",input:void 0,subscription:void 0};default:return t}},getInitialState:(t,e)=>({subscription:void 0,status:"active",data:void 0,input:e}),start:(i,{self:o})=>{"done"!==i.status&&(i.subscription=t({input:i.input}).subscribe({next:t=>{o.send({type:e,data:t})},error:t=>{o.send({type:n,data:t})},complete:()=>{o.send({type:s})}}))},getSnapshot:t=>t.data,getPersistedState:({status:t,data:e,input:n})=>({status:t,data:e,input:n}),getStatus:t=>t,restoreState:t=>({...t,subscription:void 0})}},t.fromPromise=function(t){const e="$$xstate.resolve",n="$$xstate.reject";return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case e:return{...t,status:"done",data:s.data,input:void 0};case n:return{...t,status:"error",data:s.data,input:void 0};case z:return{...t,status:"canceled",input:void 0};default:return t}},start:(s,{self:i})=>{if("active"!==s.status)return;Promise.resolve(t({input:s.input})).then((t=>{"active"===i._state.status&&i.send({type:e,data:t})}),(t=>{"active"===i._state.status&&i.send({type:n,data:t})}))},getInitialState:(t,e)=>({status:"active",data:void 0,input:e}),getSnapshot:t=>t.data,getStatus:t=>t,getPersistedState:t=>t,restoreState:t=>t}},t.fromTransition=function(t,e){return{config:t,transition:(e,n,s)=>t(e,n,s),getInitialState:(t,n)=>"function"==typeof e?e({input:n}):e,getSnapshot:t=>t,getPersistedState:t=>t,restoreState:t=>t}},t.getStateNodes=bt,t.interpret=X,t.log=function(t=K,e){return D({type:c,params:{label:e,expr:t}},((n,{state:s,actorContext:i})=>{const o="function"==typeof t?t({context:s.context,event:n,self:i?.self??{},system:i?.system}):t;return[s,{type:"xstate.log",params:{label:e,value:o},execute:t=>{e?t.logger?.(e,o):t.logger?.(o)}}]}))},t.mapState=function(t,e){let n;for(const s of Object.keys(t))b(s,e)&&(!n||e.length>n.length)&&(n=s);return t[n]},t.matchesState=b,t.not=function(t){return{type:"xstate.boolean",params:{op:"not"},children:[nt(t)],predicate:({evaluate:t,guard:e,context:n,event:s,state:i})=>!t(e.children[0],n,s,i)}},t.or=function(t){return{type:"xstate.boolean",params:{op:"or"},children:t.map((t=>nt(t))),predicate:({evaluate:t,guard:e,context:n,event:s,state:i})=>e.children.some((e=>t(e,n,s,i)))}},t.pathToStateValue=w,t.pure=function(t){return D({type:y,params:{get:t}},((e,{state:n})=>[n,{type:y,params:{actions:I(Xt(t({context:n.context,event:e})))??[]}}]))},t.raise=Lt,t.sendParent=function(t,e){return J(t,{...e,to:n.Parent})},t.sendTo=function(t,e,n){return J(e,{...n,to:t})},t.stateIn=function(t){return{type:"xstate.guard:in",params:{stateValue:t},predicate:({state:e})=>C(t)&&dt(t)?e.configuration.some((e=>e.id===t.slice(1))):e.matches(t)}},t.stop=H,t.toObserver=M,t.waitFor=function(t,e,n){const s={...se,...n};return new Promise(((n,i)=>{let o=!1;const r=s.timeout===1/0?void 0:setTimeout((()=>{u.unsubscribe(),i(new Error(`Timeout of ${s.timeout} ms exceeded`))}),s.timeout),a=()=>{clearTimeout(r),o=!0,u?.unsubscribe()};function c(t){e(t)&&(a(),n(t))}let u;c(t.getSnapshot()),o||(u=t.subscribe({next:c,error:t=>{a(),i(t)},complete:()=>{a(),i(new Error("Actor terminated without satisfying predicate"))}}),o&&u.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";let e=function(t){return t.Stop="xstate.stop",t.Raise="xstate.raise",t.Send="xstate.send",t.Cancel="xstate.cancel",t.Assign="xstate.assign",t.After="xstate.after",t.DoneState="done.state",t.DoneInvoke="done.invoke",t.Log="xstate.log",t.Init="xstate.init",t.Invoke="xstate.invoke",t.ErrorExecution="error.execution",t.ErrorCommunication="error.communication",t.ErrorPlatform="error.platform",t.ErrorCustom="xstate.error",t.Pure="xstate.pure",t.Choose="xstate.choose",t}({}),n=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});const s=e.Stop,i=e.Raise,o=e.Send,r=e.Cancel,a=e.Assign;e.After,e.DoneState;const c=e.Log,u=e.Init,h=e.Invoke,d=e.ErrorExecution,f=e.ErrorPlatform,p=e.ErrorCustom,l=e.Choose,y=e.Pure,v=".",m="",g="",x="*";function b(t,e,n=v){const s=S(t,n),i=S(e,n);return C(i)?!!C(s)&&i===s:C(s)?s in i:Object.keys(s).every((t=>t in i&&b(s[t],i[t])))}function _(t,e){try{return O(t)?t:t.toString().split(e)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}function S(t,e){if("object"==typeof(n=t)&&"value"in n&&"context"in n&&"event"in n)return t.value;var n;if(O(t))return w(t);if("string"!=typeof t)return t;return w(_(t,e))}function w(t){if(1===t.length)return t[0];const e={};let n=e;for(let e=0;e<t.length-1;e++)e===t.length-2?n[t[e]]=t[e+1]:(n[t[e]]={},n=n[t[e]]);return e}function $(t,e){const n={},s=Object.keys(t);for(let i=0;i<s.length;i++){const o=s[i];n[o]=e(t[o],o,t,i)}return n}function k(t){return[].concat(...t)}function E(t){return O(t)?t:[t]}function I(t){return void 0===t?[]:E(t)}function T(t,e,n){if(j(t))return t({context:e,event:n});const s={},i={context:e,event:n};for(const e of Object.keys(t)){const n=t[e];j(n)?s[e]=n(i):s[e]=n}return s}function O(t){return Array.isArray(t)}function j(t){return"function"==typeof t}function C(t){return"string"==typeof t}function A(t,e){return E(e).map((e=>void 0===e||"string"==typeof e?{target:e,event:t}:{...e,event:t}))}function P(t){if(void 0!==t&&t!==m)return I(t)}function M(t,e,n){const s=()=>{},i="object"==typeof t,o=i?t:null;return{next:((i?t.next:t)||s).bind(o),error:((i?t.error:e)||s).bind(o),complete:((i?t.complete:n)||s).bind(o)}}function N(t){return t?"transition"in t?{src:t,input:void 0}:t:void 0}function D(t,e){return{type:t.type,params:t.params,resolve:e}}function R(t){return"object"==typeof t&&null!==t&&"resolve"in t}function J(t,e){return D({type:o,params:{to:e?e.to:void 0,delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:j(t)?t.name:t.type}},((s,{actorContext:i,state:r})=>{const a={to:e?e.to:void 0,delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:j(t)?t.name:t.type},c={context:r.context,event:s,self:i?.self??null,system:i?.system},u=r.machine.options.delays;if("string"==typeof t)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${t}" }) instead`);const h=j(t)?t(c):t;let d;if(C(a.delay)){const t=u&&u[a.delay];d=j(t)?t(c):t}else d=j(a.delay)?a.delay(c):a.delay;const f=j(a.to)?a.to(c):a.to;let l;if("string"==typeof f){if(l=f===n.Parent?i?.self._parent:f===n.Internal?i?.self:f.startsWith("#_")?r.children[f.slice(2)]:r.children[f],!l)throw new Error(`Unable to send event to actor '${f}' from machine '${r.machine.id}'.`)}else l=f||i?.self;const y={type:o,params:{...a,to:l,event:h,delay:d,internal:f===n.Internal},execute:t=>{const e=y;if("number"!=typeof e.params.delay){const n=e.params.to,s=e.params.event;t.defer((()=>{n.send(s.type===p?{type:`${Yt(t.self.id)}`,data:s.data}:e.params.event)}))}else t.self.delaySend(e)}};return[r,y]}))}class Q{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)}prepend(t){this._current?this._current={value:t,next:this._current}:this.enqueue(t)}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),t===this._current&&(this._current=this._current.next)}this._last=null}}const V="function"==typeof Symbol&&Symbol.observable||"@@observable";const B="xstate.init",z="xstate.stop";function U(t){return t===B||t===z}function W(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const q=t=>{const e=W();e&&e.register(t)};let L=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const F={deferEvents:!0,clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class G{constructor(t,e){this.behavior=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new Q(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=L.NotStarted,this._parent=void 0,this.ref=void 0,this._actorContext=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={...F,...e},{clock:s,logger:i,parent:o,id:r,systemId:a}=n;this.system=o?.system??function(){let t=0;const e=new Map,n=new Map,s=new WeakMap;return{_bookId:()=>"x:"+t++,_register:(t,n)=>(e.set(t,n),t),_unregister:t=>{e.delete(t.sessionId);const i=s.get(t);void 0!==i&&(n.delete(i),s.delete(t))},get:t=>n.get(t),_set:(t,e)=>{const i=n.get(t);if(i&&i!==e)throw new Error(`Actor with system ID '${t}' already exists.`);n.set(t,e),s.set(e,t)}}}(),a&&(this._systemId=a,this.system._set(a,this)),this.sessionId=this.system._bookId(),this.id=r??this.sessionId,this.logger=i,this.clock=s,this._parent=o,this.options=n,this.src=n.src,this.ref=this,this._actorContext={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()}},this.send=this.send.bind(this),this._initState()}_initState(){this._state=this.options.state?this.behavior.restoreState?this.behavior.restoreState(this.options.state,this._actorContext):this.options.state:this.behavior.getInitialState(this._actorContext,this.options?.input)}update(t){this._state=t;const e=this.getSnapshot();let n;for(;n=this._deferred.shift();)n(t);for(const t of this.observers)t.next?.(e);const s=this.behavior.getStatus?.(t);switch(s?.status){case"done":this._stopProcedure(),this._doneEvent=Kt(this.id,s.data),this._parent?.send(this._doneEvent),this._complete();break;case"error":this._stopProcedure(),this._parent?.send(Yt(this.id,s.data)),this._error(s.data)}}subscribe(t,e,n){const s=M(t,e,n);return this.observers.add(s),this.status===L.Stopped&&(s.complete?.(),this.observers.delete(s)),{unsubscribe:()=>{this.observers.delete(s)}}}start(){return this.status===L.Running||(this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=L.Running,this.behavior.start&&this.behavior.start(this._state,this._actorContext),this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start()),this}_process(t){try{const e=this.behavior.transition(this._state,t,this._actorContext);this.update(e),t.type===z&&(this._stopProcedure(),this._complete())}catch(t){if(!(this.observers.size>0))throw t;this.observers.forEach((e=>{e.error?.(t)})),this.stop()}}_stop(){return this.status===L.Stopped?this:(this.mailbox.clear(),this.status===L.NotStarted?(this.status=L.Stopped,this):(this.mailbox.enqueue({type:z}),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)t.complete?.();this.observers.clear()}_error(t){for(const e of this.observers)e.error?.(t);this.observers.clear()}_stopProcedure(){if(this.status!==L.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new Q(this._process.bind(this)),this.status=L.Stopped,this.system._unregister(this),this}send(t){if("string"==typeof t)throw new Error(`Only event objects may be sent to actors; use .send({ type: "${t}" }) instead`);if(this.status!==L.Stopped){if(this.status!==L.Running&&!this.options.deferEvents)throw new Error(`Event "${t.type}" was sent to uninitialized actor "${this.id}". Make sure .start() is called for this actor, or set { deferEvents: true } in the actor options.\nEvent: ${JSON.stringify(t)}`);this.mailbox.enqueue(t)}}delaySend(t){this.delayedEventsMap[t.params.id]=this.clock.setTimeout((()=>{"to"in t.params&&t.params.to?t.params.to.send(t.params.event):this.send(t.params.event)}),t.params.delay)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:q)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.behavior.getPersistedState?.(this._state)}[V](){return this}getSnapshot(){return this.behavior.getSnapshot?this.behavior.getSnapshot(this._state):this._state}}function X(t,e){return new G(t,e)}function H(t){const e=t;return D({type:s,params:{actor:e}},((t,{state:n})=>{const s=j(e)?e({context:n.context,event:t}):e,i="string"==typeof s?n.children[s]:s;return[n,{type:"xstate.stop",params:{actor:i},execute:t=>{i&&(i.status===L.Running?t.defer((()=>{t.stopChild(i)})):t.stopChild(i))}}]}))}const K=({context:t,event:e})=>({context:t,event:e});function Y(t){return D({type:r,params:{sendId:t}},((e,{state:n,actorContext:s})=>{const i=j(t)?t({context:n.context,event:e,self:s?.self??{},system:s?.system}):t;return[n,{type:"xstate.cancel",params:{sendId:i},execute:t=>{t.self.cancel(i)}}]}))}const Z=new WeakMap;function tt(t,e,n){let s=Z.get(t);return s?e in s||(s[e]=n()):(s={[e]:n()},Z.set(t,s)),s[e]}function et(t,e,n,s){const{machine:i}=s,o=i?.options?.guards?.[t.type]??t.predicate;if(!o)throw new Error(`Guard '${t.type}' is not implemented.'.`);return o({context:e,event:n,state:s,guard:t,evaluate:et})}function nt(t,e){return C(t)?{type:t,predicate:e?.(t)||void 0,params:{type:t}}:j(t)?{type:t.name,predicate:t,params:{type:t.name,name:t.name}}:{type:t.type,params:t.params||t,children:t.children?.map((t=>nt(t,e))),predicate:e?.(t.type)||t.predicate}}const st=t=>"atomic"===t.type||"final"===t.type;function it(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function ot(t,e){const n=[];let s=t.parent;for(;s&&s!==e;)n.push(s),s=s.parent;return n}function rt(t){const e=new Set(t),n=new Set(t),s=ct(n);for(const t of e)if("compound"!==t.type||s.get(t)&&s.get(t).length){if("parallel"===t.type)for(const e of it(t))if("history"!==e.type&&!n.has(e))for(const t of mt(e))n.add(t)}else mt(t).forEach((t=>n.add(t)));for(const t of n){let e=t.parent;for(;e;)n.add(e),e=e.parent}return n}function at(t,e){const n=e.get(t);if(!n)return{};if("compound"===t.type){const t=n[0];if(!t)return{};if(st(t))return t.key}const s={};for(const t of n)s[t.key]=at(t,e);return s}function ct(t){const e=new Map;for(const n of t)e.has(n)||e.set(n,[]),n.parent&&(e.has(n.parent)||e.set(n.parent,[]),e.get(n.parent).push(n));return e}function ut(t,e){return at(t,ct(rt(e)))}function ht(t,e=t[0].machine.root){return"compound"===e.type?it(e).some((e=>"final"===e.type&&t.includes(e))):"parallel"===e.type&&it(e).every((e=>ht(t,e)))}const dt=t=>"#"===t[0];function ft(t){const n=t.config.after;if(!n)return[];const s=(n,s)=>{const i=function(t,n){const s=n?`#${n}`:"";return`${e.After}(${t})${s}`}(j(n)?`${t.id}:delay[${s}]`:n,t.id);return t.entry.push(Lt({type:i},{delay:n})),t.exit.push(Y(i)),i};return(O(n)?n.map(((t,e)=>{const n=s(t.delay,e);return{...t,event:n}})):Object.keys(n).flatMap(((t,e)=>{const i=n[t],o=C(i)?{target:i}:i,r=isNaN(+t)?t:+t,a=s(r,e);return I(o).map((t=>({...t,event:a,delay:r})))}))).map((e=>{const{delay:n}=e;return{...pt(t,e),delay:n}}))}function pt(t,e){const n=P(e.target),s=e.reenter??!1,{guards:i}=t.machine.options,o=lt(t,n),r={...e,actions:Xt(I(e.actions)),guard:e.guard?nt(e.guard,(t=>i[t])):void 0,target:o,source:t,reenter:s,eventType:e.event,toJSON:()=>({...r,source:`#${t.id}`,target:o?o.map((t=>`#${t.id}`)):void 0})};return r}function lt(t,e){if(void 0!==e)return e.map((e=>{if(!C(e))return e;if(dt(e))return t.machine.getStateNodeById(e);const n=e[0]===t.machine.delimiter;if(n&&!t.parent)return xt(t,e.slice(1));const s=n?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 xt(t.parent,s)}catch(e){throw new Error(`Invalid transition definition for state node '${t.id}':\n${e.message}`)}}))}function yt(t){const e=P(t.target);return e?e.map((e=>"string"==typeof e?xt(t.parent,e):e)):t.parent.initial.target}function vt(t){return"history"===t.type}function mt(t){const e=new Set;return function n(s){if(!e.has(s))if(e.add(s),"compound"===s.type)for(const i of s.initial.target){for(const n of ot(i,t))e.add(n);n(i)}else if("parallel"===s.type)for(const t of it(s))n(t)}(t),[...e]}function gt(t,e){if(dt(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 xt(t,e){if("string"==typeof e&&dt(e))try{return t.machine.getStateNodeById(e)}catch(t){}const n=_(e,t.machine.delimiter).slice();let s=t;for(;n.length;){const t=n.shift();if(!t.length)break;s=gt(s,t)}return s}function bt(t,e){const n=e instanceof zt?e.value:S(e,t.machine.delimiter);if(C(n))return[t,t.states[n]];const s=Object.keys(n),i=s.map((e=>gt(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,s.reduce(((e,s)=>{const i=gt(t,s);if(!i)return e;const o=bt(i,n[s]);return e.concat(o)}),[]))}function _t(t,e,n,s){return C(e)?function(t,e,n,s){const i=gt(t,e).next(n,s);return i&&i.length?i:t.next(n,s)}(t,e,n,s):1===Object.keys(e).length?function(t,e,n,s){const i=Object.keys(e),o=_t(gt(t,i[0]),e[i[0]],n,s);return o&&o.length?o:t.next(n,s)}(t,e,n,s):function(t,e,n,s){const i=[];for(const o of Object.keys(e)){const r=e[o];if(!r)continue;const a=_t(gt(t,o),r,n,s);a&&i.push(...a)}return i.length?i:t.next(n,s)}(t,e,n,s)}function St(t){return Object.keys(t.states).map((e=>t.states[e])).filter((t=>"history"===t.type))}function wt(t,e){let n=t;for(;n.parent&&n.parent!==e;)n=n.parent;return n.parent===e}function $t(t){const e=[];let n=t.parent;for(;n;)e.unshift(n),n=n.parent;return e}function kt(t,e){const n=new Set(t),s=new Set(e);for(const t of n)if(s.has(t))return!0;for(const t of s)if(n.has(t))return!0;return!1}function Et(t,e,n){const s=new Set;for(const i of t){let t=!1;const o=new Set;for(const r of s)if(kt(Ot([i],e,n),Ot([r],e,n))){if(!wt(i.source,r.source)){t=!0;break}o.add(r)}if(!t){for(const t of o)s.delete(t);s.add(i)}}return Array.from(s)}function It(t,e){if(!t.target)return[];const n=new Set;for(const s of t.target)if(vt(s))if(e[s.id])for(const t of e[s.id])n.add(t);else for(const t of It({target:yt(s)},e))n.add(t);else n.add(s);return[...n]}function Tt(t,e){const n=It(t,e);if(!n)return null;if(!t.reenter&&"parallel"!==t.source.type&&n.every((e=>wt(e,t.source))))return t.source;return function(t){const[e]=t;let n=$t(e),s=[];for(const e of t){const t=$t(e);s=n.filter((e=>t.includes(e))),n=s,s=[]}return n[n.length-1]}(n.concat(t.source))}function Ot(t,e,n){const s=new Set;for(const i of t)if(i.target?.length){const t=Tt(i,n);for(const n of e)wt(n,t)&&s.add(n)}return[...s]}function jt(t,e,n,i){const{machine:o}=e,r=e._initial||t.length>0,a=new Set(e.configuration);if(!e._initial&&!r){const t=Ut(e,{event:i,actions:[],transitions:[]});return t.changed=!1,t}const c=function(t,e,n,s,i){const o=[],r={...e.historyValue},a=Et(t,n,r),c=[...e._internalQueue];e._initial||function(t,e,n,s){const i=Ot(t,e,n);i.sort(((t,e)=>e.order-t.order));for(const t of i)for(const s of St(t)){let i;i="deep"===s.history?e=>st(e)&&wt(e,t):e=>e.parent===t,n[s.id]=Array.from(e).filter(i)}for(const t of i)s.push(...t.exit.flat(),...t.invoke.map((t=>H(t.id)))),e.delete(t)}(a,n,r,o);o.push(...a.flatMap((t=>t.actions))),Ct(a,n,o,c,e,r);const u=[...n],h=ht(u);if(h){const t=u.sort(((t,e)=>e.order-t.order)).flatMap((t=>t.exit));o.push(...t)}try{const{nextState:t}=Nt(o,s,e,i),n=h?function(t,e,n){const s=t[0].machine,i=t.find((t=>"final"===t.type&&t.parent===s.root));return i&&i.output?T(i.output,e,n):void 0}(u,t.context,s):void 0;return c.push(...t._internalQueue),Ut(e,{actions:t.actions,configuration:u,historyValue:r,_internalQueue:c,context:t.context,event:s,done:h,output:n,children:t.children})}catch(t){throw t}}(e._initial?[{target:[...e.configuration].filter(st),source:o.root,reenter:!0,actions:[],eventType:null,toJSON:null}]:t,e,a,i,n),{context:u,actions:d}=c,f=function(t,e){const n={...t.children};for(const t of e)if(t.type===h&&t.params.ref){const e=t.params.ref;e&&(n[e.id]=e)}else if(t.type===s){const e=t.params.actor;e&&delete n[e.id]}return n}(e,d),p=Ut(c,{value:{},transitions:t,children:f});return p.changed=e._initial?void 0:!Bt(p.value,e.value)||p.actions.length>0||u!==e.context,p}function Ct(t,e,n,s,i,o){const r=new Set,a=new Set;At(t,o,a,r),i._initial&&a.add(i.machine.root);for(const t of[...r].sort(((t,e)=>t.order-e.order))){e.add(t);for(const e of t.invoke)n.push(Wt(e));if(n.push(...t.entry),a.has(t))for(const t of a){const e=t.initial.actions;n.push(...e)}if("final"===t.type){const n=t.parent;if(!n.parent)continue;if(s.push(Ht(n.id,t.output?T(t.output,i.context,i.event):void 0)),n.parent){const t=n.parent;"parallel"===t.type&&it(t).every((t=>ht([...e],t)))&&s.push(Ht(t.id))}}}}function At(t,e,n,s){for(const i of t){for(const t of i.target||[])Pt(t,e,n,s);const t=Tt(i,e),o=It(i,e);for(const i of o)Mt(i,t,s,e,n)}}function Pt(t,e,n,s){if(vt(t))if(e[t.id]){const i=e[t.id];for(const t of i)Pt(t,e,n,s);for(const o of i){Mt(o,t.parent,s,e,n);for(const t of n)n.add(t)}}else{const i=yt(t);for(const t of i)Pt(t,e,n,s);for(const o of i){Mt(o,t,s,e,n);for(const t of n)n.add(t)}}else if(s.add(t),"compound"===t.type){n.add(t);const i=t.initial.target;for(const t of i)Pt(t,e,n,s);for(const o of i)Mt(o,t,s,e,n)}else if("parallel"===t.type)for(const i of it(t).filter((t=>!vt(t))))[...s].some((t=>wt(t,i)))||Pt(i,e,n,s)}function Mt(t,e,n,s,i){const o=ot(t,e);for(const t of o)if(n.add(t),"parallel"===t.type)for(const e of it(t).filter((t=>!vt(t))))[...n].some((t=>wt(t,e)))||Pt(e,s,i,n)}function Nt(t,e,n,s){const{machine:r}=n,a=[],c=[];let u=n;function h(t){a.push(t),s?.self.status===L.Running&&(t.execute?.(s),delete t.execute)}function d(t){const n=function(t,e){if(R(t))return t;const n=e[t.type];return"function"==typeof n?D({type:"xstate.function",params:t.params??{}},((e,{state:s})=>{const i={type:t.type,params:t.params,execute:t=>n({context:s.context,event:e,action:i,system:t.system,self:t.self})};return[s,i]})):n||t}(t,r.options.actions);if(R(n)){const[r,a]=n.resolve(e,{state:u,action:t,actorContext:s}),f=a.params?.actions;return u=r,(a.type===i||a.type===o&&a.params.internal)&&"number"!=typeof a.params.delay&&c.push(a),a.type!==y&&h(a),void Xt(f).forEach(d)}h(n)}for(const e of t)d(e);return{nextState:Ut(u,{actions:a,_internalQueue:c.map((t=>t.params.event))})}}function Dt(t,e,n){let s=t;const i=[];if(e.type===z)return s=Rt(e,s,n),i.push(s),{state:s,microstates:i};if(e.type!==u){s=jt(Jt(e,s),t,n,e),i.push(s)}for(;!s.done;){let t=Qt(s);if(0===t.length){if(s.configuration.some((t=>t.always))&&(s.transitions=[]),!s._internalQueue.length)break;{const t=s.actions,e=s._internalQueue[0];s=jt(Jt(e,s),s,n,e),s._internalQueue.shift(),s.actions.unshift(...t),i.push(s)}}if(t.length){const e=s.actions;s=jt(t,s,n,s.event),s.actions.unshift(...e),i.push(s)}}return s.done&&Rt(s.event,s,n),{state:s,microstates:i}}function Rt(t,e,n){const s=[];for(const t of e.configuration.sort(((t,e)=>e.order-t.order)))s.push(...t.exit);for(const t of Object.values(e.children))s.push(H(t));const{nextState:i}=Nt(s,t,e,n);return i}function Jt(t,e){return e.machine.getTransitionData(e,t)}function Qt(t){const e=new Set,n=t.configuration.filter(st);for(const s of n)t:for(const n of[s].concat(ot(s,null)))if(n.always)for(const s of n.always)if(void 0===s.guard||et(s.guard,t.context,t.event,t)){e.add(s);break t}return Et(Array.from(e),new Set(t.configuration),t.historyValue)}function Vt(t,e){return ut(t,[...rt(bt(t,e))])}function Bt(t,e){if(t===e)return!0;if(void 0===t||void 0===e)return!1;if(C(t)||C(e))return t===e;const n=Object.keys(t),s=Object.keys(e);return n.length===s.length&&n.every((n=>Bt(t[n],e[n])))}class zt{static from(t,e={},n){if(t instanceof zt)return t.context!==e?new zt({value:t.value,context:e,event:t.event,actions:[],meta:{},configuration:[],transitions:[],children:{}},n):t;const s=Zt({}),i=rt(bt(n.root,t));return new zt({value:t,context:e,event:s,actions:[],meta:void 0,configuration:Array.from(i),transitions:[],children:{}},n)}constructor(t,e){this.machine=e,this.tags=void 0,this.value=void 0,this.done=void 0,this.output=void 0,this.context=void 0,this.historyValue={},this.actions=[],this.event=void 0,this._internalQueue=void 0,this._initial=!1,this.changed=void 0,this.configuration=void 0,this.transitions=void 0,this.children=void 0,this.context=t.context,this._internalQueue=t._internalQueue??[],this.event=t.event,this.historyValue=t.historyValue||{},this.actions=t.actions??[],this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=t.configuration??Array.from(rt(bt(e.root,t.value))),this.transitions=t.transitions,this.children=t.children,this.value=ut(e.root,this.configuration),this.tags=new Set(k(this.configuration.map((t=>t.tags)))),this.done=t.done??!1,this.output=t.output}toStrings(t=this.value,e="."){if(C(t))return[t];const n=Object.keys(t);return n.concat(...n.map((n=>this.toStrings(t[n],e).map((t=>n+e+t)))))}toJSON(){const{configuration:t,transitions:e,tags:n,machine:s,...i}=this;return{...i,tags:Array.from(n),meta:this.meta}}matches(t){return b(t,this.value)}hasTag(t){return this.tags.has(t)}can(t){const e=this.machine.getTransitionData(this,t);return!!e?.length&&e.some((t=>void 0!==t.target||t.actions.length))}get nextEvents(){return tt(this,"nextEvents",(()=>[...new Set(k([...this.configuration.map((t=>t.ownEvents))]))]))}get meta(){return this.configuration.reduce(((t,e)=>(void 0!==e.meta&&(t[e.id]=e.meta),t)),{})}}function Ut(t,e={}){return new zt({...t,...e},t.machine)}function Wt(t){return D({type:h,params:t},((e,{state:n,actorContext:s})=>{const i=h,{id:o,src:r}=t;let a;if((c=r)&&"object"==typeof c&&"function"==typeof c.send)a={type:i,params:{...t,ref:r}};else{const c=N(n.machine.options.actors[r]);if(c){const u="input"in t?t.input:c.input,h=X(c.src,{id:o,src:r,parent:s?.self,systemId:t.systemId,input:"function"==typeof u?u({context:n.context,event:e,self:s?.self}):u});a={type:i,params:{...t,ref:h}}}else a={type:i,params:t}}var c;const u=a.params.ref,d=Ut(n,{children:{...n.children,[o]:u}});return a.execute=t=>{const e=t.self,{id:n,ref:s}=a.params;s&&t.defer((()=>{if(u.status!==L.Stopped)try{u.start?.()}catch(t){return void e.send(Yt(n,t))}}))},[d,a]}))}function qt(t,e,n,s,i){return(o,r={})=>{const{systemId:a}=r;if(C(o)){const c=N(e.options.actors[o]);if(c){const e=r.id??"anon",o="input"in r?r.input:c.input,u=X(c.src,{id:e,parent:t,input:"function"==typeof o?o({context:n,event:s,self:t}):o});return i.push(Wt({id:u.id,src:u,ref:u,meta:void 0,input:o,systemId:a})),u}throw new Error(`Behavior '${o}' not implemented in machine '${e.id}'`)}{const e=X(o,{id:r.id||"anonymous",parent:t,input:r.input,systemId:a});return i.push(Wt({src:e,ref:e,id:e.id,meta:void 0,input:r.input})),e}}}function Lt(t,e){return D({type:i,params:{delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:"function"==typeof t?t.name:t.type}},((n,{state:s,actorContext:o})=>{const r={delay:e?e.delay:void 0,event:t,id:e&&void 0!==e.id?e.id:"function"==typeof t?t.name:t.type},a={context:s.context,event:n,self:o?.self??{},system:o?.system},c=s.machine.options.delays;if("string"==typeof t)throw new Error(`Only event objects may be used with raise; use raise({ type: "${t}" }) instead`);const u="function"==typeof t?t(a):t;let h;if("string"==typeof r.delay){const t=c&&c[r.delay];h="function"==typeof t?t(a):t}else h="function"==typeof r.delay?r.delay(a):r.delay;const d={type:i,params:{...r,event:u,delay:h},execute:t=>{"number"!=typeof d.params.delay||t.self.delaySend(d)}};return[s,d]}))}const Ft={type:u};function Gt(t){if(R(t))return t;if("string"==typeof t)return{type:t,params:{}};if("function"==typeof t){const e="xstate.function";return D({type:e,params:{}},((n,{state:s})=>{const i={type:e,params:{function:t},execute:e=>t({context:s.context,event:n,action:i,self:e.self,system:e.system})};return[s,i]}))}return t}const Xt=t=>{if(!t)return[];return(O(t)?t:[t]).map(Gt)};function Ht(t,n){const s=`${e.DoneState}.${t}`,i={type:s,output:n,toString:()=>s};return i}function Kt(t,n){const s=`${e.DoneInvoke}.${t}`,i={type:s,output:n,toString:()=>s};return i}function Yt(t,n){const s=`${e.ErrorPlatform}.${t}`,i={type:s,data:n,toString:()=>s};return i}function Zt(t){return{type:u,input:t}}const te={};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.machine.delimiter),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?$(this.config.states,((t,e)=>new ee(t,{_parent:this,_key:e,_machine:this.machine}))):te,"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=Xt(this.config.entry),this.exit=Xt(this.config.exit),this.meta=this.config.meta,this.output="final"===this.type?this.config.output:void 0,this.tags=I(t.tags)}_initialize(){this.transitions=function(t){const e=[];if(Array.isArray(t.config.on))e.push(...t.config.on);else if(t.config.on){const{[x]:n=[],...s}=t.config.on;for(const t of Object.keys(s)){if(t===g)throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.');const n=A(t,s[t]);e.push(...n)}e.push(...A(x,n))}const n=t.config.onDone?A(String(Ht(t.id)),t.config.onDone):[],s=t.invoke.flatMap((t=>{const e=[];return t.onDone&&e.push(...A(`done.invoke.${t.id}`,t.onDone)),t.onError&&e.push(...A(`error.platform.${t.id}`,t.onError)),t.onSnapshot&&e.push(...A(`xstate.snapshot.${t.id}`,t.onSnapshot)),e})),i=t.after,o=[...n,...s,...e].flatMap((e=>I(e).map((e=>pt(t,e)))));for(const t of i)o.push(t);return o}(this),this.config.always&&(this.always=A(g,this.config.always).map((t=>pt(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,eventType:null,reenter:!1,toJSON:()=>({target:this.initial.target.map((t=>`#${t.id}`)),source:`#${this.id}`,actions:this.initial.actions,eventType:null})}:void 0,history:this.history,states:$(this.states,(t=>t.definition)),on:this.on,transitions:this.transitions,entry:this.entry,exit:this.exit,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 tt(this,"invoke",(()=>I(this.config.invoke).map(((t,e)=>{const n=`${this.id}:invocation[${e}]`;const s=function(t,e){if("object"==typeof t){if("src"in t)return t;if("transition"in t)return{id:e,src:t}}return{id:e,src:t}}(t,n),i=s.id||n,{src:o,systemId:r}=s,a=C(o)||"type"in o?o:i;return this.machine.options.actors[i]||"string"==typeof o||"type"in o||(this.machine.options.actors={...this.machine.options.actors,[i]:o}),{type:h,...s,src:a,id:i,systemId:r,toJSON(){const{onDone:t,onError:e,...n}=s;return{...n,type:h,src:a,id:i}}}}))))}get on(){return tt(this,"on",(()=>this.transitions.reduce(((t,e)=>(t[e.eventType]=t[e.eventType]||[],t[e.eventType].push(e),t)),{})))}get after(){return tt(this,"delayedTransitions",(()=>ft(this)))}get initial(){return tt(this,"initial",(()=>function(t,e){if(C(e)||O(e)){const n=I(e).map((e=>{const n=C(e)?dt(e)?t.machine.getStateNodeById(e):t.states[e]:e;if(!n)throw new Error(`Initial state node "${e}" not found on parent state node #${t.id}`);if(!wt(n,t))throw new Error(`Invalid initial target: state node #${n.id} is not a descendant of #${t.id}`);return n})),s=lt(t,n),i={source:t,actions:[],eventType:null,reenter:!1,target:s,toJSON:()=>({...i,source:`#${t.id}`,target:s?s.map((t=>`#${t.id}`)):void 0})};return i}return pt(t,{target:I(e.target).map((e=>C(e)?dt(e)?e:`${t.machine.delimiter}${e}`:e)),actions:e.actions,event:null})}(this,this.config.initial||[])))}next(t,e){const n=e.type,s=[];let i;const o=tt(this,`candidates-${n}`,(()=>{return t=n,this.transitions.filter((e=>{const{eventType:n}=e;if(n===t)return!0;if(n===x)return!0;if(!n.endsWith(".*"))return!1;const s=n.split("."),i=t.split(".");for(let t=0;t<s.length;t++){const e=s[t],n=i[t];if("*"===e)return t===s.length-1;if(e!==n)return!1}return!0}));var t}));for(const r of o){const{guard:o}=r,a=t.context;let c=!1;try{c=!o||et(o,a,e,t)}catch(t){throw new Error(`Unable to evaluate guard '${o.type}' in transition for event '${n}' in state node '${this.id}':\n${t.message}`)}if(c){s.push(...r.actions),i=r;break}}return i?[i]:void 0}get target(){if("history"===this.type){return this.config.target}}get stateIds(){const t=k(Object.keys(this.states).map((t=>this.states[t].stateIds)));return[this.id].concat(t)}get events(){return tt(this,"events",(()=>{const{states:t}=this,e=new Set(this.ownEvents);if(t)for(const n of Object.keys(t)){const s=t[n];if(s.states)for(const t of s.events)e.add(`${t}`)}return Array.from(e)}))}get ownEvents(){const t=new Set(this.transitions.filter((t=>!(!t.target&&!t.actions.length&&!t.reenter))).map((t=>t.eventType)));return Array.from(t)}}class ne{getContext(t){return this.getContextAndActions(void 0,t)[0]}getContextAndActions(t,e){const n=[],{context:s}=this.config;return[("function"==typeof s?s({spawn:qt(t?.self,this,void 0,Zt(e),n),input:e}):s)||{},n]}constructor(t,e){this.config=t,this.version=void 0,this.delimiter=void 0,this.options=void 0,this.types=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.__TContext=void 0,this.__TEvent=void 0,this.__TAction=void 0,this.__TActorMap=void 0,this.__TResolvedTypesMeta=void 0,this.id=t.id||"(machine)",this.options=Object.assign({actions:{},actors:{},delays:{},guards:{},context:{}},e),this.delimiter=this.config.delimiter||v,this.version=this.config.version,this.types=this.config.types??{},this.transition=this.transition.bind(this),this.root=new ee(t,{_key:this.id,_machine:this}),this.root._initialize(),this.states=this.root.states,this.events=this.root.events}provide(t){const{actions:e,guards:n,actors:s,delays:i}=this.options;return new ne(this.config,{actions:{...e,...t.actions},guards:{...n,...t.guards},actors:{...s,...t.actors},delays:{...i,...t.delays}})}resolveState(t){const e=rt(bt(this.root,t.value)),n=Array.from(e);return this.createState({...t,value:Vt(this.root,t.value),configuration:n,done:ht(n)})}resolveStateValue(t){const e=Vt(this.root,t),n=this.getContext();return this.resolveState(zt.from(e,n,this))}transition(t=this.initialState,e,n){const s=t instanceof zt?t:this.resolveStateValue(t);if(function(t){return"string"==typeof t.type&&(t.type===d||t.type.startsWith(f))}(e)&&!s.nextEvents.some((t=>t===e.type)))throw e.data;const{state:i}=Dt(s,e,n);return i}microstep(t=this.initialState,e,n){return Dt(t,e,n).microstates}getTransitionData(t,e){return _t(this.root,t.value,t,e)||[]}getPreInitialState(t,e){const[n,s]=this.getContextAndActions(t,e),i=function(t){const e=[],n=t.initial,s=new Set;At([n],{},new Set([t]),s);for(const t of[...s].sort(((t,e)=>t.order-e.order)))e.push(t);return e}(this.root),o=this.resolveState(this.createState({value:{},context:n,event:Zt({}),actions:[],meta:void 0,configuration:i,transitions:[],children:{}}));if(o._initial=!0,o.actions.unshift(...s),t){const{nextState:e}=Nt(s,Ft,o,t);o.children=e.children,o.actions=e.actions}return o}get initialState(){return this.getInitialState()}getInitialState(t,e){const n=Zt(e),s=this.getPreInitialState(t,e),i=jt([],s,t,n);i.actions.unshift(...s.actions);const{state:o}=Dt(i,n,t);return o}start(t,e){t.actions.forEach((t=>{t.execute?.(e)})),Object.values(t.children).forEach((t=>{if(0===t.status)try{t.start?.()}catch(n){e.self.send(Yt(t.id,n))}}))}getStateNodeById(t){const e=t.split(this.delimiter),n=e.slice(1),s=dt(e[0])?e[0].slice(1):e[0],i=this.idMap.get(s);if(!i)throw new Error(`Child state node '#${s}' does not exist on machine '${this.id}'`);return xt(i,n)}get definition(){return{context:this.getContext(),...this.root.definition}}toJSON(){return this.definition}getPersistedState(t){return function(t){const{configuration:e,transitions:n,tags:s,machine:i,children:o,...r}=t,a={};for(const t in o)a[t]={state:o[t].getPersistedState?.(),src:o[t].src};return{...r,children:a}}(t)}createState(t){const e=t instanceof zt?t:new zt(t,this),{nextState:n}=Nt(e.actions,e.event,e,void 0);return n}getStatus(t){return t.done?{status:"done",data:t.output}:{status:"active"}}restoreState(t,e){const n={};Object.keys(t.children).forEach((s=>{const i=t.children[s],o=i.state,r=i.src,a=r?N(this.options.actors[r])?.src:void 0;if(!a)return;const c=a.restoreState?.(o,e),u=X(a,{id:s,state:c});n[s]=u}));const s=this.createState(new zt({...t,children:n},this));return s.configuration.forEach((t=>{t.invoke&&t.invoke.forEach((t=>{const{id:s,src:i}=t;if(n[s])return;const o=N(this.options.actors[i]);if(o){const i=X(o.src,{id:s,parent:e?.self,input:"input"in t?t.input:o.input});n[s]=i}}))})),s.actions=[],s}}const se={timeout:1e4};t.ActionTypes=e,t.Interpreter=G,t.InterpreterStatus=L,t.SimulatedClock=class{constructor(){this.timeouts=new Map,this._now=0,this._id=0}now(){return this._now}getId(){return this._id++}setTimeout(t,e){const n=this.getId();return this.timeouts.set(n,{start:this.now(),timeout:e,fn:t}),n}clearTimeout(t){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(){[...this.timeouts].sort((([t,e],[n,s])=>{const i=e.start+e.timeout;return s.start+s.timeout>i?-1:1})).forEach((([t,e])=>{this.now()-e.start>=e.timeout&&(this.timeouts.delete(t),e.fn.call(null))}))}increment(t){this._now+=t,this.flushTimeouts()}},t.SpecialTargets=n,t.State=zt,t.StateMachine=ne,t.StateNode=ee,t.and=function(t){return{type:"xstate.boolean",params:{op:"and"},children:t.map((t=>nt(t))),predicate:({evaluate:t,guard:e,context:n,event:s,state:i})=>e.children.every((e=>t(e,n,s,i)))}},t.assign=function(t){return D({type:a,params:{assignment:t}},((e,{state:n,action:s,actorContext:i})=>{const o=[];if(!n.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const r={context:n.context,event:e,action:s,spawn:qt(i?.self,n.machine,n.context,e,o),self:i?.self??{},system:i?.system};let c={};if(j(t))c=t(r);else for(const e of Object.keys(t)){const n=t[e];c[e]=j(n)?n(r):n}const u=Object.assign({},n.context,c);return[Ut(n,{context:u}),{type:a,params:{context:u,actions:o}}]}))},t.cancel=Y,t.choose=function(t){return D({type:l,params:{guards:t}},((e,{state:n})=>{const s=t.find((t=>{const s=t.guard&&nt(t.guard,(t=>n.machine.options.guards[t]));return!s||et(s,n.context,e,n)}))?.actions;return[n,{type:l,params:{actions:Xt(s)}}]}))},t.createMachine=function(t,e){return new ne(t,e)},t.doneInvoke=Kt,t.forwardTo=function(t,e){return J((({event:t})=>t),{...e,to:t})},t.fromCallback=function(t){return{config:t,start:(t,{self:e})=>{e.send({type:B})},transition:(e,n,{self:s,id:i})=>{if(n.type===B){const n=t=>{e.canceled||s._parent?.send(t)},r=t=>{e.receivers.add(t)};return e.dispose=t(n,r,{input:e.input}),((o=e.dispose)instanceof Promise||null!==o&&(j(o)||"object"==typeof o)&&j(o.then))&&e.dispose.then((t=>{s._parent?.send(Kt(i,t)),e.canceled=!0}),(t=>{e.canceled=!0,s._parent?.send(Yt(i,t))})),e}var o;return n.type===z?(e.canceled=!0,j(e.dispose)&&e.dispose(),e):(U(n.type)||U(n.type)||e.receivers.forEach((t=>t(n))),e)},getInitialState:(t,e)=>({canceled:!1,receivers:new Set,dispose:void 0,input:e}),getSnapshot:()=>{},getPersistedState:({input:t})=>t}},t.fromEventObservable=function(t){const e="$$xstate.error",n="$$xstate.complete";return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case e:return{...t,status:"error",input:void 0,data:s.data,subscription:void 0};case n:return{...t,status:"done",input:void 0,subscription:void 0};case z:return t.subscription.unsubscribe(),{...t,status:"canceled",input:void 0,subscription:void 0};default:return t}},getInitialState:(t,e)=>({subscription:void 0,status:"active",data:void 0,input:e}),start:(s,{self:i})=>{"done"!==s.status&&(s.subscription=t({input:s.input}).subscribe({next:t=>{i._parent?.send(t)},error:t=>{i.send({type:e,data:t})},complete:()=>{i.send({type:n})}}))},getSnapshot:t=>{},getPersistedState:({status:t,data:e,input:n})=>({status:t,data:e,input:n}),getStatus:t=>t,restoreState:t=>({...t,subscription:void 0})}},t.fromObservable=function(t){const e="$$xstate.next",n="$$xstate.error",s="$$xstate.complete";return{config:t,transition:(t,i,{self:o,id:r,defer:a})=>{if("active"!==t.status)return t;switch(i.type){case e:return a((()=>{o._parent?.send({type:`xstate.snapshot.${r}`,data:i.data})})),{...t,data:i.data};case n:return{...t,status:"error",input:void 0,data:i.data,subscription:void 0};case s:return{...t,status:"done",input:void 0,subscription:void 0};case z:return t.subscription.unsubscribe(),{...t,status:"canceled",input:void 0,subscription:void 0};default:return t}},getInitialState:(t,e)=>({subscription:void 0,status:"active",data:void 0,input:e}),start:(i,{self:o})=>{"done"!==i.status&&(i.subscription=t({input:i.input}).subscribe({next:t=>{o.send({type:e,data:t})},error:t=>{o.send({type:n,data:t})},complete:()=>{o.send({type:s})}}))},getSnapshot:t=>t.data,getPersistedState:({status:t,data:e,input:n})=>({status:t,data:e,input:n}),getStatus:t=>t,restoreState:t=>({...t,subscription:void 0})}},t.fromPromise=function(t){const e="$$xstate.resolve",n="$$xstate.reject";return{config:t,transition:(t,s)=>{if("active"!==t.status)return t;switch(s.type){case e:return{...t,status:"done",data:s.data,input:void 0};case n:return{...t,status:"error",data:s.data,input:void 0};case z:return{...t,status:"canceled",input:void 0};default:return t}},start:(s,{self:i})=>{if("active"!==s.status)return;Promise.resolve(t({input:s.input})).then((t=>{"active"===i._state.status&&i.send({type:e,data:t})}),(t=>{"active"===i._state.status&&i.send({type:n,data:t})}))},getInitialState:(t,e)=>({status:"active",data:void 0,input:e}),getSnapshot:t=>t.data,getStatus:t=>t,getPersistedState:t=>t,restoreState:t=>t}},t.fromTransition=function(t,e){return{config:t,transition:(e,n,s)=>t(e,n,s),getInitialState:(t,n)=>"function"==typeof e?e({input:n}):e,getSnapshot:t=>t,getPersistedState:t=>t,restoreState:t=>t}},t.getStateNodes=bt,t.interpret=X,t.log=function(t=K,e){return D({type:c,params:{label:e,expr:t}},((n,{state:s,actorContext:i})=>{const o="function"==typeof t?t({context:s.context,event:n,self:i?.self??{},system:i?.system}):t;return[s,{type:"xstate.log",params:{label:e,value:o},execute:t=>{e?t.logger?.(e,o):t.logger?.(o)}}]}))},t.mapState=function(t,e){let n;for(const s of Object.keys(t))b(s,e)&&(!n||e.length>n.length)&&(n=s);return t[n]},t.matchesState=b,t.not=function(t){return{type:"xstate.boolean",params:{op:"not"},children:[nt(t)],predicate:({evaluate:t,guard:e,context:n,event:s,state:i})=>!t(e.children[0],n,s,i)}},t.or=function(t){return{type:"xstate.boolean",params:{op:"or"},children:t.map((t=>nt(t))),predicate:({evaluate:t,guard:e,context:n,event:s,state:i})=>e.children.some((e=>t(e,n,s,i)))}},t.pathToStateValue=w,t.pure=function(t){return D({type:y,params:{get:t}},((e,{state:n})=>[n,{type:y,params:{actions:I(Xt(t({context:n.context,event:e})))??[]}}]))},t.raise=Lt,t.sendParent=function(t,e){return J(t,{...e,to:n.Parent})},t.sendTo=function(t,e,n){return J(e,{...n,to:t})},t.stateIn=function(t){return{type:"xstate.guard:in",params:{stateValue:t},predicate:({state:e})=>C(t)&&dt(t)?e.configuration.some((e=>e.id===t.slice(1))):e.matches(t)}},t.stop=H,t.toObserver=M,t.waitFor=function(t,e,n){const s={...se,...n};return new Promise(((n,i)=>{let o=!1;const r=s.timeout===1/0?void 0:setTimeout((()=>{u.unsubscribe(),i(new Error(`Timeout of ${s.timeout} ms exceeded`))}),s.timeout),a=()=>{clearTimeout(r),o=!0,u?.unsubscribe()};function c(t){e(t)&&(a(),n(t))}let u;c(t.getSnapshot()),o||(u=t.subscribe({next:c,error:t=>{a(),i(t)},complete:()=>{a(),i(new Error("Actor terminated without satisfying predicate"))}}),o&&u.unsubscribe())}))},Object.defineProperty(t,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=xstate.umd.min.js.map