xstate 5.0.0-beta.18 → 5.0.0-beta.19

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 (31) 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.map +1 -1
  6. package/actors/dist/xstate-actors.cjs.js +1 -1
  7. package/actors/dist/xstate-actors.development.cjs.js +1 -1
  8. package/actors/dist/xstate-actors.development.esm.js +1 -1
  9. package/actors/dist/xstate-actors.esm.js +1 -1
  10. package/actors/dist/xstate-actors.umd.min.js +1 -1
  11. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  12. package/dist/{actions-5fb9f10d.cjs.js → actions-2d912781.cjs.js} +8 -4
  13. package/dist/{actions-4d6514d2.esm.js → actions-3b74fb92.esm.js} +8 -4
  14. package/dist/{actions-13190b25.development.esm.js → actions-72105f77.development.esm.js} +8 -4
  15. package/dist/{actions-40bd643f.development.cjs.js → actions-bce11b97.development.cjs.js} +8 -4
  16. package/dist/declarations/src/actors/callback.d.ts +4 -2
  17. package/dist/declarations/src/actors/observable.d.ts +8 -4
  18. package/dist/declarations/src/actors/promise.d.ts +5 -2
  19. package/dist/declarations/src/actors/transition.d.ts +6 -3
  20. package/dist/declarations/src/types.d.ts +9 -4
  21. package/dist/xstate.cjs.js +1 -1
  22. package/dist/xstate.development.cjs.js +1 -1
  23. package/dist/xstate.development.esm.js +2 -2
  24. package/dist/xstate.esm.js +2 -2
  25. package/dist/xstate.umd.min.js +1 -1
  26. package/dist/xstate.umd.min.js.map +1 -1
  27. package/guards/dist/xstate-guards.cjs.js +1 -1
  28. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  29. package/guards/dist/xstate-guards.development.esm.js +1 -1
  30. package/guards/dist/xstate-guards.esm.js +1 -1
  31. package/package.json +1 -1
@@ -370,7 +370,8 @@ promiseCreator) {
370
370
  }
371
371
  const resolvedPromise = Promise.resolve(promiseCreator({
372
372
  input: state.input,
373
- system
373
+ system,
374
+ self
374
375
  }));
375
376
  resolvedPromise.then(response => {
376
377
  // TODO: remove this condition once dead letter queue lands
@@ -483,7 +484,8 @@ function fromObservable(observableCreator) {
483
484
  }
484
485
  state.subscription = observableCreator({
485
486
  input: state.input,
486
- system
487
+ system,
488
+ self
487
489
  }).subscribe({
488
490
  next: value => {
489
491
  self.send({
@@ -589,7 +591,8 @@ function fromEventObservable(lazyObservable) {
589
591
  }
590
592
  state.subscription = lazyObservable({
591
593
  input: state.input,
592
- system
594
+ system,
595
+ self
593
596
  }).subscribe({
594
597
  next: value => {
595
598
  self._parent?.send(value);
@@ -832,7 +835,8 @@ function fromCallback(invokeCallback) {
832
835
  };
833
836
  state.dispose = invokeCallback(sender, receiver, {
834
837
  input: state.input,
835
- system
838
+ system,
839
+ self: self
836
840
  });
837
841
  if (isPromiseLike(state.dispose)) {
838
842
  state.dispose.then(resolved => {
@@ -386,7 +386,8 @@ promiseCreator) {
386
386
  }
387
387
  const resolvedPromise = Promise.resolve(promiseCreator({
388
388
  input: state.input,
389
- system
389
+ system,
390
+ self
390
391
  }));
391
392
  resolvedPromise.then(response => {
392
393
  // TODO: remove this condition once dead letter queue lands
@@ -499,7 +500,8 @@ function fromObservable(observableCreator) {
499
500
  }
500
501
  state.subscription = observableCreator({
501
502
  input: state.input,
502
- system
503
+ system,
504
+ self
503
505
  }).subscribe({
504
506
  next: value => {
505
507
  self.send({
@@ -605,7 +607,8 @@ function fromEventObservable(lazyObservable) {
605
607
  }
606
608
  state.subscription = lazyObservable({
607
609
  input: state.input,
608
- system
610
+ system,
611
+ self
609
612
  }).subscribe({
610
613
  next: value => {
611
614
  self._parent?.send(value);
@@ -848,7 +851,8 @@ function fromCallback(invokeCallback) {
848
851
  };
849
852
  state.dispose = invokeCallback(sender, receiver, {
850
853
  input: state.input,
851
- system
854
+ system,
855
+ self: self
852
856
  });
853
857
  if (isPromiseLike(state.dispose)) {
854
858
  state.dispose.then(resolved => {
@@ -388,7 +388,8 @@ promiseCreator) {
388
388
  }
389
389
  const resolvedPromise = Promise.resolve(promiseCreator({
390
390
  input: state.input,
391
- system
391
+ system,
392
+ self
392
393
  }));
393
394
  resolvedPromise.then(response => {
394
395
  // TODO: remove this condition once dead letter queue lands
@@ -501,7 +502,8 @@ function fromObservable(observableCreator) {
501
502
  }
502
503
  state.subscription = observableCreator({
503
504
  input: state.input,
504
- system
505
+ system,
506
+ self
505
507
  }).subscribe({
506
508
  next: value => {
507
509
  self.send({
@@ -607,7 +609,8 @@ function fromEventObservable(lazyObservable) {
607
609
  }
608
610
  state.subscription = lazyObservable({
609
611
  input: state.input,
610
- system
612
+ system,
613
+ self
611
614
  }).subscribe({
612
615
  next: value => {
613
616
  self._parent?.send(value);
@@ -850,7 +853,8 @@ function fromCallback(invokeCallback) {
850
853
  };
851
854
  state.dispose = invokeCallback(sender, receiver, {
852
855
  input: state.input,
853
- system
856
+ system,
857
+ self: self
854
858
  });
855
859
  if (isPromiseLike(state.dispose)) {
856
860
  state.dispose.then(resolved => {
@@ -1,8 +1,10 @@
1
- import { InvokeCallback, ActorLogic, EventObject } from "../types.js";
1
+ import { InvokeCallback, ActorLogic, EventObject, ActorRefFrom } from "../types.js";
2
2
  export interface CallbackInternalState<TEvent extends EventObject> {
3
3
  canceled: boolean;
4
4
  receivers: Set<(e: TEvent) => void>;
5
5
  dispose: void | (() => void) | Promise<any>;
6
6
  input?: any;
7
7
  }
8
- export declare function fromCallback<TEvent extends EventObject>(invokeCallback: InvokeCallback): ActorLogic<TEvent, undefined, CallbackInternalState<TEvent>>;
8
+ export type CallbackActorLogic<TEvent extends EventObject> = ActorLogic<TEvent, undefined, CallbackInternalState<TEvent>>;
9
+ export type CallbackActorRef<TEvent extends EventObject> = ActorRefFrom<CallbackActorLogic<TEvent>>;
10
+ export declare function fromCallback<TEvent extends EventObject>(invokeCallback: InvokeCallback): CallbackActorLogic<TEvent>;
@@ -1,4 +1,4 @@
1
- import { Subscribable, ActorLogic, EventObject, Subscription, AnyActorSystem } from "../types.js";
1
+ import { Subscribable, ActorLogic, EventObject, Subscription, AnyActorSystem, ActorRefFrom } from "../types.js";
2
2
  export interface ObservableInternalState<T> {
3
3
  subscription: Subscription | undefined;
4
4
  status: 'active' | 'done' | 'error' | 'canceled';
@@ -6,10 +6,13 @@ export interface ObservableInternalState<T> {
6
6
  input?: any;
7
7
  }
8
8
  export type ObservablePersistedState<T> = Omit<ObservableInternalState<T>, 'subscription'>;
9
- export declare function fromObservable<T, TEvent extends EventObject>(observableCreator: ({ input, system }: {
9
+ export type ObservableActorLogic<T> = ActorLogic<EventObject, T | undefined, ObservableInternalState<T>, ObservablePersistedState<T>>;
10
+ export type ObservableActorRef<T> = ActorRefFrom<ObservableActorLogic<T>>;
11
+ export declare function fromObservable<T>(observableCreator: ({ input, system }: {
10
12
  input: any;
11
13
  system: AnyActorSystem;
12
- }) => Subscribable<T>): ActorLogic<TEvent, T | undefined, ObservableInternalState<T>, ObservablePersistedState<T>>;
14
+ self: ObservableActorRef<T>;
15
+ }) => Subscribable<T>): ObservableActorLogic<T>;
13
16
  /**
14
17
  * Creates event observable logic that listens to an observable
15
18
  * that delivers event objects.
@@ -21,4 +24,5 @@ export declare function fromObservable<T, TEvent extends EventObject>(observable
21
24
  export declare function fromEventObservable<T extends EventObject>(lazyObservable: ({ input }: {
22
25
  input: any;
23
26
  system: AnyActorSystem;
24
- }) => Subscribable<T>): ActorLogic<EventObject, T | undefined, ObservableInternalState<T>, ObservablePersistedState<T>>;
27
+ self: ObservableActorRef<T>;
28
+ }) => Subscribable<T>): ObservableActorLogic<T>;
@@ -1,4 +1,4 @@
1
- import { ActorLogic, AnyActorSystem } from "../types.js";
1
+ import { ActorLogic, ActorRefFrom, AnyActorSystem } from "../types.js";
2
2
  export interface PromiseInternalState<T> {
3
3
  status: 'active' | 'error' | 'done' | 'canceled';
4
4
  data: T | undefined;
@@ -15,8 +15,11 @@ export type PromiseEvent<T> = {
15
15
  } | {
16
16
  type: 'xstate.stop';
17
17
  };
18
+ export type PromiseActorLogic<T> = ActorLogic<PromiseEvent<T>, T | undefined, PromiseInternalState<T>>;
19
+ export type PromiseActorRef<T> = ActorRefFrom<PromiseActorLogic<T>>;
18
20
  export declare function fromPromise<T>(promiseCreator: ({ input, system }: {
19
21
  input: any;
20
22
  system: AnyActorSystem;
21
- }) => PromiseLike<T>): ActorLogic<PromiseEvent<T>, T | undefined, PromiseInternalState<T>>;
23
+ self: PromiseActorRef<T>;
24
+ }) => PromiseLike<T>): PromiseActorLogic<T>;
22
25
  export {};
@@ -1,4 +1,6 @@
1
- import { ActorLogic, ActorContext, ActorSystem, EventObject } from "../types.js";
1
+ import { ActorLogic, ActorContext, ActorSystem, EventObject, ActorRefFrom } from "../types.js";
2
+ export type TransitionActorLogic<TState, TEvent extends EventObject> = ActorLogic<TEvent, TState, TState>;
3
+ export type TransitionActorRef<TState, TEvent extends EventObject> = ActorRefFrom<TransitionActorLogic<TState, TEvent>>;
2
4
  /**
3
5
  * Returns actor logic from a transition function and its initial state.
4
6
  *
@@ -8,6 +10,7 @@ import { ActorLogic, ActorContext, ActorSystem, EventObject } from "../types.js"
8
10
  * @param initialState The initial state of the transition function.
9
11
  * @returns Actor logic
10
12
  */
11
- export declare function fromTransition<TState, TEvent extends EventObject, TSystem extends ActorSystem<any>>(transition: (state: TState, event: TEvent, actorContext: ActorContext<TEvent, TState, TSystem>) => TState, initialState: TState | (({ input }: {
13
+ export declare function fromTransition<TState, TEvent extends EventObject, TSystem extends ActorSystem<any>>(transition: (state: TState, event: TEvent, actorContext: ActorContext<TEvent, TState, TSystem>) => TState, initialState: TState | (({ input, self }: {
12
14
  input: any;
13
- }) => TState)): ActorLogic<TEvent, TState, TState>;
15
+ self: TransitionActorRef<TState, TEvent>;
16
+ }) => TState)): TransitionActorLogic<TState, TEvent>;
@@ -4,6 +4,7 @@ import type { ActorStatus, Clock, Interpreter } from "./interpreter.js";
4
4
  import type { StateMachine } from "./StateMachine.js";
5
5
  import { TypegenDisabled, ResolveTypegenMeta, TypegenConstraint, MarkAllImplementationsAsProvided, AreAllImplementationsAssumedToBeProvided } from "./typegenTypes.js";
6
6
  import { PromiseEvent } from "./actors/promise.js";
7
+ import { CallbackActorRef } from "./actors/callback.js";
7
8
  export type AnyFunction = (...args: any[]) => any;
8
9
  type ReturnTypeOrValue<T> = T extends AnyFunction ? ReturnType<T> : T;
9
10
  export type IsNever<T> = [T] extends [never] ? true : false;
@@ -146,9 +147,10 @@ export type Transition<TContext extends MachineContext, TEvent extends EventObje
146
147
  export type Receiver<TEvent extends EventObject> = (listener: {
147
148
  bivarianceHack(event: TEvent): void;
148
149
  }['bivarianceHack']) => void;
149
- export type InvokeCallback<TEvent extends EventObject = AnyEventObject, TSentEvent extends EventObject = AnyEventObject> = (sendBack: (event: TSentEvent) => void, onReceive: Receiver<TEvent>, { input, system }: {
150
+ export type InvokeCallback<TEvent extends EventObject = AnyEventObject, TSentEvent extends EventObject = AnyEventObject> = (sendBack: (event: TSentEvent) => void, onReceive: Receiver<TEvent>, { input, system, self }: {
150
151
  input: any;
151
152
  system: AnyActorSystem;
153
+ self: CallbackActorRef<TEvent>;
152
154
  }) => (() => void) | Promise<any> | void;
153
155
  export interface InvokeMeta {
154
156
  src: string;
@@ -420,17 +422,20 @@ export type ContextFactory<TContext extends MachineContext> = ({ spawn, input }:
420
422
  spawn: Spawner;
421
423
  input: any;
422
424
  }) => TContext;
423
- export interface MachineConfig<TContext extends MachineContext, TEvent extends EventObject, TAction extends ParameterizedObject = ParameterizedObject, TActorMap extends ActorMap = ActorMap, TTypesMeta = TypegenDisabled> extends StateNodeConfig<NoInfer<TContext>, NoInfer<TEvent>, TAction> {
425
+ export type MachineConfig<TContext extends MachineContext, TEvent extends EventObject, TAction extends ParameterizedObject = ParameterizedObject, TActorMap extends ActorMap = ActorMap, TTypesMeta = TypegenDisabled> = (StateNodeConfig<NoInfer<TContext>, NoInfer<TEvent>, TAction> & {
424
426
  /**
425
427
  * The initial context (extended state)
426
428
  */
427
- context?: InitialContext<LowInfer<TContext>>;
428
429
  /**
429
430
  * The machine's own version.
430
431
  */
431
432
  version?: string;
432
433
  types?: MachineTypes<TContext, TEvent, TActorMap, TTypesMeta>;
433
- }
434
+ }) & (Equals<TContext, MachineContext> extends true ? {
435
+ context?: InitialContext<LowInfer<TContext>>;
436
+ } : {
437
+ context: InitialContext<LowInfer<TContext>>;
438
+ });
434
439
  export type ActorMap = Record<string, {
435
440
  output: any;
436
441
  }>;
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var actors_dist_xstateActors = require('./actions-5fb9f10d.cjs.js');
5
+ var actors_dist_xstateActors = require('./actions-2d912781.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-40bd643f.development.cjs.js');
5
+ var actors_dist_xstateActors = require('./actions-bce11b97.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 { S as STATE_DELIMITER, m as mapValues, t as toArray, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, c as memo, e as evaluateGuard, d as flatten, g as createInvokeId, h as getDelayedTransitions, i as formatInitialTransition, j as getCandidates, k as toInvokeConfig, l as getConfiguration, n as getStateNodes, r as resolveStateValue, o as isInFinalState, p as State, q as isErrorEvent, s as macrostep, u as transitionNode, v as getInitialConfiguration, w as resolveActionsAndContext, x as assign, y as createInitEvent, z as microstep, A as isAtomicStateNode, B as error, C as isStateId, D as getStateNodeByPath, E as getPersistedState, F as resolveReferencedActor, G as interpret, H as matchesState } from './actions-13190b25.development.esm.js';
2
- export { a5 as ConstantPrefix, L as Interpreter, M as InterpreterStatus, a6 as SpecialTargets, p as State, a3 as and, x as assign, P as cancel, Q as choose, O as doneInvoke, K as forwardTo, _ as fromCallback, $ as fromEventObservable, Z as fromObservable, Y as fromPromise, a0 as fromTransition, n as getStateNodes, G as interpret, R as log, H as matchesState, a2 as not, a4 as or, W as pathToStateValue, T as pure, U as raise, J as sendParent, I as sendTo, a1 as stateIn, V as stop, X as toObserver } from './actions-13190b25.development.esm.js';
1
+ import { S as STATE_DELIMITER, m as mapValues, t as toArray, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, c as memo, e as evaluateGuard, d as flatten, g as createInvokeId, h as getDelayedTransitions, i as formatInitialTransition, j as getCandidates, k as toInvokeConfig, l as getConfiguration, n as getStateNodes, r as resolveStateValue, o as isInFinalState, p as State, q as isErrorEvent, s as macrostep, u as transitionNode, v as getInitialConfiguration, w as resolveActionsAndContext, x as assign, y as createInitEvent, z as microstep, A as isAtomicStateNode, B as error, C as isStateId, D as getStateNodeByPath, E as getPersistedState, F as resolveReferencedActor, G as interpret, H as matchesState } from './actions-72105f77.development.esm.js';
2
+ export { a5 as ConstantPrefix, L as Interpreter, M as InterpreterStatus, a6 as SpecialTargets, p as State, a3 as and, x as assign, P as cancel, Q as choose, O as doneInvoke, K as forwardTo, _ as fromCallback, $ as fromEventObservable, Z as fromObservable, Y as fromPromise, a0 as fromTransition, n as getStateNodes, G as interpret, R as log, H as matchesState, a2 as not, a4 as or, W as pathToStateValue, T as pure, U as raise, J as sendParent, I as sendTo, a1 as stateIn, V as stop, X as toObserver } from './actions-72105f77.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 { S as STATE_DELIMITER, m as mapValues, t as toArray, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, c as memo, e as evaluateGuard, d as flatten, g as createInvokeId, h as getDelayedTransitions, i as formatInitialTransition, j as getCandidates, k as toInvokeConfig, l as getConfiguration, n as getStateNodes, r as resolveStateValue, o as isInFinalState, p as State, q as isErrorEvent, s as macrostep, u as transitionNode, v as getInitialConfiguration, w as resolveActionsAndContext, x as assign, y as createInitEvent, z as microstep, A as isAtomicStateNode, B as error, C as isStateId, D as getStateNodeByPath, E as getPersistedState, F as resolveReferencedActor, G as interpret, H as matchesState } from './actions-4d6514d2.esm.js';
2
- export { a5 as ConstantPrefix, L as Interpreter, M as InterpreterStatus, a6 as SpecialTargets, p as State, a3 as and, x as assign, P as cancel, Q as choose, O as doneInvoke, K as forwardTo, _ as fromCallback, $ as fromEventObservable, Z as fromObservable, Y as fromPromise, a0 as fromTransition, n as getStateNodes, G as interpret, R as log, H as matchesState, a2 as not, a4 as or, W as pathToStateValue, T as pure, U as raise, J as sendParent, I as sendTo, a1 as stateIn, V as stop, X as toObserver } from './actions-4d6514d2.esm.js';
1
+ import { S as STATE_DELIMITER, m as mapValues, t as toArray, f as formatTransitions, a as toTransitionConfigArray, b as formatTransition, N as NULL_EVENT, c as memo, e as evaluateGuard, d as flatten, g as createInvokeId, h as getDelayedTransitions, i as formatInitialTransition, j as getCandidates, k as toInvokeConfig, l as getConfiguration, n as getStateNodes, r as resolveStateValue, o as isInFinalState, p as State, q as isErrorEvent, s as macrostep, u as transitionNode, v as getInitialConfiguration, w as resolveActionsAndContext, x as assign, y as createInitEvent, z as microstep, A as isAtomicStateNode, B as error, C as isStateId, D as getStateNodeByPath, E as getPersistedState, F as resolveReferencedActor, G as interpret, H as matchesState } from './actions-3b74fb92.esm.js';
2
+ export { a5 as ConstantPrefix, L as Interpreter, M as InterpreterStatus, a6 as SpecialTargets, p as State, a3 as and, x as assign, P as cancel, Q as choose, O as doneInvoke, K as forwardTo, _ as fromCallback, $ as fromEventObservable, Z as fromObservable, Y as fromPromise, a0 as fromTransition, n as getStateNodes, G as interpret, R as log, H as matchesState, a2 as not, a4 as or, W as pathToStateValue, T as pure, U as raise, J as sendParent, I as sendTo, a1 as stateIn, V as stop, X as toObserver } from './actions-3b74fb92.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.After="xstate.after",t.DoneState="done.state",t.DoneInvoke="done.invoke",t.ErrorExecution="error.execution",t.ErrorCommunication="error.communication",t.ErrorPlatform="error.platform",t.ErrorCustom="xstate.error",t}({}),n=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});e.After,e.DoneState;const s=e.ErrorExecution,i=e.ErrorPlatform,o=e.ErrorCustom,r=".",a="",c="",u="xstate.init";function f(t,e,s,{to:i,event:o,id:r,delay:a}){const c=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${o}" }) instead`);const u="function"==typeof o?o(s):o;let f;if("string"==typeof a){const t=c&&c[a];f="function"==typeof t?t(s):t}else f="function"==typeof a?a(s):a;const d="function"==typeof i?i(s):i;let h;if("string"==typeof d){if(h=d===n.Parent?t?.self._parent:d===n.Internal?t?.self:d.startsWith("#_")?e.children[d.slice(2)]:e.children[d],!h)throw new Error(`Unable to send event to actor '${d}' from machine '${e.machine.id}'.`)}else h=d||t?.self;return[e,{to:h,event:u,id:r,delay:f}]}function d(t,e){if("number"==typeof e.delay)return void t.self.delaySend(e);const{to:n,event:s}=e;t.defer((()=>{n.send(s.type===o?{type:`${Kt(t.self.id)}`,data:s.data}:s)}))}function h(t,e,n){function s(t){}return s.type="xstate.sendTo",s.to=t,s.event=e,s.id=n?.id,s.delay=n?.delay,s.resolve=f,s.execute=d,s}const p=new WeakMap;function l(t,e,n){let s=p.get(t);return s?e in s||(s[e]=n()):(s={[e]:n()},p.set(t,s)),s[e]}function y(t,e,n,{sendId:s}){return[e,"function"==typeof s?s(n):s]}function g(t,e){t.self.cancel(e)}function m(t){function e(t){}return e.type="xstate.cancel",e.sendId=t,e.resolve=y,e.execute=g,e}class v{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 _="function"==typeof Symbol&&Symbol.observable||"@@observable";const b="$$xstate.resolve",x="$$xstate.reject";function S(t,e){const n=$(t),s=$(e);return"string"==typeof s?"string"==typeof n&&s===n:"string"==typeof n?n in s:Object.keys(n).every((t=>t in s&&S(n[t],s[t])))}function w(t){try{return M(t)?t:t.toString().split(r)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}function $(t){if("object"==typeof(e=t)&&"value"in e&&"context"in e&&"event"in e)return t.value;var e;if(M(t))return k(t);if("string"!=typeof t)return t;return k(w(t))}function k(t){if(1===t.length)return t[0];const e={};let n=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)n[t[e]]=t[e+1];else{const s=n;n={},s[t[e]]=n}return e}function E(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 I(t){return[].concat(...t)}function T(t){return M(t)?t:[t]}function O(t){return void 0===t?[]:T(t)}function j(t,e,n){if("function"==typeof 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];s[e]="function"==typeof n?n(i):n}return s}function M(t){return Array.isArray(t)}function P(t){return T(t).map((t=>void 0===t||"string"==typeof t?{target:t}:t))}function N(t){if(void 0!==t&&t!==a)return O(t)}function A(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 C(t){return t?"transition"in t?{src:t,input:void 0}:t:void 0}const D="xstate.init",R="xstate.stop";function Q(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const J=t=>{const e=Q();e&&e.register(t)};let V=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const z={deferEvents:!0,clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class B{constructor(t,e){this.logic=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new v(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=V.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={...z,...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.logic.restoreState?this.logic.restoreState(this.options.state,this._actorContext):this.options.state:this.logic.getInitialState(this._actorContext,this.options?.input)}update(t){this._state=t;const e=this.getSnapshot();let n;for(;n=this._deferred.shift();)n();for(const t of this.observers)t.next?.(e);const s=this.logic.getStatus?.(t);switch(s?.status){case"done":this._stopProcedure(),this._doneEvent=Ht(this.id,s.data),this._parent?.send(this._doneEvent),this._complete();break;case"error":this._stopProcedure(),this._parent?.send(Kt(this.id,s.data)),this._error(s.data)}}subscribe(t,e,n){const s=A(t,e,n);return this.observers.add(s),this.status===V.Stopped&&(s.complete?.(),this.observers.delete(s)),{unsubscribe:()=>{this.observers.delete(s)}}}start(){return this.status===V.Running||(this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=V.Running,this.logic.start&&this.logic.start(this._state,this._actorContext),this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start()),this}_process(t){try{const e=this.logic.transition(this._state,t,this._actorContext);this.update(e),t.type===R&&(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===V.Stopped?this:(this.mailbox.clear(),this.status===V.NotStarted?(this.status=V.Stopped,this):(this.mailbox.enqueue({type:R}),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!==V.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new v(this._process.bind(this)),this.status=V.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!==V.Stopped){if(this.status!==V.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({event:t,id:e,delay:n,to:s}){const i=this.clock.setTimeout((()=>{s?s.send(t):this.send(t)}),n);e&&(this.delayedEventsMap[e]=i)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:J)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.logic.getPersistedState?.(this._state)}[_](){return this}getSnapshot(){return this.logic.getSnapshot?this.logic.getSnapshot(this._state):this._state}}function U(t,e){return new B(t,e)}function W(t,e,n,{id:s,systemId:i,src:o,input:r}){const a=C(e.machine.implementations.actors[o]);let c;if(a){const u=r||a.input;c=U(a.src,{id:s,src:o,parent:t?.self,systemId:i,input:"function"==typeof u?u({context:e.context,event:n.event,self:t?.self}):u})}return[Ct(e,{children:{...e.children,[s]:c}}),{id:s,actorRef:c}]}function q(t,{id:e,actorRef:n}){n&&t.defer((()=>{if(n.status!==V.Stopped)try{n.start?.()}catch(n){return void t.self.send(Kt(e,n))}}))}function F({id:t,systemId:e,src:n,input:s}){function i(t){}return i.type="xstate.invoke",i.id=t,i.systemId=e,i.src=n,i.input=s,i.resolve=W,i.execute=q,i}function G(t,e,n,s){const{machine:i}=s,o=i?.implementations?.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:G})}function L(t,e){if("string"==typeof t){const n=e?.(t);return"function"==typeof n?{type:t,predicate:n,params:{type:t}}:n||{type:t,params:{type:t}}}if("function"==typeof t)return{type:t.name,predicate:t,params:{type:t.name,name:t.name}};const n=e?.(t.type);return"function"==typeof n?{type:t.type,params:t.params||t,children:t.children?.map((t=>L(t,e))),predicate:e?.(t.type)||t.predicate}:n||{type:t.type,params:t.params||t,children:t.children?.map((t=>L(t,e))),predicate:t.predicate}}const X=t=>"atomic"===t.type||"final"===t.type;function H(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function K(t,e){const n=[];let s=t.parent;for(;s&&s!==e;)n.push(s),s=s.parent;return n}function Y(t){const e=new Set(t),n=new Set(t),s=tt(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 H(t))if("history"!==e.type&&!n.has(e))for(const t of ut(e))n.add(t)}else ut(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 Z(t,e){const n=e.get(t);if(!n)return{};if("compound"===t.type){const t=n[0];if(!t)return{};if(X(t))return t.key}const s={};for(const t of n)s[t.key]=Z(t,e);return s}function tt(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 et(t,e){return Z(t,tt(Y(e)))}function nt(t,e=t[0].machine.root){return"compound"===e.type?H(e).some((e=>"final"===e.type&&t.includes(e))):"parallel"===e.type&&H(e).every((e=>nt(t,e)))}const st=t=>"#"===t[0];function it(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}`}("function"==typeof n?`${t.id}:delay[${s}]`:n,t.id);return t.entry.push(Ft({type:i},{id:i,delay:n})),t.exit.push(m(i)),i};return(M(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="string"==typeof i?{target:i}:i,r=isNaN(+t)?t:+t,a=s(r,e);return O(o).map((t=>({...t,event:a,delay:r})))}))).map((e=>{const{delay:n}=e;return{...ot(t,e.event,e),delay:n}}))}function ot(t,e,n){const s=N(n.target),i=n.reenter??!1,{guards:o}=t.machine.implementations,r=rt(t,s),a={...n,actions:O(n.actions),guard:n.guard?L(n.guard,(t=>o[t])):void 0,target:r,source:t,reenter:i,eventType:e,toJSON:()=>({...a,source:`#${t.id}`,target:r?r.map((t=>`#${t.id}`)):void 0})};return a}function rt(t,e){if(void 0!==e)return e.map((e=>{if("string"!=typeof e)return e;if(st(e))return t.machine.getStateNodeById(e);const n=e[0]===r;if(n&&!t.parent)return dt(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 dt(t.parent,s)}catch(e){throw new Error(`Invalid transition definition for state node '${t.id}':\n${e.message}`)}}))}function at(t){const e=N(t.target);return e?e.map((e=>"string"==typeof e?dt(t.parent,e):e)):t.parent.initial.target}function ct(t){return"history"===t.type}function ut(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 K(i,t))e.add(n);n(i)}else if("parallel"===s.type)for(const t of H(s))n(t)}(t),[...e]}function ft(t,e){if(st(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 dt(t,e){if("string"==typeof e&&st(e))try{return t.machine.getStateNodeById(e)}catch(t){}const n=w(e).slice();let s=t;for(;n.length;){const t=n.shift();if(!t.length)break;s=ft(s,t)}return s}function ht(t,e){const n=e instanceof At?e.value:$(e);if("string"==typeof n)return[t,t.states[n]];const s=Object.keys(n),i=s.map((e=>ft(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,s.reduce(((e,s)=>{const i=ft(t,s);if(!i)return e;const o=ht(i,n[s]);return e.concat(o)}),[]))}function pt(t,e,n,s){return"string"==typeof e?function(t,e,n,s){const i=ft(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=pt(ft(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=pt(ft(t,o),r,n,s);a&&i.push(...a)}return i.length?i:t.next(n,s)}(t,e,n,s)}function lt(t){return Object.keys(t.states).map((e=>t.states[e])).filter((t=>"history"===t.type))}function yt(t,e){let n=t;for(;n.parent&&n.parent!==e;)n=n.parent;return n.parent===e}function gt(t){const e=[];let n=t.parent;for(;n;)e.unshift(n),n=n.parent;return e}function mt(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 vt(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(mt(xt([i],e,n),xt([r],e,n))){if(!yt(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 _t(t,e){if(!t.target)return[];const n=new Set;for(const s of t.target)if(ct(s))if(e[s.id])for(const t of e[s.id])n.add(t);else for(const t of _t({target:at(s)},e))n.add(t);else n.add(s);return[...n]}function bt(t,e){const n=_t(t,e);if(!n)return null;if(!t.reenter&&"parallel"!==t.source.type&&n.every((e=>yt(e,t.source))))return t.source;return function(t){const[e]=t;let n=gt(e),s=[];for(const e of t){const t=gt(e);s=n.filter((e=>t.includes(e))),n=s,s=[]}return n[n.length-1]}(n.concat(t.source))}function xt(t,e,n){const s=new Set;for(const i of t)if(i.target?.length){const t=bt(i,n);for(const n of e)yt(n,t)&&s.add(n)}return[...s]}function St(t,e,n,s,i){const o=new Set(e.configuration);if(!t.length)return e;const r=function(t,e,n,s,i,o){const r=[],a={...e.historyValue},c=vt(t,n,a),u=[...e._internalQueue];o||function(t,e,n,s){const i=xt(t,e,n);i.sort(((t,e)=>e.order-t.order));for(const t of i)for(const s of lt(t)){let i;i="deep"===s.history?e=>X(e)&&yt(e,t):e=>e.parent===t,n[s.id]=Array.from(e).filter(i)}for(const t of i)s.push(...t.exit,...t.invoke.map((t=>Qt(t.id)))),e.delete(t)}(c,n,a,r);r.push(...c.flatMap((t=>t.actions))),wt(s,c,n,r,u,e,a,o);const f=[...n],d=nt(f);if(d){const t=f.sort(((t,e)=>e.order-t.order)).flatMap((t=>t.exit));r.push(...t)}try{const t=It(r,s,e,i),n=d?function(t,e,n){const s=t[0].machine,i=t.find((t=>"final"===t.type&&t.parent===s.root));return i&&i.output?j(i.output,e,n):void 0}(f,t.context,s):void 0;return u.push(...t._internalQueue),Ct(e,{configuration:f,historyValue:a,_internalQueue:u,context:t.context,done:d,output:n,children:t.children})}catch(t){throw t}}(t,e,o,s,n,i);return Ct(r,{value:{}})}function wt(t,e,n,s,i,o,r,a){const c=new Set,u=new Set;$t(e,r,u,c),a&&u.add(o.machine.root);for(const e of[...c].sort(((t,e)=>t.order-e.order))){n.add(e);for(const t of e.invoke)s.push(F(t));if(s.push(...e.entry),u.has(e))for(const t of u){const e=t.initial.actions;s.push(...e)}if("final"===e.type){const s=e.parent;if(!s.parent)continue;if(i.push(Xt(s.id,e.output?j(e.output,o.context,t):void 0)),s.parent){const t=s.parent;"parallel"===t.type&&H(t).every((t=>nt([...n],t)))&&i.push(Xt(t.id))}}}}function $t(t,e,n,s){for(const i of t){for(const t of i.target||[])kt(t,e,n,s);const t=bt(i,e),o=_t(i,e);for(const i of o)Et(i,t,s,e,n)}}function kt(t,e,n,s){if(ct(t))if(e[t.id]){const i=e[t.id];for(const t of i)kt(t,e,n,s);for(const o of i){Et(o,t.parent,s,e,n);for(const t of n)n.add(t)}}else{const i=at(t);for(const t of i)kt(t,e,n,s);for(const o of i){Et(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)kt(t,e,n,s);for(const o of i)Et(o,t,s,e,n)}else if("parallel"===t.type)for(const i of H(t).filter((t=>!ct(t))))[...s].some((t=>yt(t,i)))||kt(i,e,n,s)}function Et(t,e,n,s,i){const o=K(t,e);for(const t of o)if(n.add(t),"parallel"===t.type)for(const e of H(t).filter((t=>!ct(t))))[...n].some((t=>yt(t,e)))||kt(e,s,i,n)}function It(t,e,n,s){const{machine:i}=n;let o=Ct(n,{_internalQueue:[]});for(const n of t){const t="function"==typeof n?n:i.implementations.actions["string"==typeof n?n:n.type];if(!t)continue;const r={context:o.context,event:e,self:s?.self,system:s?.system,action:"string"==typeof n?{type:n}:n};if(!("resolve"in t)){s?.self.status===V.Running?t(r):s?.defer((()=>t(r)));continue}const a=t,[c,u,f]=a.resolve(s,o,r,t);o=c,"execute"in t&&(s?.self.status===V.Running?a.execute(s,u):s?.defer(a.execute.bind(null,s,u))),f&&(o=It(f,e,o,s))}return o}function Tt(t,e,n){let s=t;const i=[];if(e.type===R)return s=Ot(e,s,n),i.push(s),{state:s,microstates:i};let o=e;if(o.type!==u){s=St(jt(o,s),t,n,o,!1),i.push(s)}for(;!s.done;){let t=Mt(s,o);if(t.length)s=St(t,s,n,o,!1),i.push(s);else{if(!s._internalQueue.length)break;o=s._internalQueue[0];s=St(jt(o,s),s,n,o,!1),s._internalQueue.shift(),i.push(s)}}return s.done&&Ot(o,s,n),{state:s,microstates:i}}function Ot(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(Qt(t));return It(s,t,e,n)}function jt(t,e){return e.machine.getTransitionData(e,t)}function Mt(t,e){const n=new Set,s=t.configuration.filter(X);for(const i of s)t:for(const s of[i].concat(K(i,null)))if(s.always)for(const i of s.always)if(void 0===i.guard||G(i.guard,t.context,e,t)){n.add(i);break t}return vt(Array.from(n),new Set(t.configuration),t.historyValue)}function Pt(t,e){return et(t,[...Y(ht(t,e))])}function Nt(t){const e=[],n=t.initial,s=new Set;$t([n],{},new Set([t]),s);for(const t of[...s].sort(((t,e)=>t.order-e.order)))e.push(t);return e}class At{static from(t,e={},n){if(t instanceof At)return t.context!==e?new At({value:t.value,context:e,meta:{},configuration:[],children:{}},n):t;const s=Y(ht(n.root,t));return new At({value:t,context:e,meta:void 0,configuration:Array.from(s),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._internalQueue=void 0,this.configuration=void 0,this.children=void 0,this.context=t.context,this._internalQueue=t._internalQueue??[],this.historyValue=t.historyValue||{},this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=t.configuration??Array.from(Y(ht(e.root,t.value))),this.children=t.children,this.value=et(e.root,this.configuration),this.tags=new Set(I(this.configuration.map((t=>t.tags)))),this.done=t.done??!1,this.output=t.output}toStrings(t=this.value){if("string"==typeof t)return[t];const e=Object.keys(t);return e.concat(...e.map((e=>this.toStrings(t[e]).map((t=>e+r+t)))))}toJSON(){const{configuration:t,tags:e,machine:n,...s}=this;return{...s,tags:Array.from(e),meta:this.meta}}matches(t){return S(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 l(this,"nextEvents",(()=>[...new Set(I([...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 Ct(t,e={}){return new At({...t,...e},t.machine)}function Dt(t,e,n,{actorRef:s}){const i="function"==typeof s?s(n):s,o="string"==typeof i?e.children[i]:i;let r=e.children;return o&&(r={...r},delete r[o.id]),[Ct(e,{children:r}),o]}function Rt(t,e){e&&(e.status===V.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function Qt(t){function e(t){}return e.type="xstate.stop",e.actorRef=t,e.resolve=Dt,e.execute=Rt,e}function Jt(t,e,n,{value:s,label:i}){return[e,{value:"function"==typeof s?s(n):s,label:i}]}function Vt({logger:t},{value:e,label:n}){n?t(n,e):t(e)}function zt(t,{machine:e,context:n},s,i){return(o,r)=>{const a=((o,r={})=>{const{systemId:a}=r;if("string"==typeof o){const c=C(e.implementations.actors[o]);if(!c)throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`);const u="input"in r?r.input:c.input,f=U(c.src,{id:r.id,parent:t.self,input:"function"==typeof u?u({context:n,event:s,self:t.self}):u,systemId:a});return i[f.id]=f,f}return U(o,{id:r.id,parent:t.self,input:r.input,systemId:a})})(o,r);return i[a.id]=a,t.defer((()=>{if(a.status!==V.Stopped)try{a.start?.()}catch(e){return void t.self.send(Kt(a.id,e))}})),a}}function Bt(t,e,n,{assignment:s}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const i={},o={context:e.context,event:n.event,action:n.action,spawn:zt(t,e,n.event,i),self:t?.self,system:t?.system};let r={};if("function"==typeof s)r=s(o);else for(const t of Object.keys(s)){const e=s[t];r[t]="function"==typeof e?e(o):e}return[Ct(e,{context:Object.assign({},e.context,r),children:Object.keys(i).length?{...e.children,...i}:e.children})]}function Ut(t){function e(t){}return e.type="xstate.assign",e.assignment=t,e.resolve=Bt,e}function Wt(t,e,n,{event:s,id:i,delay:o}){const r=e.machine.implementations.delays;if("string"==typeof s)throw new Error(`Only event objects may be used with raise; use raise({ type: "${s}" }) instead`);const a="function"==typeof s?s(n):s;let c;if("string"==typeof o){const t=r&&r[o];c="function"==typeof t?t(n):t}else c="function"==typeof o?o(n):o;return["number"!=typeof c?Ct(e,{_internalQueue:e._internalQueue.concat(a)}):e,{event:a,id:i,delay:c}]}function qt(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}function Ft(t,e){function n(t){}return n.type="xstate.raise",n.event=t,n.id=e?.id,n.delay=e?.delay,n.resolve=Wt,n.execute=qt,n}function Gt(t,e,n,{branches:s}){const i=s.find((t=>{const s=t.guard&&L(t.guard,(t=>e.machine.implementations.guards[t]));return!s||G(s,e.context,n.event,e)}))?.actions;return[e,void 0,O(i)]}function Lt(t,e,n,{get:s}){return[e,void 0,O(s({context:e.context,event:n.event}))]}function Xt(t,n){const s=`${e.DoneState}.${t}`,i={type:s,output:n,toString:()=>s};return i}function Ht(t,n){const s=`${e.DoneInvoke}.${t}`,i={type:s,output:n,toString:()=>s};return i}function Kt(t,n){const s=`${e.ErrorPlatform}.${t}`,i={type:s,data:n,toString:()=>s};return i}const Yt={},Zt=t=>"string"==typeof t?{type:t}:"function"==typeof t?"resolve"in t?{type:t.type}:{type:t.name}:t;class te{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(r),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?E(this.config.states,((t,e)=>new te(t,{_parent:this,_key:e,_machine:this.machine}))):Yt,"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=O(this.config.entry),this.exit=O(this.config.exit),this.meta=this.config.meta,this.output="final"===this.type?this.config.output:void 0,this.tags=O(t.tags)}_initialize(){this.transitions=function(t){const e=new Map;if(t.config.on)for(const n of Object.keys(t.config.on)){if(n===c)throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.');const s=t.config.on[n];e.set(n,P(s).map((e=>ot(t,n,e))))}if(t.config.onDone){const n=String(Xt(t.id));e.set(n,P(t.config.onDone).map((e=>ot(t,n,e))))}for(const n of t.invoke){if(n.onDone){const s=`done.invoke.${n.id}`;e.set(s,P(n.onDone).map((e=>ot(t,s,e))))}if(n.onError){const s=`error.platform.${n.id}`;e.set(s,P(n.onError).map((e=>ot(t,s,e))))}if(n.onSnapshot){const s=`xstate.snapshot.${n.id}`;e.set(s,P(n.onSnapshot).map((e=>ot(t,s,e))))}}for(const n of t.after){let t=e.get(n.eventType);t||(t=[],e.set(n.eventType,t)),t.push(n)}return e}(this),this.config.always&&(this.always=P(this.config.always).map((t=>ot(this,c,t)))),Object.keys(this.states).forEach((t=>{this.states[t]._initialize()}))}get definition(){return{id:this.id,key:this.key,version:this.machine.version,type:this.type,initial:this.initial?{target:this.initial.target,source:this,actions:this.initial.actions.map(Zt),eventType:null,reenter:!1,toJSON:()=>({target:this.initial.target.map((t=>`#${t.id}`)),source:`#${this.id}`,actions:this.initial.actions.map(Zt),eventType:null})}:void 0,history:this.history,states:E(this.states,(t=>t.definition)),on:this.on,transitions:[...this.transitions.values()].flat().map((t=>({...t,actions:t.actions.map(Zt)}))),entry:this.entry.map(Zt),exit:this.exit.map(Zt),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 l(this,"invoke",(()=>O(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,o=s.src,{systemId:r}=s,a="string"==typeof o||"type"in o?o:i;return this.machine.implementations.actors[i]||"string"==typeof o||"type"in o||(this.machine.implementations.actors={...this.machine.implementations.actors,[i]:o}),{type:"xstate.invoke",...s,src:a,id:i,systemId:r,toJSON(){const{onDone:t,onError:e,...n}=s;return{...n,type:"xstate.invoke",src:a,id:i}}}}))))}get on(){return l(this,"on",(()=>[...this.transitions].flatMap((([t,e])=>e.map((e=>[t,e])))).reduce(((t,[e,n])=>(t[e]=t[e]||[],t[e].push(n),t)),{})))}get after(){return l(this,"delayedTransitions",(()=>it(this)))}get initial(){return l(this,"initial",(()=>function(t,e){if("string"==typeof e||M(e)){const n=O(e).map((e=>{const n="string"==typeof e?st(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(!yt(n,t))throw new Error(`Invalid initial target: state node #${n.id} is not a descendant of #${t.id}`);return n})),s=rt(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 ot(t,"__INITIAL__",{target:O(e.target).map((t=>"string"==typeof t?st(t)?t:`${r}${t}`:t)),actions:e.actions})}(this,this.config.initial||[])))}next(t,e){const n=e.type,s=[];let i;const o=l(this,`candidates-${n}`,(()=>{return e=n,(t=this).transitions.get(e)||[...t.transitions.keys()].filter((t=>{if("*"===t)return!0;if(!t.endsWith(".*"))return!1;const n=t.split("."),s=e.split(".");for(let t=0;t<n.length;t++){const e=n[t],i=s[t];if("*"===e)return t===n.length-1;if(e!==i)return!1}return!0})).sort(((t,e)=>e.length-t.length)).flatMap((e=>t.transitions.get(e)));var t,e}));for(const r of o){const{guard:o}=r,a=t.context;let c=!1;try{c=!o||G(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=I(Object.keys(this.states).map((t=>this.states[t].stateIds)));return[this.id].concat(t)}get events(){return l(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.keys()].filter((t=>this.transitions.get(t).some((t=>!(!t.target&&!t.actions.length&&!t.reenter))))));return Array.from(t)}}class ee{constructor(t,e){this.config=t,this.version=void 0,this.implementations=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.implementations={actors:e?.actors??{},actions:e?.actions??{},delays:e?.delays??{},guards:e?.guards??{}},this.version=this.config.version,this.types=this.config.types??{},this.transition=this.transition.bind(this),this.root=new te(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.implementations;return new ee(this.config,{actions:{...e,...t.actions},guards:{...n,...t.guards},actors:{...s,...t.actors},delays:{...i,...t.delays}})}resolveState(t){const e=Y(ht(this.root,t.value)),n=Array.from(e);return this.createState({...t,value:Pt(this.root,t.value),configuration:n,done:nt(n)})}resolveStateValue(t,...[e]){const n=Pt(this.root,t);return this.resolveState(At.from(n,e,this))}transition(t,e,n){if(function(t){return"string"==typeof t.type&&(t.type===s||t.type.startsWith(i))}(e)&&!t.nextEvents.some((t=>t===e.type)))throw e.data;const{state:o}=Tt(t,e,n);return o}microstep(t,e,n){return Tt(t,e,n).microstates}getTransitionData(t,e){return pt(this.root,t.value,t,e)||[]}getPreInitialState(t,e){const{context:n}=this.config,s=this.resolveState(this.createState({value:{},context:"function"!=typeof n&&n?n:{},meta:void 0,configuration:Nt(this.root),children:{}}));if("function"==typeof n){return It([Ut((({spawn:t,event:e})=>n({spawn:t,input:e.input})))],e,s,t)}return s}getInitialState(t,e){const n=function(t){return{type:u,input:t}}(e),s=this.getPreInitialState(t,n),i=St([{target:[...s.configuration].filter(X),source:this.root,reenter:!0,actions:[],eventType:null,toJSON:null}],s,t,n,!0),{state:o}=Tt(i,n,t);return o}start(t,e){Object.values(t.children).forEach((t=>{if(0===t.status)try{t.start?.()}catch(n){e.self.send(Kt(t.id,n))}}))}getStateNodeById(t){const e=t.split(r),n=e.slice(1),s=st(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 dt(i,n)}get definition(){return this.root.definition}toJSON(){return this.definition}getPersistedState(t){return function(t){const{configuration:e,tags:n,machine:s,children:i,...o}=t,r={};for(const t in i)r[t]={state:i[t].getPersistedState?.(),src:i[t].src};return{...o,children:r}}(t)}createState(t){return t instanceof At?t:new At(t,this)}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?C(this.implementations.actors[r])?.src:void 0;if(!a)return;const c=a.restoreState?.(o,e),u=U(a,{id:s,state:c});n[s]=u}));const s=this.createState(new At({...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=C(this.implementations.actors[i]);if(o){const i=U(o.src,{id:s,parent:e?.self,input:"input"in t?t.input:o.input});n[s]=i}}))})),s}}const ne={timeout:1e4};t.ConstantPrefix=e,t.Interpreter=B,t.InterpreterStatus=V,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=At,t.StateMachine=ee,t.StateNode=te,t.and=function(t){return{type:"xstate.boolean",params:{op:"and"},children:t.map((t=>L(t))),predicate:({evaluate:t,guard:e,context:n,event:s,state:i})=>e.children.every((e=>t(e,n,s,i)))}},t.assign=Ut,t.cancel=m,t.choose=function(t){function e(t){}return e.type="xstate.choose",e.branches=t,e.resolve=Gt,e},t.createMachine=function(t,e){return new ee(t,e)},t.doneInvoke=Ht,t.forwardTo=function(t,e){return h(t,(({event:t})=>t),e)},t.fromCallback=function(t){return{config:t,start:(t,{self:e})=>{e.send({type:D})},transition:(e,n,{self:s,id:i,system:o})=>{if(n.type===D){const n=t=>{e.canceled||s._parent?.send(t)},a=t=>{e.receivers.add(t)};return e.dispose=t(n,a,{input:e.input,system:o}),((r=e.dispose)instanceof Promise||null!==r&&("function"==typeof r||"object"==typeof r)&&"function"==typeof r.then)&&e.dispose.then((t=>{s._parent?.send(Ht(i,t)),e.canceled=!0}),(t=>{e.canceled=!0,s._parent?.send(Kt(i,t))})),e}var r;return n.type===R?(e.canceled=!0,"function"==typeof e.dispose&&e.dispose(),e):(function(t){return t.type===D||t.type===R}(n)||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 R: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,system:o})=>{"done"!==s.status&&(s.subscription=t({input:s.input,system:o}).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 R: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,system:r})=>{"done"!==i.status&&(i.subscription=t({input:i.input,system:r}).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){return{config:t,transition:(t,e)=>{if("active"!==t.status)return t;switch(e.type){case b:return{...t,status:"done",data:e.data,input:void 0};case x:return{...t,status:"error",data:e.data,input:void 0};case R:return{...t,status:"canceled",input:void 0};default:return t}},start:(e,{self:n,system:s})=>{if("active"!==e.status)return;Promise.resolve(t({input:e.input,system:s})).then((t=>{"active"===n._state.status&&n.send({type:b,data:t})}),(t=>{"active"===n._state.status&&n.send({type:x,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=ht,t.interpret=U,t.log=function(t=(({context:t,event:e})=>({context:t,event:e})),e){function n(t){}return n.type="xstate.log",n.value=t,n.label=e,n.resolve=Jt,n.execute=Vt,n},t.mapState=function(t,e){let n;for(const s of Object.keys(t))S(s,e)&&(!n||e.length>n.length)&&(n=s);return t[n]},t.matchesState=S,t.not=function(t){return{type:"xstate.boolean",params:{op:"not"},children:[L(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=>L(t))),predicate:({evaluate:t,guard:e,context:n,event:s,state:i})=>e.children.some((e=>t(e,n,s,i)))}},t.pathToStateValue=k,t.pure=function(t){function e(t){}return e.type="xstate.pure",e.get=t,e.resolve=Lt,e},t.raise=Ft,t.sendParent=function(t,e){return h(n.Parent,t,e)},t.sendTo=h,t.stateIn=function(t){return{type:"xstate.guard:in",params:{stateValue:t},predicate:({state:e})=>"string"==typeof t&&st(t)?e.configuration.some((e=>e.id===t.slice(1))):e.matches(t)}},t.stop=Qt,t.toObserver=A,t.waitFor=function(t,e,n){const s={...ne,...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.After="xstate.after",t.DoneState="done.state",t.DoneInvoke="done.invoke",t.ErrorExecution="error.execution",t.ErrorCommunication="error.communication",t.ErrorPlatform="error.platform",t.ErrorCustom="xstate.error",t}({}),n=function(t){return t.Parent="#_parent",t.Internal="#_internal",t}({});e.After,e.DoneState;const s=e.ErrorExecution,i=e.ErrorPlatform,o=e.ErrorCustom,r=".",a="",c="",u="xstate.init";function f(t,e,s,{to:i,event:o,id:r,delay:a}){const c=e.machine.implementations.delays;if("string"==typeof o)throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${o}" }) instead`);const u="function"==typeof o?o(s):o;let f;if("string"==typeof a){const t=c&&c[a];f="function"==typeof t?t(s):t}else f="function"==typeof a?a(s):a;const d="function"==typeof i?i(s):i;let h;if("string"==typeof d){if(h=d===n.Parent?t?.self._parent:d===n.Internal?t?.self:d.startsWith("#_")?e.children[d.slice(2)]:e.children[d],!h)throw new Error(`Unable to send event to actor '${d}' from machine '${e.machine.id}'.`)}else h=d||t?.self;return[e,{to:h,event:u,id:r,delay:f}]}function d(t,e){if("number"==typeof e.delay)return void t.self.delaySend(e);const{to:n,event:s}=e;t.defer((()=>{n.send(s.type===o?{type:`${Kt(t.self.id)}`,data:s.data}:s)}))}function h(t,e,n){function s(t){}return s.type="xstate.sendTo",s.to=t,s.event=e,s.id=n?.id,s.delay=n?.delay,s.resolve=f,s.execute=d,s}const p=new WeakMap;function l(t,e,n){let s=p.get(t);return s?e in s||(s[e]=n()):(s={[e]:n()},p.set(t,s)),s[e]}function y(t,e,n,{sendId:s}){return[e,"function"==typeof s?s(n):s]}function g(t,e){t.self.cancel(e)}function m(t){function e(t){}return e.type="xstate.cancel",e.sendId=t,e.resolve=y,e.execute=g,e}class v{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 _="function"==typeof Symbol&&Symbol.observable||"@@observable";const b="$$xstate.resolve",x="$$xstate.reject";function S(t,e){const n=$(t),s=$(e);return"string"==typeof s?"string"==typeof n&&s===n:"string"==typeof n?n in s:Object.keys(n).every((t=>t in s&&S(n[t],s[t])))}function w(t){try{return M(t)?t:t.toString().split(r)}catch(e){throw new Error(`'${t}' is not a valid state path.`)}}function $(t){if("object"==typeof(e=t)&&"value"in e&&"context"in e&&"event"in e)return t.value;var e;if(M(t))return k(t);if("string"!=typeof t)return t;return k(w(t))}function k(t){if(1===t.length)return t[0];const e={};let n=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)n[t[e]]=t[e+1];else{const s=n;n={},s[t[e]]=n}return e}function E(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 I(t){return[].concat(...t)}function T(t){return M(t)?t:[t]}function O(t){return void 0===t?[]:T(t)}function j(t,e,n){if("function"==typeof 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];s[e]="function"==typeof n?n(i):n}return s}function M(t){return Array.isArray(t)}function P(t){return T(t).map((t=>void 0===t||"string"==typeof t?{target:t}:t))}function N(t){if(void 0!==t&&t!==a)return O(t)}function A(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 C(t){return t?"transition"in t?{src:t,input:void 0}:t:void 0}const D="xstate.init",R="xstate.stop";function Q(){const t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:window;if(t.__xstate__)return t.__xstate__}const J=t=>{const e=Q();e&&e.register(t)};let V=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const z={deferEvents:!0,clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console),devTools:!1};class B{constructor(t,e){this.logic=t,this._state=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this.mailbox=new v(this._process.bind(this)),this.delayedEventsMap={},this.observers=new Set,this.logger=void 0,this.status=V.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={...z,...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.logic.restoreState?this.logic.restoreState(this.options.state,this._actorContext):this.options.state:this.logic.getInitialState(this._actorContext,this.options?.input)}update(t){this._state=t;const e=this.getSnapshot();let n;for(;n=this._deferred.shift();)n();for(const t of this.observers)t.next?.(e);const s=this.logic.getStatus?.(t);switch(s?.status){case"done":this._stopProcedure(),this._doneEvent=Ht(this.id,s.data),this._parent?.send(this._doneEvent),this._complete();break;case"error":this._stopProcedure(),this._parent?.send(Kt(this.id,s.data)),this._error(s.data)}}subscribe(t,e,n){const s=A(t,e,n);return this.observers.add(s),this.status===V.Stopped&&(s.complete?.(),this.observers.delete(s)),{unsubscribe:()=>{this.observers.delete(s)}}}start(){return this.status===V.Running||(this.system._register(this.sessionId,this),this._systemId&&this.system._set(this._systemId,this),this.status=V.Running,this.logic.start&&this.logic.start(this._state,this._actorContext),this.update(this._state),this.options.devTools&&this.attachDevTools(),this.mailbox.start()),this}_process(t){try{const e=this.logic.transition(this._state,t,this._actorContext);this.update(e),t.type===R&&(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===V.Stopped?this:(this.mailbox.clear(),this.status===V.NotStarted?(this.status=V.Stopped,this):(this.mailbox.enqueue({type:R}),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!==V.Running)return this;for(const t of Object.keys(this.delayedEventsMap))this.clock.clearTimeout(this.delayedEventsMap[t]);return this.mailbox.clear(),this.mailbox=new v(this._process.bind(this)),this.status=V.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!==V.Stopped){if(this.status!==V.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({event:t,id:e,delay:n,to:s}){const i=this.clock.setTimeout((()=>{s?s.send(t):this.send(t)}),n);e&&(this.delayedEventsMap[e]=i)}cancel(t){this.clock.clearTimeout(this.delayedEventsMap[t]),delete this.delayedEventsMap[t]}attachDevTools(){const{devTools:t}=this.options;if(t){("function"==typeof t?t:J)(this)}}toJSON(){return{id:this.id}}getPersistedState(){return this.logic.getPersistedState?.(this._state)}[_](){return this}getSnapshot(){return this.logic.getSnapshot?this.logic.getSnapshot(this._state):this._state}}function U(t,e){return new B(t,e)}function W(t,e,n,{id:s,systemId:i,src:o,input:r}){const a=C(e.machine.implementations.actors[o]);let c;if(a){const u=r||a.input;c=U(a.src,{id:s,src:o,parent:t?.self,systemId:i,input:"function"==typeof u?u({context:e.context,event:n.event,self:t?.self}):u})}return[Ct(e,{children:{...e.children,[s]:c}}),{id:s,actorRef:c}]}function q(t,{id:e,actorRef:n}){n&&t.defer((()=>{if(n.status!==V.Stopped)try{n.start?.()}catch(n){return void t.self.send(Kt(e,n))}}))}function F({id:t,systemId:e,src:n,input:s}){function i(t){}return i.type="xstate.invoke",i.id=t,i.systemId=e,i.src=n,i.input=s,i.resolve=W,i.execute=q,i}function G(t,e,n,s){const{machine:i}=s,o=i?.implementations?.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:G})}function L(t,e){if("string"==typeof t){const n=e?.(t);return"function"==typeof n?{type:t,predicate:n,params:{type:t}}:n||{type:t,params:{type:t}}}if("function"==typeof t)return{type:t.name,predicate:t,params:{type:t.name,name:t.name}};const n=e?.(t.type);return"function"==typeof n?{type:t.type,params:t.params||t,children:t.children?.map((t=>L(t,e))),predicate:e?.(t.type)||t.predicate}:n||{type:t.type,params:t.params||t,children:t.children?.map((t=>L(t,e))),predicate:t.predicate}}const X=t=>"atomic"===t.type||"final"===t.type;function H(t){return Object.values(t.states).filter((t=>"history"!==t.type))}function K(t,e){const n=[];let s=t.parent;for(;s&&s!==e;)n.push(s),s=s.parent;return n}function Y(t){const e=new Set(t),n=new Set(t),s=tt(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 H(t))if("history"!==e.type&&!n.has(e))for(const t of ut(e))n.add(t)}else ut(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 Z(t,e){const n=e.get(t);if(!n)return{};if("compound"===t.type){const t=n[0];if(!t)return{};if(X(t))return t.key}const s={};for(const t of n)s[t.key]=Z(t,e);return s}function tt(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 et(t,e){return Z(t,tt(Y(e)))}function nt(t,e=t[0].machine.root){return"compound"===e.type?H(e).some((e=>"final"===e.type&&t.includes(e))):"parallel"===e.type&&H(e).every((e=>nt(t,e)))}const st=t=>"#"===t[0];function it(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}`}("function"==typeof n?`${t.id}:delay[${s}]`:n,t.id);return t.entry.push(Ft({type:i},{id:i,delay:n})),t.exit.push(m(i)),i};return(M(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="string"==typeof i?{target:i}:i,r=isNaN(+t)?t:+t,a=s(r,e);return O(o).map((t=>({...t,event:a,delay:r})))}))).map((e=>{const{delay:n}=e;return{...ot(t,e.event,e),delay:n}}))}function ot(t,e,n){const s=N(n.target),i=n.reenter??!1,{guards:o}=t.machine.implementations,r=rt(t,s),a={...n,actions:O(n.actions),guard:n.guard?L(n.guard,(t=>o[t])):void 0,target:r,source:t,reenter:i,eventType:e,toJSON:()=>({...a,source:`#${t.id}`,target:r?r.map((t=>`#${t.id}`)):void 0})};return a}function rt(t,e){if(void 0!==e)return e.map((e=>{if("string"!=typeof e)return e;if(st(e))return t.machine.getStateNodeById(e);const n=e[0]===r;if(n&&!t.parent)return dt(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 dt(t.parent,s)}catch(e){throw new Error(`Invalid transition definition for state node '${t.id}':\n${e.message}`)}}))}function at(t){const e=N(t.target);return e?e.map((e=>"string"==typeof e?dt(t.parent,e):e)):t.parent.initial.target}function ct(t){return"history"===t.type}function ut(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 K(i,t))e.add(n);n(i)}else if("parallel"===s.type)for(const t of H(s))n(t)}(t),[...e]}function ft(t,e){if(st(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 dt(t,e){if("string"==typeof e&&st(e))try{return t.machine.getStateNodeById(e)}catch(t){}const n=w(e).slice();let s=t;for(;n.length;){const t=n.shift();if(!t.length)break;s=ft(s,t)}return s}function ht(t,e){const n=e instanceof At?e.value:$(e);if("string"==typeof n)return[t,t.states[n]];const s=Object.keys(n),i=s.map((e=>ft(t,e))).filter(Boolean);return[t.machine.root,t].concat(i,s.reduce(((e,s)=>{const i=ft(t,s);if(!i)return e;const o=ht(i,n[s]);return e.concat(o)}),[]))}function pt(t,e,n,s){return"string"==typeof e?function(t,e,n,s){const i=ft(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=pt(ft(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=pt(ft(t,o),r,n,s);a&&i.push(...a)}return i.length?i:t.next(n,s)}(t,e,n,s)}function lt(t){return Object.keys(t.states).map((e=>t.states[e])).filter((t=>"history"===t.type))}function yt(t,e){let n=t;for(;n.parent&&n.parent!==e;)n=n.parent;return n.parent===e}function gt(t){const e=[];let n=t.parent;for(;n;)e.unshift(n),n=n.parent;return e}function mt(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 vt(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(mt(xt([i],e,n),xt([r],e,n))){if(!yt(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 _t(t,e){if(!t.target)return[];const n=new Set;for(const s of t.target)if(ct(s))if(e[s.id])for(const t of e[s.id])n.add(t);else for(const t of _t({target:at(s)},e))n.add(t);else n.add(s);return[...n]}function bt(t,e){const n=_t(t,e);if(!n)return null;if(!t.reenter&&"parallel"!==t.source.type&&n.every((e=>yt(e,t.source))))return t.source;return function(t){const[e]=t;let n=gt(e),s=[];for(const e of t){const t=gt(e);s=n.filter((e=>t.includes(e))),n=s,s=[]}return n[n.length-1]}(n.concat(t.source))}function xt(t,e,n){const s=new Set;for(const i of t)if(i.target?.length){const t=bt(i,n);for(const n of e)yt(n,t)&&s.add(n)}return[...s]}function St(t,e,n,s,i){const o=new Set(e.configuration);if(!t.length)return e;const r=function(t,e,n,s,i,o){const r=[],a={...e.historyValue},c=vt(t,n,a),u=[...e._internalQueue];o||function(t,e,n,s){const i=xt(t,e,n);i.sort(((t,e)=>e.order-t.order));for(const t of i)for(const s of lt(t)){let i;i="deep"===s.history?e=>X(e)&&yt(e,t):e=>e.parent===t,n[s.id]=Array.from(e).filter(i)}for(const t of i)s.push(...t.exit,...t.invoke.map((t=>Qt(t.id)))),e.delete(t)}(c,n,a,r);r.push(...c.flatMap((t=>t.actions))),wt(s,c,n,r,u,e,a,o);const f=[...n],d=nt(f);if(d){const t=f.sort(((t,e)=>e.order-t.order)).flatMap((t=>t.exit));r.push(...t)}try{const t=It(r,s,e,i),n=d?function(t,e,n){const s=t[0].machine,i=t.find((t=>"final"===t.type&&t.parent===s.root));return i&&i.output?j(i.output,e,n):void 0}(f,t.context,s):void 0;return u.push(...t._internalQueue),Ct(e,{configuration:f,historyValue:a,_internalQueue:u,context:t.context,done:d,output:n,children:t.children})}catch(t){throw t}}(t,e,o,s,n,i);return Ct(r,{value:{}})}function wt(t,e,n,s,i,o,r,a){const c=new Set,u=new Set;$t(e,r,u,c),a&&u.add(o.machine.root);for(const e of[...c].sort(((t,e)=>t.order-e.order))){n.add(e);for(const t of e.invoke)s.push(F(t));if(s.push(...e.entry),u.has(e))for(const t of u){const e=t.initial.actions;s.push(...e)}if("final"===e.type){const s=e.parent;if(!s.parent)continue;if(i.push(Xt(s.id,e.output?j(e.output,o.context,t):void 0)),s.parent){const t=s.parent;"parallel"===t.type&&H(t).every((t=>nt([...n],t)))&&i.push(Xt(t.id))}}}}function $t(t,e,n,s){for(const i of t){for(const t of i.target||[])kt(t,e,n,s);const t=bt(i,e),o=_t(i,e);for(const i of o)Et(i,t,s,e,n)}}function kt(t,e,n,s){if(ct(t))if(e[t.id]){const i=e[t.id];for(const t of i)kt(t,e,n,s);for(const o of i){Et(o,t.parent,s,e,n);for(const t of n)n.add(t)}}else{const i=at(t);for(const t of i)kt(t,e,n,s);for(const o of i){Et(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)kt(t,e,n,s);for(const o of i)Et(o,t,s,e,n)}else if("parallel"===t.type)for(const i of H(t).filter((t=>!ct(t))))[...s].some((t=>yt(t,i)))||kt(i,e,n,s)}function Et(t,e,n,s,i){const o=K(t,e);for(const t of o)if(n.add(t),"parallel"===t.type)for(const e of H(t).filter((t=>!ct(t))))[...n].some((t=>yt(t,e)))||kt(e,s,i,n)}function It(t,e,n,s){const{machine:i}=n;let o=Ct(n,{_internalQueue:[]});for(const n of t){const t="function"==typeof n?n:i.implementations.actions["string"==typeof n?n:n.type];if(!t)continue;const r={context:o.context,event:e,self:s?.self,system:s?.system,action:"string"==typeof n?{type:n}:n};if(!("resolve"in t)){s?.self.status===V.Running?t(r):s?.defer((()=>t(r)));continue}const a=t,[c,u,f]=a.resolve(s,o,r,t);o=c,"execute"in t&&(s?.self.status===V.Running?a.execute(s,u):s?.defer(a.execute.bind(null,s,u))),f&&(o=It(f,e,o,s))}return o}function Tt(t,e,n){let s=t;const i=[];if(e.type===R)return s=Ot(e,s,n),i.push(s),{state:s,microstates:i};let o=e;if(o.type!==u){s=St(jt(o,s),t,n,o,!1),i.push(s)}for(;!s.done;){let t=Mt(s,o);if(t.length)s=St(t,s,n,o,!1),i.push(s);else{if(!s._internalQueue.length)break;o=s._internalQueue[0];s=St(jt(o,s),s,n,o,!1),s._internalQueue.shift(),i.push(s)}}return s.done&&Ot(o,s,n),{state:s,microstates:i}}function Ot(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(Qt(t));return It(s,t,e,n)}function jt(t,e){return e.machine.getTransitionData(e,t)}function Mt(t,e){const n=new Set,s=t.configuration.filter(X);for(const i of s)t:for(const s of[i].concat(K(i,null)))if(s.always)for(const i of s.always)if(void 0===i.guard||G(i.guard,t.context,e,t)){n.add(i);break t}return vt(Array.from(n),new Set(t.configuration),t.historyValue)}function Pt(t,e){return et(t,[...Y(ht(t,e))])}function Nt(t){const e=[],n=t.initial,s=new Set;$t([n],{},new Set([t]),s);for(const t of[...s].sort(((t,e)=>t.order-e.order)))e.push(t);return e}class At{static from(t,e={},n){if(t instanceof At)return t.context!==e?new At({value:t.value,context:e,meta:{},configuration:[],children:{}},n):t;const s=Y(ht(n.root,t));return new At({value:t,context:e,meta:void 0,configuration:Array.from(s),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._internalQueue=void 0,this.configuration=void 0,this.children=void 0,this.context=t.context,this._internalQueue=t._internalQueue??[],this.historyValue=t.historyValue||{},this.matches=this.matches.bind(this),this.toStrings=this.toStrings.bind(this),this.configuration=t.configuration??Array.from(Y(ht(e.root,t.value))),this.children=t.children,this.value=et(e.root,this.configuration),this.tags=new Set(I(this.configuration.map((t=>t.tags)))),this.done=t.done??!1,this.output=t.output}toStrings(t=this.value){if("string"==typeof t)return[t];const e=Object.keys(t);return e.concat(...e.map((e=>this.toStrings(t[e]).map((t=>e+r+t)))))}toJSON(){const{configuration:t,tags:e,machine:n,...s}=this;return{...s,tags:Array.from(e),meta:this.meta}}matches(t){return S(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 l(this,"nextEvents",(()=>[...new Set(I([...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 Ct(t,e={}){return new At({...t,...e},t.machine)}function Dt(t,e,n,{actorRef:s}){const i="function"==typeof s?s(n):s,o="string"==typeof i?e.children[i]:i;let r=e.children;return o&&(r={...r},delete r[o.id]),[Ct(e,{children:r}),o]}function Rt(t,e){e&&(e.status===V.Running?t.defer((()=>{t.stopChild(e)})):t.stopChild(e))}function Qt(t){function e(t){}return e.type="xstate.stop",e.actorRef=t,e.resolve=Dt,e.execute=Rt,e}function Jt(t,e,n,{value:s,label:i}){return[e,{value:"function"==typeof s?s(n):s,label:i}]}function Vt({logger:t},{value:e,label:n}){n?t(n,e):t(e)}function zt(t,{machine:e,context:n},s,i){return(o,r)=>{const a=((o,r={})=>{const{systemId:a}=r;if("string"==typeof o){const c=C(e.implementations.actors[o]);if(!c)throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`);const u="input"in r?r.input:c.input,f=U(c.src,{id:r.id,parent:t.self,input:"function"==typeof u?u({context:n,event:s,self:t.self}):u,systemId:a});return i[f.id]=f,f}return U(o,{id:r.id,parent:t.self,input:r.input,systemId:a})})(o,r);return i[a.id]=a,t.defer((()=>{if(a.status!==V.Stopped)try{a.start?.()}catch(e){return void t.self.send(Kt(a.id,e))}})),a}}function Bt(t,e,n,{assignment:s}){if(!e.context)throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.");const i={},o={context:e.context,event:n.event,action:n.action,spawn:zt(t,e,n.event,i),self:t?.self,system:t?.system};let r={};if("function"==typeof s)r=s(o);else for(const t of Object.keys(s)){const e=s[t];r[t]="function"==typeof e?e(o):e}return[Ct(e,{context:Object.assign({},e.context,r),children:Object.keys(i).length?{...e.children,...i}:e.children})]}function Ut(t){function e(t){}return e.type="xstate.assign",e.assignment=t,e.resolve=Bt,e}function Wt(t,e,n,{event:s,id:i,delay:o}){const r=e.machine.implementations.delays;if("string"==typeof s)throw new Error(`Only event objects may be used with raise; use raise({ type: "${s}" }) instead`);const a="function"==typeof s?s(n):s;let c;if("string"==typeof o){const t=r&&r[o];c="function"==typeof t?t(n):t}else c="function"==typeof o?o(n):o;return["number"!=typeof c?Ct(e,{_internalQueue:e._internalQueue.concat(a)}):e,{event:a,id:i,delay:c}]}function qt(t,e){"number"!=typeof e.delay||t.self.delaySend(e)}function Ft(t,e){function n(t){}return n.type="xstate.raise",n.event=t,n.id=e?.id,n.delay=e?.delay,n.resolve=Wt,n.execute=qt,n}function Gt(t,e,n,{branches:s}){const i=s.find((t=>{const s=t.guard&&L(t.guard,(t=>e.machine.implementations.guards[t]));return!s||G(s,e.context,n.event,e)}))?.actions;return[e,void 0,O(i)]}function Lt(t,e,n,{get:s}){return[e,void 0,O(s({context:e.context,event:n.event}))]}function Xt(t,n){const s=`${e.DoneState}.${t}`,i={type:s,output:n,toString:()=>s};return i}function Ht(t,n){const s=`${e.DoneInvoke}.${t}`,i={type:s,output:n,toString:()=>s};return i}function Kt(t,n){const s=`${e.ErrorPlatform}.${t}`,i={type:s,data:n,toString:()=>s};return i}const Yt={},Zt=t=>"string"==typeof t?{type:t}:"function"==typeof t?"resolve"in t?{type:t.type}:{type:t.name}:t;class te{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(r),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?E(this.config.states,((t,e)=>new te(t,{_parent:this,_key:e,_machine:this.machine}))):Yt,"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=O(this.config.entry),this.exit=O(this.config.exit),this.meta=this.config.meta,this.output="final"===this.type?this.config.output:void 0,this.tags=O(t.tags)}_initialize(){this.transitions=function(t){const e=new Map;if(t.config.on)for(const n of Object.keys(t.config.on)){if(n===c)throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.');const s=t.config.on[n];e.set(n,P(s).map((e=>ot(t,n,e))))}if(t.config.onDone){const n=String(Xt(t.id));e.set(n,P(t.config.onDone).map((e=>ot(t,n,e))))}for(const n of t.invoke){if(n.onDone){const s=`done.invoke.${n.id}`;e.set(s,P(n.onDone).map((e=>ot(t,s,e))))}if(n.onError){const s=`error.platform.${n.id}`;e.set(s,P(n.onError).map((e=>ot(t,s,e))))}if(n.onSnapshot){const s=`xstate.snapshot.${n.id}`;e.set(s,P(n.onSnapshot).map((e=>ot(t,s,e))))}}for(const n of t.after){let t=e.get(n.eventType);t||(t=[],e.set(n.eventType,t)),t.push(n)}return e}(this),this.config.always&&(this.always=P(this.config.always).map((t=>ot(this,c,t)))),Object.keys(this.states).forEach((t=>{this.states[t]._initialize()}))}get definition(){return{id:this.id,key:this.key,version:this.machine.version,type:this.type,initial:this.initial?{target:this.initial.target,source:this,actions:this.initial.actions.map(Zt),eventType:null,reenter:!1,toJSON:()=>({target:this.initial.target.map((t=>`#${t.id}`)),source:`#${this.id}`,actions:this.initial.actions.map(Zt),eventType:null})}:void 0,history:this.history,states:E(this.states,(t=>t.definition)),on:this.on,transitions:[...this.transitions.values()].flat().map((t=>({...t,actions:t.actions.map(Zt)}))),entry:this.entry.map(Zt),exit:this.exit.map(Zt),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 l(this,"invoke",(()=>O(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,o=s.src,{systemId:r}=s,a="string"==typeof o||"type"in o?o:i;return this.machine.implementations.actors[i]||"string"==typeof o||"type"in o||(this.machine.implementations.actors={...this.machine.implementations.actors,[i]:o}),{type:"xstate.invoke",...s,src:a,id:i,systemId:r,toJSON(){const{onDone:t,onError:e,...n}=s;return{...n,type:"xstate.invoke",src:a,id:i}}}}))))}get on(){return l(this,"on",(()=>[...this.transitions].flatMap((([t,e])=>e.map((e=>[t,e])))).reduce(((t,[e,n])=>(t[e]=t[e]||[],t[e].push(n),t)),{})))}get after(){return l(this,"delayedTransitions",(()=>it(this)))}get initial(){return l(this,"initial",(()=>function(t,e){if("string"==typeof e||M(e)){const n=O(e).map((e=>{const n="string"==typeof e?st(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(!yt(n,t))throw new Error(`Invalid initial target: state node #${n.id} is not a descendant of #${t.id}`);return n})),s=rt(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 ot(t,"__INITIAL__",{target:O(e.target).map((t=>"string"==typeof t?st(t)?t:`${r}${t}`:t)),actions:e.actions})}(this,this.config.initial||[])))}next(t,e){const n=e.type,s=[];let i;const o=l(this,`candidates-${n}`,(()=>{return e=n,(t=this).transitions.get(e)||[...t.transitions.keys()].filter((t=>{if("*"===t)return!0;if(!t.endsWith(".*"))return!1;const n=t.split("."),s=e.split(".");for(let t=0;t<n.length;t++){const e=n[t],i=s[t];if("*"===e)return t===n.length-1;if(e!==i)return!1}return!0})).sort(((t,e)=>e.length-t.length)).flatMap((e=>t.transitions.get(e)));var t,e}));for(const r of o){const{guard:o}=r,a=t.context;let c=!1;try{c=!o||G(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=I(Object.keys(this.states).map((t=>this.states[t].stateIds)));return[this.id].concat(t)}get events(){return l(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.keys()].filter((t=>this.transitions.get(t).some((t=>!(!t.target&&!t.actions.length&&!t.reenter))))));return Array.from(t)}}class ee{constructor(t,e){this.config=t,this.version=void 0,this.implementations=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.implementations={actors:e?.actors??{},actions:e?.actions??{},delays:e?.delays??{},guards:e?.guards??{}},this.version=this.config.version,this.types=this.config.types??{},this.transition=this.transition.bind(this),this.root=new te(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.implementations;return new ee(this.config,{actions:{...e,...t.actions},guards:{...n,...t.guards},actors:{...s,...t.actors},delays:{...i,...t.delays}})}resolveState(t){const e=Y(ht(this.root,t.value)),n=Array.from(e);return this.createState({...t,value:Pt(this.root,t.value),configuration:n,done:nt(n)})}resolveStateValue(t,...[e]){const n=Pt(this.root,t);return this.resolveState(At.from(n,e,this))}transition(t,e,n){if(function(t){return"string"==typeof t.type&&(t.type===s||t.type.startsWith(i))}(e)&&!t.nextEvents.some((t=>t===e.type)))throw e.data;const{state:o}=Tt(t,e,n);return o}microstep(t,e,n){return Tt(t,e,n).microstates}getTransitionData(t,e){return pt(this.root,t.value,t,e)||[]}getPreInitialState(t,e){const{context:n}=this.config,s=this.resolveState(this.createState({value:{},context:"function"!=typeof n&&n?n:{},meta:void 0,configuration:Nt(this.root),children:{}}));if("function"==typeof n){return It([Ut((({spawn:t,event:e})=>n({spawn:t,input:e.input})))],e,s,t)}return s}getInitialState(t,e){const n=function(t){return{type:u,input:t}}(e),s=this.getPreInitialState(t,n),i=St([{target:[...s.configuration].filter(X),source:this.root,reenter:!0,actions:[],eventType:null,toJSON:null}],s,t,n,!0),{state:o}=Tt(i,n,t);return o}start(t,e){Object.values(t.children).forEach((t=>{if(0===t.status)try{t.start?.()}catch(n){e.self.send(Kt(t.id,n))}}))}getStateNodeById(t){const e=t.split(r),n=e.slice(1),s=st(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 dt(i,n)}get definition(){return this.root.definition}toJSON(){return this.definition}getPersistedState(t){return function(t){const{configuration:e,tags:n,machine:s,children:i,...o}=t,r={};for(const t in i)r[t]={state:i[t].getPersistedState?.(),src:i[t].src};return{...o,children:r}}(t)}createState(t){return t instanceof At?t:new At(t,this)}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?C(this.implementations.actors[r])?.src:void 0;if(!a)return;const c=a.restoreState?.(o,e),u=U(a,{id:s,state:c});n[s]=u}));const s=this.createState(new At({...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=C(this.implementations.actors[i]);if(o){const i=U(o.src,{id:s,parent:e?.self,input:"input"in t?t.input:o.input});n[s]=i}}))})),s}}const ne={timeout:1e4};t.ConstantPrefix=e,t.Interpreter=B,t.InterpreterStatus=V,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=At,t.StateMachine=ee,t.StateNode=te,t.and=function(t){return{type:"xstate.boolean",params:{op:"and"},children:t.map((t=>L(t))),predicate:({evaluate:t,guard:e,context:n,event:s,state:i})=>e.children.every((e=>t(e,n,s,i)))}},t.assign=Ut,t.cancel=m,t.choose=function(t){function e(t){}return e.type="xstate.choose",e.branches=t,e.resolve=Gt,e},t.createMachine=function(t,e){return new ee(t,e)},t.doneInvoke=Ht,t.forwardTo=function(t,e){return h(t,(({event:t})=>t),e)},t.fromCallback=function(t){return{config:t,start:(t,{self:e})=>{e.send({type:D})},transition:(e,n,{self:s,id:i,system:o})=>{if(n.type===D){const n=t=>{e.canceled||s._parent?.send(t)},a=t=>{e.receivers.add(t)};return e.dispose=t(n,a,{input:e.input,system:o,self:s}),((r=e.dispose)instanceof Promise||null!==r&&("function"==typeof r||"object"==typeof r)&&"function"==typeof r.then)&&e.dispose.then((t=>{s._parent?.send(Ht(i,t)),e.canceled=!0}),(t=>{e.canceled=!0,s._parent?.send(Kt(i,t))})),e}var r;return n.type===R?(e.canceled=!0,"function"==typeof e.dispose&&e.dispose(),e):(function(t){return t.type===D||t.type===R}(n)||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 R: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,system:o})=>{"done"!==s.status&&(s.subscription=t({input:s.input,system:o,self:i}).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 R: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,system:r})=>{"done"!==i.status&&(i.subscription=t({input:i.input,system:r,self:o}).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){return{config:t,transition:(t,e)=>{if("active"!==t.status)return t;switch(e.type){case b:return{...t,status:"done",data:e.data,input:void 0};case x:return{...t,status:"error",data:e.data,input:void 0};case R:return{...t,status:"canceled",input:void 0};default:return t}},start:(e,{self:n,system:s})=>{if("active"!==e.status)return;Promise.resolve(t({input:e.input,system:s,self:n})).then((t=>{"active"===n._state.status&&n.send({type:b,data:t})}),(t=>{"active"===n._state.status&&n.send({type:x,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=ht,t.interpret=U,t.log=function(t=(({context:t,event:e})=>({context:t,event:e})),e){function n(t){}return n.type="xstate.log",n.value=t,n.label=e,n.resolve=Jt,n.execute=Vt,n},t.mapState=function(t,e){let n;for(const s of Object.keys(t))S(s,e)&&(!n||e.length>n.length)&&(n=s);return t[n]},t.matchesState=S,t.not=function(t){return{type:"xstate.boolean",params:{op:"not"},children:[L(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=>L(t))),predicate:({evaluate:t,guard:e,context:n,event:s,state:i})=>e.children.some((e=>t(e,n,s,i)))}},t.pathToStateValue=k,t.pure=function(t){function e(t){}return e.type="xstate.pure",e.get=t,e.resolve=Lt,e},t.raise=Ft,t.sendParent=function(t,e){return h(n.Parent,t,e)},t.sendTo=h,t.stateIn=function(t){return{type:"xstate.guard:in",params:{stateValue:t},predicate:({state:e})=>"string"==typeof t&&st(t)?e.configuration.some((e=>e.id===t.slice(1))):e.matches(t)}},t.stop=Qt,t.toObserver=A,t.waitFor=function(t,e,n){const s={...ne,...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