xstate 6.0.0-alpha.13 → 6.0.0-alpha.14

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 (28) hide show
  1. package/dist/{StateMachine-193c2d4d.development.esm.js → StateMachine-0fa9c094.development.esm.js} +1 -1
  2. package/dist/{StateMachine-614c0f36.esm.js → StateMachine-8dbd20ad.esm.js} +1 -1
  3. package/dist/{StateMachine-3120a7ff.development.cjs.js → StateMachine-98411dc5.development.cjs.js} +1 -1
  4. package/dist/{StateMachine-1b26c5de.cjs.js → StateMachine-e3b1aa2d.cjs.js} +1 -1
  5. package/dist/declarations/src/State.d.ts +13 -2
  6. package/dist/declarations/src/setup.d.ts +30 -1
  7. package/dist/declarations/src/types.d.ts +18 -2
  8. package/dist/{index-08d86676.development.cjs.js → index-07b19ed0.development.cjs.js} +2 -3
  9. package/dist/{index-32631949.cjs.js → index-0d940f2c.cjs.js} +2 -3
  10. package/dist/{index-603c1cda.esm.js → index-2635a437.esm.js} +2 -3
  11. package/dist/{index-7081e0c9.development.esm.js → index-362125ce.development.esm.js} +2 -3
  12. package/dist/xstate-actors.cjs.js +1 -1
  13. package/dist/xstate-actors.development.cjs.js +1 -1
  14. package/dist/xstate-actors.development.esm.js +1 -1
  15. package/dist/xstate-actors.esm.js +1 -1
  16. package/dist/xstate-graph.cjs.js +2 -2
  17. package/dist/xstate-graph.development.cjs.js +2 -2
  18. package/dist/xstate-graph.development.esm.js +2 -2
  19. package/dist/xstate-graph.esm.js +2 -2
  20. package/dist/xstate-graph.umd.min.js +1 -1
  21. package/dist/xstate-graph.umd.min.js.map +1 -1
  22. package/dist/xstate.cjs.js +26 -4
  23. package/dist/xstate.development.cjs.js +26 -4
  24. package/dist/xstate.development.esm.js +28 -6
  25. package/dist/xstate.esm.js +28 -6
  26. package/dist/xstate.umd.min.js +1 -1
  27. package/dist/xstate.umd.min.js.map +1 -1
  28. package/package.json +1 -1
@@ -1,2 +1,2 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XState={})}(this,function(t){"use strict";class e{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const n="@xstate.init",s="@xstate.stop";function o(t,e){return{type:`xstate.after.${t}.${e}`}}function i(t){return{type:`xstate.timeout.actor.${t}`}}function r(t,e){return{type:`xstate.done.state.${t}`,output:e}}function c(t){return{type:n,input:t}}function a(t){setTimeout(()=>{throw t})}const u="function"==typeof Symbol&&Symbol.observable||"@@observable",f={"@xstate.start":t=>{t.start()},"@xstate.raise":(t,e,n)=>{t.system.scheduler.schedule(t.self,t.self,e,n?.delay??0,n?.id)},"@xstate.sendTo":(t,e,n,s)=>{if("string"==typeof n)throw new Error("Only event objects may be used with sendTo");void 0!==s?.delay?t.system.scheduler.schedule(t.self,e,n,s?.delay??0,s?.id):t.defer(()=>{t.system._relay(t.self,e,n)})},"@xstate.cancel":(t,e)=>{t.system.scheduler.cancel(t.self,e)},"@xstate.stop":(t,e)=>{t.stopChild(e)}},h=new WeakMap;function d(t){const e=(t,e)=>[{status:"active",output:void 0,error:void 0,input:t},[]];return{start:(e,{self:n,system:s})=>{const o=e.input.actor;if(!o||"stopped"===o.getSnapshot().status)return;const i=t(e.input,{self:n,system:s});if(!i)return;let r,c=!1;const a=()=>{c||(c=!0,i.unsubscribe(),r?.unsubscribe())},u=n._parent;u&&(r=u.subscribe({complete:a,error:a})),h.set(n,{unsubscribe:a})},transition:(t,e,{self:n})=>e.type===s?(h.get(n)?.unsubscribe(),h.delete(n),[{...t,status:"stopped",error:void 0},[]]):[t,[]],initialTransition:e,getInitialSnapshot:(t,n)=>e(n)[0],getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}}function l(t,e,n){"stopped"!==t.getSnapshot().status&&t._parent&&e._relay(t,t._parent,n())}function p(){return d(({actor:t,eventType:e,mapper:n},{self:s,system:o})=>{const i="*"!==e&&e.endsWith(".*");return t.on(i?"*":e,t=>{i&&!Rt(t.type,e)||l(s,o,()=>n(t))})})}const y=p();function g(){return d(({actor:t,mappers:e},{self:n,system:s})=>{const{done:o,error:i,snapshot:r}=e;return t.subscribe({next:t=>{"done"===t.status&&o?l(n,s,()=>o(t.output)):"error"===t.status&&i?l(n,s,()=>i(t.error)):"active"===t.status&&r&&l(n,s,()=>r(t))},error:t=>{i&&l(n,s,()=>i(t))},complete:()=>{}})})}const v=g();function m(t,e){return{...t,...e}}function x(t,e,...n){t.push({action:e,args:n})}function _(t,e,n){x(t,f["@xstate.start"],e),t.push(function(t,e){return Object.assign(function(n){return{children:{...n.children,[e]:t}}},{_special:!0})}(e,n??e.id))}function b(t,e,n,s=!1,o=!0){const i={cancel:n=>{x(e,f["@xstate.cancel"],t,n)},emit:t=>{e.push(t)},log:(...n)=>{x(e,t.logger,...n)},raise:(s,o)=>{if("string"==typeof s)throw new Error("Only event objects may be used with raise");void 0!==o?.delay?x(e,f["@xstate.raise"],t,s,o):n.push(s)},spawn:(n,s)=>{if(!o)return{id:s?.id??s?.registryKey??n.id};const i=te(n,{...s,parent:t.self});return _(e,i,s?.id),i},sendTo:(n,s,o)=>{n&&x(e,f["@xstate.sendTo"],t,n,s,o)},stop:n=>{n&&(x(e,f["@xstate.stop"],t,n),e.push(function(t){return Object.assign(function(e){const n={...e.children};for(const e of Object.keys(n))n[e]===t&&delete n[e];return{children:n}},{_special:!0})}(n)))}};return s&&Object.assign(i,{listen:(n,s,o)=>{const i=te(y,{input:{actor:n,eventType:s,mapper:o},parent:t.self});return x(e,f["@xstate.start"],i),i},subscribeTo:(n,s)=>{const o=te(v,{input:{actor:n,mappers:"function"==typeof s?{snapshot:s}:s},parent:t.self});return x(e,f["@xstate.start"],o),o}}),$(i,(t,...n)=>{x(e,t,...n)})}function w(t,e){switch(t){case f["@xstate.start"]:{const[t]=e;return{actor:t,id:t.id,logic:t.logic,src:t.src,input:t.options?.input}}case f["@xstate.raise"]:{const[,t,n]=e;return{event:t,id:n?.id,delay:n?.delay}}case f["@xstate.sendTo"]:{const[,t,n,s]=e;return{target:t,event:n,id:s?.id,delay:s?.delay}}case f["@xstate.cancel"]:{const[,t]=e;return{id:t}}case f["@xstate.stop"]:{const[,t]=e;return{actor:t}}default:return}}function S(t,e,n,s){let o=t;const i=[];for(const r of s){const s="function"==typeof r?r:"object"==typeof r&&"action"in r&&"function"==typeof r.action&&r.action.bind(null,...r.args),c={context:o.context,event:e,output:Ot(e),self:n.self,system:n.system,children:o.children,parent:n.self._parent,actions:t.machine.implementations.actions,actorSources:t.machine.implementations.actorSources};let a;if("object"==typeof r&&null!==r){const{type:t,...e}=r;a=e}if(s&&"_special"in s){i.push({type:"object"==typeof r?"action"in r&&"function"==typeof r.action?r.action.name??"(anonymous)":r.type??"(anonymous)":r.name||"(anonymous)",params:a,args:[],exec:void 0});const t=s(c,E);t&&("context"in t||"children"in t)&&(o=bt(o,{...void 0!==t.context?{context:m(o.context,t.context)}:{},..."children"in t?{children:t.children}:{}}));continue}if(!s||!("resolve"in s)){const t="object"==typeof r&&null!==r&&"action"in r&&"function"==typeof r.action?w(r.action,r.args):void 0;i.push({type:"object"==typeof r?"action"in r&&"function"==typeof r.action?r.action.name??"(anonymous)":r.type:r.name||"(anonymous)",params:a,args:"object"==typeof r&&"action"in r?r.args:[],exec:s||("object"==typeof r&&null!==r?()=>n.defer(()=>n.emit(r)):void 0),...t});continue}}return[o,i]}function $(t,e){const n=(t,...n)=>{e(t,...n)};return Object.assign(n,{cancel:()=>{},emit:()=>{},log:()=>{},raise:()=>{},spawn:()=>({}),sendTo:()=>{},stop:()=>{},listen:()=>({}),subscribeTo:()=>({}),...t}),n}const E=$({},()=>{});function k(t){const e=t.trim(),n=e.match(/^(\d+)ms$/i);if(n)return parseInt(n[1],10);const s=e.match(/^(\d*)(\.?)(\d*)s$/i);if(s){const t=s[1]?parseInt(s[1],10):0,e=!!s[2],n=s[3]?parseInt(s[3].padEnd(3,"0").slice(0,3),10):0;return 1e3*t+(e?n:0)}const o=e.match(/^P(?:(?<weeks>\d+(?:[.,]\d+)?)W)?(?:(?<days>\d+(?:[.,]\d+)?)D)?(?:T(?:(?<hours>\d+(?:[.,]\d+)?)H)?(?:(?<minutes>\d+(?:[.,]\d+)?)M)?(?:(?<seconds>\d+(?:[.,]\d+)?)S)?)?$/i);if(!o?.groups)return;const{weeks:i,days:r,hours:c,minutes:a,seconds:u}=o.groups;if(!(i||r||c||a||u))return;const f=t=>t?Number(t.replace(",",".")):0;return 7*f(i)*24*60*60*1e3+24*f(r)*60*60*1e3+60*f(c)*60*1e3+60*f(a)*1e3+1e3*f(u)}function A(t){if(void 0!==t)return"number"==typeof t?t:k(t)}function j(t,e,n){if("function"==typeof t)return t(n);const s=function(t,e){if("string"!=typeof t)return t;const n=e[t];return void 0!==n?n:k(t)??t}(t,e);return"function"==typeof s?s(n):s}function I(t){return"atomic"===t.type||"final"===t.type||"choice"===t.type}function T(t){return Object.values(t.states).filter(t=>"history"!==t.type)}function O(t,e){const n=[];if(e===t)return n;let s=t.parent;for(;s&&s!==e;)n.push(s),s=s.parent;return n}function M(t){const e=new Set(t),n=new Set;for(const t of e)t.parent&&n.add(t.parent);for(const t of e)if("compound"!==t.type||n.has(t)){if("parallel"===t.type)for(const n of T(t))if(!e.has(n)){const t=V(n);for(const n of t)e.add(n)}}else for(const n of V(t))e.add(n);for(const t of e){let n=t.parent;for(;n&&!e.has(n);)e.add(n),n=n.parent}return e}function N(t,e){const n=M(e),s=new Map;for(const t of n)s.has(t)||s.set(t,[]),t.parent&&(s.has(t.parent)||s.set(t.parent,[]),s.get(t.parent).push(t));const o=t=>{const e=s.get(t);if(!e)return{};if("compound"===t.type){const t=e[0];if(!t)return{};if(I(t))return t.key}const n={};for(const t of e)n[t.key]=o(t);return n};return o(t)}function D(t,e){return"compound"===e.type?T(e).some(e=>"final"===e.type&&t.has(e)):"parallel"===e.type?T(e).every(e=>D(t,e)):"final"===e.type}const P=t=>"#"===t[0];function C(t,e,n){const s=e.type,o=t.entry;t.entry=(t,i)=>(i.raise(e,{id:s,delay:n(t)}),"function"==typeof o?o(t,i):void 0);const i=t.exit;return t.exit=(t,e)=>(e.cancel(s),"function"==typeof i?i(t,e):void 0),s}function R(t,e,n){const s=Dt(n.target),o=n.reenter??!1,i=L(t,s),r={...n,target:i,source:t,reenter:o,eventType:e,toJSON:()=>({...r,source:`#${t.id}`,target:i?i.map(t=>`#${t.id}`):void 0})};return r}function L(t,e){if(void 0!==e)return e.map(e=>{if("string"!=typeof e)return e;if(P(e))return t.machine.getStateNodeById(e);const n="."===e[0];if(n&&!t.parent)return z(t,e.slice(1));const s=n?t.key+e:e;if(!t.parent)throw new Error(`Invalid target: "${e}"`);try{return z(t.parent,s)}catch(e){throw new Error(`Invalid transition definition for state node '${t.id}':\n${e.message}`)}})}function K(t){const e=Dt(t.config.target);return e?{target:e.map(e=>"string"==typeof e?z(t.parent,e):e),source:t,reenter:!1,eventType:""}:t.parent.initial}function W(t){return"history"===t.type}function V(t){const e=new Set;!function t(n){if(!e.has(n))if(e.add(n),"compound"===n.type)t(n.initial.target[0]);else if("parallel"===n.type)for(const e of T(n))t(e)}(t);for(const n of e)for(const s of O(n,t))e.add(s);return e}function J(t,e){if(P(e))return t.machine.getStateNodeById(e);if(!t.states)throw new Error(`Unable to retrieve child state '${e}' from '${t.id}'; no child states exist.`);const n=t.states[e];if(!n)throw new Error(`Child state '${e}' does not exist on '${t.id}'`);return n}function z(t,e){if("string"==typeof e&&P(e))try{return t.machine.getStateNodeById(e)}catch{}const n=Et(e).slice();let s=t;for(;n.length;){const t=n.shift();if(!t.length)break;s=J(s,t)}return s}function B(t,e){if("string"==typeof e){const n=t.states[e];if(!n)throw new Error(`State '${e}' does not exist on '${t.id}'`);return[t,n]}const n=Object.keys(e),s=new Array(n.length),o=[t.machine.root,t];for(let e=0;e<n.length;e++){const i=J(t,n[e]);s[e]=i,o.push(i)}for(let t=0;t<n.length;t++)o.push(...B(s[t],e[n[t]]));return o}function F(t,e,n,s,o){if("string"==typeof e){const i=J(t,e).next(n,s,o);return i&&i.length?i:t.next(n,s,o)}const i=Object.keys(e),r=i[0];if(1===i.length){const i=F(J(t,r),e[r],n,s,o);return i&&i.length?i:t.next(n,s,o)}const c=[];for(const r of i){const i=e[r];if(!i)continue;const a=F(J(t,r),i,n,s,o);a&&c.push(...a)}return c.length?c:t.next(n,s,o)}function q(t,e){let n=t;for(;n.parent&&n.parent!==e;)n=n.parent;return n.parent===e}function U(t,e){for(const n of t)if(q(n,e))return!0;return!1}function G(t,e){const n=t instanceof Set?t.size:Array.isArray(t)?t.length:void 0,s=e instanceof Set?e.size:Array.isArray(e)?e.length:void 0;void 0!==n&&void 0!==s&&s<n&&([t,e]=[e,t]);const o=t instanceof Set?t:new Set(t);for(const t of e)if(o.has(t))return!0;return!1}function X(t,e,n,s,o){const i=new Set,r=new Map,c=t=>{let i=r.get(t);return i||(i=Q([t],e,n,s,o),r.set(t,i)),i};for(const e of t){let t=!1;const n=new Set;for(const s of i)if(G(c(e),c(s))){if(!q(e.source,s.source)){t=!0;break}n.add(s)}if(!t){for(const t of n)i.delete(t);i.add(e)}}return Array.from(i)}function Z(t,e,n,s){const o=e.historyValue,{targets:i}=nt(t,e,n,s,{resolveActions:!1});if(!i)return[];const r=new Set;for(const t of i)if(W(t))if(o[t.id])for(const e of o[t.id])r.add(e);else for(const o of Z(K(t),e,n,s))r.add(o);else r.add(t);return[...r]}function H(t,e,n,s){const o=Z(t,e,n,s),{reenter:i}=nt(t,e,n,s,{resolveActions:!1});if(!i&&o.every(e=>e===t.source||q(e,t.source)))return t.source;const[r,...c]=o.concat(t.source);for(const t of O(r,void 0))if(c.every(e=>q(e,t)))return t;return i?void 0:t.source.machine.root}function Q(t,e,n,s,o){const i=new Set;for(const r of t){const{targets:t}=nt(r,n,s,o,{resolveActions:!1});if(t?.length){const t=H(r,n,s,o);r.reenter&&r.source===t&&i.add(t);for(const n of e)q(n,t)&&i.add(n)}}return[...i]}function Y(t,e,n,s,o){return et([{target:[...V(t)],source:t,reenter:!0,eventType:null,toJSON:null}],e,n,s,!0,o)}function tt(t,e){return{...t,...e}}function et(t,e,n,s,o,i){const c=[];if(!t.length)return[e,c];{const a=new Set(e._nodes);let u=e.historyValue;const h=e.context,d=X(t,a,e,s,n),l=t=>nt(t,e,s,n),p=(t,o,i,r)=>{if(2===t.length){const c=[],a=[];let u;const f=b(n,c,a,!0),h=t({context:o,event:s,parent:n.self._parent,self:n.self,children:i,system:n.system,actions:e.machine.implementations.actions,actorSources:e.machine.implementations.actorSources,guards:e.machine.implementations.guards,delays:e.machine.implementations.delays,input:r},f);return void 0!==h?.context&&(u=tt(o,h.context)),[c,u,a]}return[[Object.assign((e,n)=>t({...e,input:r},n),"_special"in t?{_special:!0}:{})],void 0,void 0]};let y=e;const g=()=>{const t=Q(d,a,e,s,n);let o;t.sort((t,e)=>e.order-t.order);const r=[...a];for(const e of t)for(const t of Object.values(e.states)){if("history"!==t.type)continue;const n="deep"===t.history?t=>I(t)&&q(t,e):t=>t.parent===e;o??={...u},o[t.id]=r.filter(n)}for(const o of t){const t=e._stateInputs?.[o.id],[r,u,f]=o.exit?p(o.exit,y.context,e.children,t):[[]];f?.length&&i.push(...f),u&&(y=bt(y,{context:u}));const[h,d]=S(y,s,n,r);y=h,c.push(...d);for(const t of o.invoke){const e=y.children[t.id];e&&!e._isExternal&&n.stopChild(e),delete y.children[t.id]}a.delete(o)}u=o||u};o||g();let v=y.context;const m=[],x=[];for(const t of d){t.actions&&m.push(...It(t.actions));const e=l(t);void 0!==e.context&&(v=tt(v,e.context)),e.actions&&m.push(...e.actions),e.internalEvents&&x.push(...e.internalEvents)}x.length&&i.push(...x);const _=()=>{const t=t=>{const e=y.machine.root;if(void 0===e.output)return;let o;if(void 0!==t.output&&t.parent)o=Tt(t.output,y.context,s,n.self);else if("parallel"===t.type){const e=`xstate.done.state.${t.id}`,n=i.find(t=>t.type===e);o=n?.output}return Tt(e.output,y.context,r(t.id,o),n.self)},h=new Set,g=new Set,v=(t,e)=>{for(const n of t)if(e&&!q(n,e)||h.add(n),"parallel"===n.type)for(const t of T(n))U(h,t)||(h.add(t),m(t))},m=t=>{if(W(t))if(u[t.id]){const e=u[t.id];for(const t of e)h.add(t),m(t);for(const n of e)v(O(n,t.parent),void 0)}else{const e=K(t),{targets:n}=l(e);for(const s of n??[])h.add(s),e===t.parent?.initial&&g.add(t.parent),m(s);for(const e of n??[])v(O(e,t.parent),void 0)}else{if("compound"===t.type){const[e]=l(t.initial).targets;return W(e)||(h.add(e),g.add(e)),m(e),void v(O(e,t),void 0)}if("parallel"===t.type)for(const e of T(t))U(h,e)||(h.add(e),g.add(e),m(e))}};for(const t of d){const o=H(t,e,s,n),{targets:i,reenter:r}=nt(t,e,s,n,{resolveActions:!1});for(const e of i??[])W(e)||t.source===e&&t.source===o&&!r||(h.add(e),g.add(e)),m(e);const c=Z(t,e,s,n);for(const e of c){const n=O(e,o);"parallel"===o?.type&&n.push(o),v(n,!t.source.parent&&r?void 0:o)}}o&&g.add(e.machine.root);const x={...e._stateInputs};for(const t of d){const{targets:o,input:i}=nt(t,e,s,n,{resolveActions:!1});if(i&&o)for(const t of o)x[t.id]=i}const _=new Set,b={...e.children};for(const o of[...h].sort((t,e)=>t.order-e.order)){a.add(o);const u=[];let h=!1;for(const t of o.invoke){h=!0;let o=t.logic;"function"==typeof o&&(o=o({actorSources:e.machine.implementations.actorSources,context:y.context,event:s,self:n.self}));const i="string"==typeof o?Ct(e.machine,o):o;if(!i)throw new Error(`Actor logic '${"string"==typeof o?o:"inline"}' not implemented in machine '${e.machine.id}'`);const r="function"==typeof t.input?t.input({self:n.self,context:y.context,event:s,output:Ot(s)}):t.input,c=te(i,{...t,input:r,parent:n.self,syncSnapshot:!!t.onSnapshot});u.push({action:f["@xstate.start"],args:[c]}),t.id&&(b[t.id]=c)}h&&(y=bt(y,{children:b}));let d=y.context,l=!1;const v=x[o.id];if(o.entry){const[t,e,n]=p(o.entry,d,b,v);u.push(...t),n?.length&&i.push(...n),e&&(d=e,l=!0)}if(l&&(y.context=d),g.has(o)){const{actions:t,input:e}=nt(o.initial,y,s,n);if(t&&u.push(...t),e&&o.initial?.target)for(const t of o.initial.target)x[t.id]=e}const[m,w]=S(y,s,n,u);if(y=m,u.length=0,c.push(...w),"final"!==o.type)continue;const $=o.parent;let E="parallel"===$?.type?$:$?.parent,k=E||o;for("compound"===$?.type&&i.push(r($.id,void 0!==o.output?Tt(o.output,y.context,s,n.self):void 0));"parallel"===E?.type&&!_.has(E)&&D(a,E);){_.add(E);const t={};for(const e of T(E)){if("final"===e.type){t[e.key]=void 0!==e.output?Tt(e.output,y.context,s,n.self):void 0;continue}if("parallel"===e.type){const n=`xstate.done.state.${e.id}`,s=i.find(t=>t.type===n);t[e.key]=s?.output;continue}const o=T(e).find(t=>"final"===t.type&&a.has(t));t[e.key]=void 0!==o?.output?Tt(o.output,y.context,s,n.self):void 0}i.push(r(E.id,t)),k=E,E=E.parent}E||(y=bt(y,{status:"done",output:t(k)}))}JSON.stringify(x)!==JSON.stringify(e._stateInputs||{})&&(y=bt(y,{_stateInputs:x}))},[w,$]=S(y,s,n,m);y=w,c.push(...$),v&&v!==e.context&&(y=bt(y,{context:v})),_();const E=[...a];if("done"===y.status){const t=[];E.sort((t,e)=>e.order-t.order).forEach(e=>{if(e.exit){const n=y._stateInputs?.[e.id],[s,,o]=p(e.exit,y.context,y.children,n);t.push(...s),o?.length&&i.push(...o)}});const[e,o]=S(y,s,n,t);y=e,c.push(...o)}return u===e.historyValue&&e._nodes.length===a.size&&e._nodes.every(t=>a.has(t))?y.context!==h?[bt(y),c]:[y,c]:[bt(y,{_nodes:E,historyValue:u}),c]}}function nt(t,e,n,s,o){const i={context:e.context,event:n,output:Ot(n),value:e.value,children:e.children,system:s.system,parent:s.self._parent,self:s.self,actions:e.machine.implementations.actions,actorSources:e.machine.implementations.actorSources,guards:e.machine.implementations.guards,delays:e.machine.implementations.delays};if(t.to){const r=[],c=[],a=b(s,r,c,!1,o?.resolveActions??!0),u=t.to(i,a),f=u?.target?L(t.source,It(u.target)):void 0,h="function"==typeof t.input?t.input({context:e.context,event:n,output:Ot(n)}):t.input;return{targets:f,context:u?.context,reenter:u?.reenter,actions:r,internalEvents:c,input:h}}const r="function"==typeof t.input?t.input({context:e.context,event:n,output:Ot(n)}):t.input,c="function"==typeof t.context?t.context(i):t.context;return{targets:t.target,context:c,reenter:t.reenter,actions:void 0,internalEvents:void 0,input:r}}function st(t,e,o,i){let r=t;const c=[];function a(t,e){const n=o.self._collectedMicrosteps||[];n.push(...e),o.self._collectedMicrosteps=n,c.push(t)}if(e.type===s)return r=bt(rt(r,o),{status:"stopped"}),a([r,[]],[]),{snapshot:r,microsteps:c};let u=e;if(u.type!==n){const e=u,n=function(t){return t.type.startsWith("xstate.error.actor")}(e),s=r.machine.getTransitionData(r,e,o.self);if(n&&!s.length)return r=bt(t,{status:"error",error:e.error}),a([r,[]],[]),{snapshot:r,microsteps:c};const f=et(s,t,o,u,!1,i);r=f[0],a(f,s)}let f=!0;const h=t.machine.options?.maxIterations??1/0;let d=0,l=0;for(;"active"===r.status;){if(l++,l>1e3)throw new Error("Microstep count exceeded 1000");if(d++,d>h)throw new Error(`Infinite loop detected (>${h} microsteps)`);let t=f?ct(r,u,o):[];const e=t.length?r:void 0;if(!t.length){if(!i.length)break;u=i.shift(),t=r.machine.getTransitionData(r,u,o.self)}const n=et(t,r,o,u,!1,i);r=n[0],f=r!==e,a(n,t)}return"active"!==r.status&&r.children&&rt(r,o),{snapshot:r,microsteps:c}}function ot(t,e,n,s,o){return!!t.to&&it(t.to,e,n,s,o,s.machine.implementations)}function it(t,e,n,s,o,i){let r,c=!1;try{const a=()=>{throw c=!0,new Error("Effect triggered")};r=t({context:e,event:n,output:Ot(n),self:o,system:o.system,value:s.value,children:s.children,parent:{send:a},actions:i.actions,actorSources:i.actorSources,guards:i.guards,delays:i.delays},function(t,e){const n=(t,...n)=>{e(t,...n)};return Object.assign(n,{cancel:()=>{},emit:()=>{},log:()=>{},raise:()=>{},spawn:()=>({}),sendTo:()=>{},stop:()=>{},listen:()=>({}),subscribeTo:()=>({}),...t}),n}({emit:a,cancel:a,log:a,raise:a,spawn:a,sendTo:a,stop:a},a))}catch(t){if(c)return!0;throw t}return void 0!==r}function rt(t,e){let n;if(!t.children||0===(n=Object.values(t.children).filter(Boolean)).length)return t;for(const t of n)e.stopChild(t);return bt(t,{children:{}})}function ct(t,e,n){const s=new Set,o=t._nodes.filter(I);for(const i of o)t:for(let o=i;o;o=o.parent)if(o.always)for(const i of o.always)if(at(i,e,t,o,n.self)){s.add(i);break t}return X(Array.from(s),new Set(t._nodes),t,e,n)}function at(t,e,n,s,o){if(t.guard){const i={context:n.context,event:e,output:Ot(e),self:o,parent:o._parent,children:n.children,actions:s.machine.implementations.actions,actorSources:s.machine.implementations.actorSources,guards:s.machine.implementations.guards,delays:s.machine.implementations.delays,_snapshot:n};if(!t.guard(i))return!1}return!t.to||it(t.to,n.context,e,n,o,s.machine.implementations)}function ut(t){return!!t&&"object"==typeof t&&"machine"in t&&"value"in t}let ft,ht;function dt(){return ft??=ue()}const lt=function(t){return $t(t,this.value)},pt=function(t){return this.tags.has(t)},yt=function(t){const e=dt(),n=function(){if(ht)return ht;const t=dt();return ht={self:t,logger:()=>{},id:"",sessionId:"",defer:()=>{},system:t.system,stopChild:()=>{},emit:()=>{},actionExecutor:()=>{}},ht}(),s=this.machine.getTransitionData(this,t,e);if(!s?.length)return!1;for(const o of s){if(void 0!==o.target)return!0;const s=nt(o,this,t,n,{resolveActions:!1});if(s.targets?.length||s.context||ot(o,this.context,t,this,e))return!0}return!1},gt=function(){const{_nodes:t,_stateInputs:e,tags:n,machine:s,getMeta:o,getInputs:i,toJSON:r,can:c,hasTag:a,matches:u,...f}=this;return{...f,tags:Array.from(n)}},vt=function(){const t={};for(const e of this._nodes)void 0!==e.meta&&(t[e.id]=e.meta);return t},mt=function(){return this._stateInputs};function xt(t){const e=new Set;for(const n of t)for(const t of n.tags)e.add(t);return e}function _t(t,e){return{status:t.status,output:t.output,error:t.error,machine:e,context:t.context,_nodes:t._nodes,value:t.value??N(e.root,t._nodes),tags:xt(t._nodes),children:t.children,historyValue:t.historyValue||{},_stateInputs:t._stateInputs||{},matches:lt,hasTag:pt,can:yt,getMeta:vt,getInputs:mt,toJSON:gt}}function bt(t,e={}){const n={...t,...e};return(e._nodes??t._nodes)===t._nodes?{status:n.status,output:n.output,error:n.error,machine:t.machine,context:n.context,_nodes:t._nodes,value:t.value,tags:t.tags,children:n.children,historyValue:n.historyValue||{},_stateInputs:n._stateInputs||{},matches:lt,hasTag:pt,can:yt,getMeta:vt,getInputs:mt,toJSON:gt}:_t({...n,value:void 0},t.machine)}function wt(t){const e={};for(const n in t){const s=t[n];Array.isArray(s)&&(e[n]=s.map(t=>({id:t.id})))}return e}function St(t){let e;for(const n in t){const s=t[n];if(s&&"object"==typeof s)if("sessionId"in s&&"send"in s&&"ref"in s)e??=Array.isArray(t)?t.slice():{...t},e[n]={xstate$$type:Ut,id:s.id};else{const o=St(s);o!==s&&(e??=Array.isArray(t)?t.slice():{...t},e[n]=o)}}return e??t}function $t(t,e){const n=kt(t),s=kt(e);return"string"==typeof s?"string"==typeof n&&s===n:"string"==typeof n?n in s:Object.keys(n).every(t=>t in s&&$t(n[t],s[t]))}function Et(t){if(Mt(t))return t;const e=[];let n="";for(let s=0;s<t.length;s++){switch(t.charCodeAt(s)){case 92:n+=t[s+1],s++;continue;case 46:e.push(n),n="";continue}n+=t[s]}return e.push(n),e}function kt(t){if(ut(t))return t.value;if("string"!=typeof t)return t;return At(Et(t))}function At(t){if(1===t.length)return t[0];const e={};let n=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)n[t[e]]=t[e+1];else{const s=n;n={},s[t[e]]=n}return e}function jt(t){return Mt(t)?t:[t]}function It(t){return void 0===t?[]:jt(t)}function Tt(t,e,n,s){if("function"==typeof t){return t({context:e,event:n,output:Ot(n),self:s})}return t}function Ot(t){if(function(t){return t.type.startsWith("xstate.done.actor.")||t.type.startsWith("xstate.done.state.")}(t)){return t.output}}function Mt(t){return Array.isArray(t)}function Nt(t){return jt(t).map(t=>void 0===t||"string"==typeof t?{target:t}:"function"==typeof t?{to:t}:t)}function Dt(t){if(void 0!==t&&""!==t)return It(t)}function Pt(t,e,n){const s="object"==typeof t,o=s?t:void 0;return{next:(s?t.next:t)?.bind(o),error:(s?t.error:e)?.bind(o),complete:(s?t.complete:n)?.bind(o)}}function Ct(t,e){const n=e.match(/^xstate\.invoke\.(\d+)\.(.*)/);if(!n)return t.implementations.actorSources[e];const[,s,o]=n,i=t.getStateNodeById(o).config.invoke,r=(Array.isArray(i)?i[s]:i).src;return"string"==typeof r?t.implementations.actorSources[r]:r}function Rt(t,e){if(e===t)return!0;if("*"===e)return!0;if(!e.endsWith(".*"))return!1;const n=e.split("."),s=t.split(".");for(let t=0;t<n.length;t++){const e=n[t],o=s[t];if("*"===e){return t===n.length-1}if(e!==o)return!1}return!0}function Lt(t,e){return`${t.sessionId}.${e}`}const Kt=new WeakMap,Wt="xstate.logic.effect.resolve",Vt="xstate.logic.effect.reject",Jt="xstate.logic.effect.start";function zt(t){let e=Kt.get(t);return e||(e=new Map,Kt.set(t,e)),e}function Bt(t,e){if(t?.length)for(const n of t)switch(n.type){case"emit":e.emit(n.event);break;case"sendBack":{const t=e.self._parent;t&&e.system._relay(e.self,t,n.event);break}case"raise":e.system._relay(e.self,e.self,n.event);break;case"effect":{if(!n.key){const t=n.exec();"function"==typeof t&&zt(e.self).set(Symbol(),{cleanup:t});break}const t=zt(e.self);if(t.has(n.key))break;const s=n.exec();t.set(n.key,{cleanup:"function"==typeof s?s:void 0});break}case"cleanupEffects":{const t=Kt.get(e.self);if(!t)break;for(const{cleanup:e}of t.values())e?.();Kt.delete(e.self);break}}}function Ft(t){const e=(e,n,o)=>{if("active"!==e.status)return[e,[]];if(n.type===s)return[{...e,status:"stopped",input:void 0},[{type:"cleanupEffects"}]];if(n.type===Jt)return[{...e,effects:{...e.effects,[n.key]:{status:"active"}}},[]];if(n.type===Wt)return[{...e,effects:{...e.effects,[n.key]:{status:"done",output:n.output}}},[]];if(n.type===Vt)return[{...e,effects:{...e.effects,[n.key]:{status:"error",error:n.error}}},[]];const i=[],r={},c={emit:t=>{i.push({type:"emit",event:t})},sendBack:t=>{i.push({type:"sendBack",event:t})},raise:t=>{i.push({type:"raise",event:t})},effect:(t,n)=>{if("string"==typeof t)return s=t,o=n,void(e.effects?.[s]||(i.push({type:"effect",key:s,exec:o}),r[s]={status:"active"}));var s,o;i.push({type:"effect",exec:t})}},a=t.run({context:e.context,event:n,input:e.input,system:o.system,self:o.self,emit:o.emit},c),u={...e,...a||{}};return("context"in e||void 0!==a?.context)&&(u.context=a?.context??e.context),(a?.effects||Object.keys(r).length)&&(u.effects={...e.effects,...a?.effects,...r}),[u,i]},n={id:t.id,config:t,transition:e,start:(t,n)=>{const[s,o]=e(t,c(t.input),n);Object.assign(t,s),Bt(o,n)},initialTransition:(e,n)=>{const s=function(t,e){return"function"==typeof t?t({input:e}):t}(t.context,e),o={status:"active",output:void 0,error:void 0,input:e};return void 0!==s&&(o.context=s),[{...o},[]]},getInitialSnapshot:(t,e)=>n.initialTransition(e,t)[0],getPersistedSnapshot:t=>t,restoreSnapshot:t=>t};return n}let qt=!1;const Ut=1;let Gt=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const Xt={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console)};function Zt(t){return"object"==typeof t&&null!==t&&"args"in t&&"exec"in t}function Ht(t,e){if(!t?.length)return[];const n=[];for(const s of t)Zt(s)?e.actionExecutor(s):n.push(s);return n}class Qt{constructor(t,n){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this._boundProcess=this._process.bind(this),this.mailbox=new e(this._boundProcess),this.observers=new Set,this.eventListeners=new Map,this.logger=void 0,this._processingStatus=Gt.NotStarted,this._forceDeferredActions=!1,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this._lastSourceRef=void 0,this._collectedMicrosteps=[],this._collectedActions=[],this._collectedSent=[],this._initialEffects=void 0,this.registryKey=void 0,this.sessionId=void 0,this.system=void 0,this.trigger=void 0,this.src=void 0,this._deferred=[],this._pendingEffects=void 0;const s={...Xt,...n},{clock:o,logger:i,parent:r,syncSnapshot:c,id:u,registryKey:f,inspect:h}=s;this.system=r?r.system:s._systemRef?.current??function(t,e){let n=0;const s=new Map,o=new Map,i=new WeakMap,r=new Set,c={},{clock:a,logger:u}=e,f=(t,e,n,s,o)=>{r.size&&t&&(t._collectedSent??=[]).push({targetRef:e,targetId:e.id,event:n,delay:s,id:o})},h={schedule:(t,e,n,s,o=Math.random().toString(36).slice(2))=>{f(t,e,n,s,o);const i={source:t,target:e,event:n,delay:s,id:o,startedAt:Date.now()},r=Lt(t,o);l._snapshot._scheduledEvents[r]=i;const u=a.setTimeout(()=>{delete c[r],delete l._snapshot._scheduledEvents[r],d(t,e,n)},s);c[r]=u},cancel:(t,e)=>{const n=Lt(t,e),s=c[n];delete c[n],delete l._snapshot._scheduledEvents[n],void 0!==s&&a.clearTimeout(s)},cancelAll:t=>{for(const e in l._snapshot._scheduledEvents){const n=l._snapshot._scheduledEvents[e];n.source===t&&h.cancel(t,n.id)}}},d=(t,e,n)=>{const s=e.logic;if("function"==typeof s?.isInternalEventType&&s.isInternalEventType(n.type)&&t!==e)throw new Error(`Internal event "${n.type}" cannot be sent to actor "${e.id}" from outside.`);e._lastSourceRef=t,e._send(n)},l={children:s,reverseKeyedActors:i,keyedActors:o,_snapshot:{_scheduledEvents:(e?.snapshot&&e.snapshot.scheduler)??{}},_bookId:()=>"x:"+n++,_register:(t,e)=>(s.set(t,e),t),_unregister:t=>{s.delete(t.sessionId);const e=i.get(t);void 0!==e&&(o.delete(e),i.delete(t))},get:t=>o.get(t),getAll:()=>Object.fromEntries(o.entries()),_set:(t,e)=>{const n=o.get(t);if(n&&n!==e)throw new Error(`Actor with registry key '${t}' already exists.`);o.set(t,e),i.set(e,t)},inspect:t=>{const e=Pt(t);return r.add(e),{unsubscribe(){r.delete(e)}}},_sendInspectionEvent:e=>{if(!r.size)return;const n={...e,rootId:t.sessionId};r.forEach(t=>t.next?.(n))},_relay:(t,e,n)=>{f(t,e,n),d(t,e,n)},scheduler:h,getSnapshot:()=>({_scheduledEvents:{...l._snapshot._scheduledEvents}}),start:()=>{const t=l._snapshot._scheduledEvents;l._snapshot._scheduledEvents={};for(const e in t){const{source:n,target:s,event:o,delay:i,id:r}=t[e];h.schedule(n,s,o,i,r)}},_clock:a,_logger:u,_timerStrategy:e.timers};return l}(this,{clock:o,logger:i,timers:s.timers}),r||!s._systemRef||s._systemRef.current||(s._systemRef.current=this.system),h&&!r&&this.system.inspect(Pt(h)),this.sessionId=this.system._bookId(),this.id=u??this.sessionId,this.logger=n?.logger??this.system._logger,this.clock=n?.clock??this.system._clock,this._parent=r,this._syncSnapshot=c,this.options=s,this.src=s.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()},emit:t=>{const e=this.eventListeners.get(t.type),n=this.eventListeners.get("*");if(e||n){if(e)for(const n of e)try{n(t)}catch(t){a(t)}if(n)for(const e of n)try{e(t)}catch(t){a(t)}}},actionExecutor:t=>{const e=()=>{if(this._collectedActions.push({type:t.type,params:t.params}),!t.exec)return;const e=qt;try{qt=!0,t.exec()}finally{qt=e}};this._processingStatus!==Gt.Running||this._forceDeferredActions?this._deferred.push(e):e()}},this.send=this.send.bind(this),this.trigger=new Proxy({},{get:(t,e)=>t=>{this.send({...t,type:e})}});const d=f;d&&(this.registryKey=d,this.system._set(d,this)),this._collectedMicrosteps=[];let l=n?.snapshot??n?.state;if(l&&"object"==typeof l&&"_pendingEffects"in l){const{_pendingEffects:t,...e}=l;this._pendingEffects=t,l=e}try{if(l)this._snapshot=this.logic.restoreSnapshot?this.logic.restoreSnapshot(l,this._actorScope):l;else{const[t,e]=this.logic.initialTransition(this.options?.input,this._actorScope);this._snapshot=t,this._initialEffects=e}}catch(t){this._snapshot={status:"error",output:void 0,error:t},this._deferred.length=0}d&&"active"!==this._snapshot.status&&this.system._unregister(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this,parentRef:this._parent,id:this.id,src:this.src,snapshot:this._snapshot})}_setErrorSnapshot(t,e=this._snapshot){this._snapshot={...e,status:"error",error:t}}_next(t){for(const e of this.observers)try{e.next?.(t)}catch(t){a(t)}}update(t,e){this._snapshot=t;for(let e=0;e<this._deferred.length;e++){const n=this._deferred[e];try{n()}catch(e){this._deferred.length=0,this._setErrorSnapshot(e,t);break}}switch(this._deferred.length=0,this._snapshot.status){case"active":this._next(t);break;case"done":{this._next(t),this._stopProcedure(),this._complete();const e=(n=this.id,s=this._snapshot.output,{type:`xstate.done.actor.${n}`,output:s,actorId:n});this._parent&&this.system._relay(this,this._parent,e);break}case"error":this._error(this._snapshot.error)}var n,s;this.system._sendInspectionEvent({type:"@xstate.transition",actorRef:this,event:e,sourceRef:this._lastSourceRef,targetRef:this,snapshot:t,microsteps:this._collectedMicrosteps,actions:this._collectedActions,sent:this._collectedSent,eventType:e.type}),this._collectedMicrosteps=[],this._collectedActions=[],this._collectedSent=[]}_flushInitialEffects(){if(!this._initialEffects)return!0;this._forceDeferredActions=!0;try{return Bt(Ht(this._initialEffects,this._actorScope),this._actorScope),this._initialEffects=void 0,!0}catch(t){return this._initialEffects=void 0,this._deferred.length=0,this._setErrorSnapshot(t),this._error(t),!1}finally{this._forceDeferredActions=!1}}subscribe(t,e,n){const s=Pt(t,e,n);if(this._processingStatus!==Gt.Stopped)this.observers.add(s);else switch(this._snapshot.status){case"done":try{s.complete?.()}catch(t){a(t)}break;case"error":{const t=this._snapshot.error;if(s.error)try{s.error(t)}catch(t){a(t)}else a(t);break}}return{unsubscribe:()=>{this.observers.delete(s)}}}on(t,e){let n=this.eventListeners.get(t);return n||(n=new Set,this.eventListeners.set(t,n)),n.add(e),{unsubscribe:()=>{n.delete(e)}}}select(t,e=Object.is){return{subscribe:(n,s,o)=>{const i=Pt(n,s,o);let r=t(this.getSnapshot());return this.subscribe({next:n=>{const s=t(n);e(r,s)||(r=s,i.next?.(s))},error:i.error,complete:i.complete})},get:()=>t(this.getSnapshot())}}start(){if(this._processingStatus===Gt.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this.registryKey&&this.system._set(this.registryKey,this),this._processingStatus=Gt.Running;const t=c(this.options.input);this._lastSourceRef=this._parent;switch(this._snapshot.status){case"done":return this._flushInitialEffects()?(this.update(this._snapshot,t),this):this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._setErrorSnapshot(t),this._error(t),this}if(!this._flushInitialEffects())return this;if(this.update(this._snapshot,t),this._pendingEffects){const t=this.options.timers??this.system._timerStrategy??"resume";for(const e of this._pendingEffects)"@xstate.raise"===e.type&&this.system.scheduler.schedule(this,this,e.event,Yt(t,e),e.id);this._pendingEffects=void 0}return this.mailbox.start(),this}_process(t){let e,n;try{e=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){n={err:t}}if(n){const{err:t}=n;return this._setErrorSnapshot(t),void this._error(t)}try{const[n,s]=e,o=Ht(s,this._actorScope);this.update(n,t),Bt(o,this._actorScope)}catch(t){return this._setErrorSnapshot(t),void this._error(t)}t.type===s&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===Gt.Stopped?this:(this.mailbox.clear(),this._processingStatus===Gt.NotStarted?(this._processingStatus=Gt.Stopped,this):(this.mailbox.enqueue({type:s}),this.system._unregister(this),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){a(t)}this.observers.clear(),this.eventListeners.clear()}_error(t){if(this._stopProcedure(),this.observers.size){let e=!1;for(const n of this.observers){const s=n.error;e||=!s;try{s?.(t)}catch(t){a(t)}}this.observers.clear(),this.eventListeners.clear(),e&&a(t)}else this._parent||a(t),this.eventListeners.clear();var e;this._parent&&this.system._relay(this,this._parent,{type:`xstate.error.actor.${e=this.id}`,error:t,actorId:e})}_stopProcedure(){this._processingStatus===Gt.Running&&(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new e(this._boundProcess),this._processingStatus=Gt.Stopped,this.system._unregister(this))}_send(t){this._processingStatus!==Gt.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}toJSON(){return{xstate$$type:Ut,id:this.id}}getPersistedSnapshot(t){const e=this.logic.getPersistedSnapshot(this._snapshot,t),n=this.system._snapshot._scheduledEvents;let s;const o=Date.now();for(const t in n){const e=n[t];e.source===this&&e.target===this&&(s??=[]).push({type:"@xstate.raise",event:e.event,id:e.id,delay:e.delay,startedAt:e.startedAt,elapsed:Math.max(0,o-e.startedAt)})}return s??=this._pendingEffects,s?{...e,_pendingEffects:s}:e}[u](){return this}getSnapshot(){return this._snapshot}}function Yt(t,e){if("function"==typeof t)return t(e);switch(t){case"restart":return e.delay;case"absolute":return Math.max(0,e.startedAt+e.delay-Date.now());default:return Math.max(0,e.delay-e.elapsed)}}function te(t,e){return new Qt(t,e)}const ee=new WeakMap;const ne="xstate.observable.next",se="xstate.observable.error",oe="xstate.observable.complete";const ie="xstate.async.resolve",re="xstate.async.reject";class ce extends Error{constructor(t){super(`Async logic timed out after ${t}.`),this.name="TimeoutError"}}const ae=Ft({context:void 0,run:()=>{}});function ue(){return te(ae)}function fe(t,{machine:e,context:n},s,o){return(i,r)=>{const c=((i,r)=>{if("string"==typeof i){const c=Ct(e,i);if(!c)throw new Error(`Actor logic '${i}' not implemented in machine '${e.id}'`);const a=te(c,{id:r?.id,parent:t.self,syncSnapshot:r?.syncSnapshot,input:"function"==typeof r?.input?r.input({context:n,event:s,self:t.self}):r?.input,src:i,registryKey:r?.registryKey});return o[a.id]=a,a}return te(i,{id:r?.id,parent:t.self,syncSnapshot:r?.syncSnapshot,input:r?.input,src:i,registryKey:r?.registryKey})})(i,r);return o[c.id]=c,t.defer(()=>{c._processingStatus!==Gt.Stopped&&c.start()}),c}}const he=new WeakMap;function de(t,e,n){let s=he.get(t);return s?e in s||(s[e]=n()):(s={[e]:n()},he.set(t,s)),s[e]}const le={},pe=["invoke","after","on","entry","exit","always","states","initial","onDone","timeout","onTimeout","history","target","output"];class ye{constructor(t,e){this.config=t,this.key=void 0,this.id=void 0,this.type=void 0,this.path=void 0,this.states=void 0,this.history=void 0,this.entry=void 0,this.exit=void 0,this.parent=void 0,this.machine=void 0,this.meta=void 0,this.output=void 0,this.order=-1,this.description=void 0,this.tags=[],this.transitions=void 0,this.always=void 0,this.invoke=void 0,this.on=void 0,this.after=void 0,this.events=void 0,this.ownEvents=void 0,this.parent=e._parent,this.key=e._key,this.machine=e._machine,this.path=this.parent?this.parent.path.concat(this.key):[];const n=this.config.states?Object.keys(this.config.states)[0]:void 0;if(this.id=this.config.id||[this.machine.id,...this.path].join("."),this.type=this.config.type||(void 0!==n?"compound":this.config.history?"history":"atomic"),this.description=this.config.description,function(t){const e=t.config;if("choice"!==t.type){if(void 0!==e.choice)throw new Error(`State "${t.id}" has \`choice\`, but \`choice\` can only be used with \`type: 'choice'\`.`);return}if("function"!=typeof e.choice)throw new Error(`Choice state "${t.id}" must declare a \`choice\` function.`);for(const n of pe)if(void 0!==e[n])throw new Error(`Choice state "${t.id}" cannot declare \`${n}\`.`)}(this),this.order=this.machine.idMap.size,this.machine.idMap.set(this.id,this),this.states=this.config.states?function(t,e){const n={},s=Object.keys(t);for(let o=0;o<s.length;o++){const i=s[o];n[i]=e(t[i],i,t,o)}return n}(this.config.states,(t,e)=>new ye(t,{_parent:this,_key:e,_machine:this.machine})):le,"compound"===this.type&&!this.config.initial)throw new Error(`No initial state specified for compound state node "#${this.id}".`);this.history=!0===this.config.history?"shallow":this.config.history||!1,this.entry=this.config.entry,this.exit=this.config.exit,this.entry&&(this.entry._special=!0),this.exit&&(this.exit._special=!0),this.meta=this.config.meta,this.output="final"!==this.type&&this.parent?void 0:this.config.output,this.tags=It(t.tags).slice(),this.invoke=It(this.config.invoke).map((t,e)=>{const{src:n,registryKey:s}=t,o=(i=this.id,`${e}.${i}`);var i;const r=t.id??o,c="string"==typeof n?n:`xstate.invoke.${o}`;return{...t,src:c,logic:n,id:r,registryKey:s}})}_initialize(){this.after=function(t){const e=t.config.after,n=t.config.timeout,s=t.config.onTimeout,r=t.invoke.filter(t=>void 0!==t.timeout);if(!e&&void 0===n&&0===r.length)return[];const c=[];if(e)for(const n of Object.keys(e)){const s=Number.isNaN(+n)?n:+n;c.push({event:o(s,t.id),delay:s,transitions:e[n],fromDelaysMap:!0})}var a;void 0!==n&&s&&c.push({event:(a=t.id,{type:`xstate.timeout.${a}`}),delay:n,transitions:s,fromDelaysMap:!0});for(const t of r)c.push({event:i(t.id),delay:t.timeout,transitions:t.onTimeout,fromDelaysMap:!1});const u=[];for(const{event:e,delay:n,transitions:s,fromDelaysMap:o}of c){const i=C(t,e,e=>j(n,o?e.delays:{},{context:e.context,event:e.event,stateNode:t}));for(const t of Nt(s))u.push({...t,event:i,delay:n})}return u.map(e=>({...R(t,e.event,e),delay:e.delay}))}(this),this.transitions=function(t){const e=new Map;if(t.config.on)for(const n of Object.keys(t.config.on)){if(""===n)throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.');const s=t.config.on[n];e.set(n,ge(s,e=>R(t,n,e)))}if(t.config.onDone){const n=`xstate.done.state.${t.id}`;e.set(n,ge(t.config.onDone,e=>R(t,n,e)))}const n=(e,n)=>R(t,e,{to:(t,e)=>(e.cancel(n),{})}),s=(e,n,s)=>{const{target:o,to:i,reenter:r,...c}=n;return R(t,e,{...c,reenter:r,to:(t,e)=>{if(i){let n=!1;const o=new Proxy(e,{apply:(t,e,s)=>(n=!0,Reflect.apply(t,e,s)),get(t,e,s){const o=Reflect.get(t,e,s);return"function"!=typeof o?o:(...e)=>(n=!0,o.apply(t,e))}}),r=i(t,o);return(void 0!==r||n)&&e.cancel(s),r}return e.cancel(s),{target:o,reenter:r}}})};for(const o of t.invoke){const r=void 0!==o.timeout?i(o.id).type:void 0;if(o.onDone){const i=`xstate.done.actor.${o.id}`,c=ge(o.onDone,e=>r?s(i,e,r):R(t,i,e));r&&c.push(n(i,r)),e.set(i,c)}else if(r){const t=`xstate.done.actor.${o.id}`;e.set(t,[n(t,r)])}if(o.onError){const n=`xstate.error.actor.${o.id}`;e.set(n,ge(o.onError,e=>r?s(n,e,r):R(t,n,e)))}if(o.onSnapshot){const n=`xstate.snapshot.${o.id}`;e.set(n,ge(o.onSnapshot,e=>R(t,n,e)))}}for(const n of t.after){let t=e.get(n.eventType);t||(t=[],e.set(n.eventType,t)),t.push(n)}return e}(this),"choice"===this.type?this.always=function(t){const e=t.config.choice,n=e=>{if(!e||void 0===e.target)throw new Error(`Choice state "${t.id}" must resolve to a target.`);for(const n of["actions","to"])if(void 0!==e[n])throw new Error(`Choice state "${t.id}" cannot declare \`${n}\` on a choice.`);return e};return[R(t,"",{to:t=>n(e(t))})]}(this):this.config.always&&(this.always=ge(this.config.always,t=>R(this,"",t)));for(const t of Object.keys(this.states))this.states[t]._initialize();this._refreshEventMetadata()}_refreshEventMetadata(){const t={},e=[];for(const[n,s]of this.transitions)t[n]=s.slice(),s.some(t=>t.target||t.reenter||t.to)&&e.push(n);this.on=t,this.ownEvents=e;const n=new Set(e);for(const t of Object.values(this.states))for(const e of t.events)n.add(e);this.events=Array.from(n)}get initial(){return de(this,"initial",()=>function(t,e){const n="object"==typeof e&&null!==e?e.target:e,s="object"==typeof e&&null!==e?e.input:void 0,o="string"==typeof n?t.states[n]:void 0;if(!o&&n)throw new Error(`Initial state node "${n}" not found on parent state node #${t.id}`);const i={source:t,target:o?[o]:void 0,input:s};return i}(this,this.config.initial))}next(t,e,n){const s=e.type,o=de(this,`candidates-${s}`,()=>function(t,e){const n=t.transitions.get(e),s=[...t.transitions.keys()].filter(t=>t!==e&&Rt(e,t)).sort((t,e)=>e.length-t.length).flatMap(e=>t.transitions.get(e));return n?[...n,...s]:s}(this,s));for(const s of o){if(at(s,e,t,this,n))return[s]}}}function ge(t,e){const n=Nt(t),s=new Array(n.length);for(let t=0;t<n.length;t++)s[t]=e(n[t]);return s}class ve{constructor(t,e){this.config=t,this.version=void 0,this.schemas=void 0,this.implementations=void 0,this.options=void 0,this.idMap=new Map,this.root=void 0,this.id=void 0,this.states=void 0,this.events=void 0,this.internalEventDescriptors=void 0,this._json=void 0,this.id=t.id||"(machine)",this.implementations={actorSources:t.actorSources??{},actions:t.actions??{},delays:t.delays??{},guards:t.guards??{},...e},this.version=this.config.version,this.schemas=this.config.schemas,this.internalEventDescriptors=this.config.internalEvents??[],this.options={maxIterations:1/0,...this.config.options},this.transition=this.transition.bind(this),this.initialTransition=this.initialTransition.bind(this),this.getInitialSnapshot=this.getInitialSnapshot.bind(this),this.getPersistedSnapshot=this.getPersistedSnapshot.bind(this),this.restoreSnapshot=this.restoreSnapshot.bind(this),this.start=this.start.bind(this),this.root=new ye(t,{_key:this.id,_machine:this}),this.root._initialize(),function(t){const e=[],n=s=>{Object.values(s).forEach(s=>{if(s.config.route&&s.config.id){const o=s.config.id,i=s.config.route,r=({event:t})=>t.to===`#${o}`;if("function"==typeof i)return e.push(R(t,"xstate.route",{guard:r,to:t=>{const e=i(t);if(e)return{...!0===e?{}:e,target:`#${o}`}}})),void(s.states&&n(s.states));const{guard:c,...a}=i,u={...a,guard:r,target:`#${o}`};e.push(R(t,"xstate.route",u))}s.states&&n(s.states)})};n(t.states),e.length>0&&t.transitions.set("xstate.route",e)}(this.root),this.root._refreshEventMetadata(),this.states=this.root.states,this.events=this.root.events}provide(t){const{actions:e,guards:n,actorSources:s,delays:o}=this.implementations,i=new ve(this.config,{actions:{...e,...t.actions},guards:{...n,...t.guards},actorSources:{...s,...t.actorSources},delays:{...o,...t.delays}});return i._json=this._json,i}resolveState(t){const e=(n=this.root,s=t.value,N(n,M(B(n,s))));var n,s;const o=M(B(this.root,e));return _t({_nodes:[...o],value:e,context:t.context||{},children:{},status:D(o,this.root)?"done":t.status||"active",output:t.output,error:t.error,historyValue:t.historyValue},this)}transition(t,e,n){const s=this._transitionFast(t,e,n);if(s)return[s,[]];const{snapshot:o,microsteps:i}=st(t,e,n,[]);return[o,i.flatMap(([,t])=>t)]}_transitionFast(t,e,n){if("active"!==t.status||"string"!=typeof t.value||this.root.always?.length)return;const s=this.root.states[t.value];if(!s||"atomic"!==s.type||s.exit||s.invoke.length||s.always?.length||s.after?.length)return;const o=s.transitions.get(e.type);if(1!==o?.length)return;const i=o[0];if(i.guard||i.actions||i.to||i.reenter||i.input||"function"==typeof i.context||i.target&&1!==i.target.length)return;const r=i.target?.[0]??s,c=r!==s;if(r.parent!==this.root||"atomic"!==r.type||c&&(r.entry||r.invoke.length||r.always?.length||r.after?.length))return;const a=void 0!==i.context?{...t.context,...i.context}:t.context,u=n.self._collectedMicrosteps||[];return u.push(i),n.self._collectedMicrosteps=u,bt(t,{...a!==t.context?{context:a}:{},...c?{_nodes:[this.root,r]}:{}})}microstep(t,e,n){const{microsteps:s}=st(t,e,n,[]),o=new Array(s.length);for(let t=0;t<s.length;t++)o[t]=s[t][0];return o}getTransitionData(t,e,n){return F(this.root,t.value,t,e,n)||[]}isInternalEventType(t){for(const e of this.internalEventDescriptors)if(Rt(t,e))return!0;return!1}_getPreInitialState(t,e){const{context:n}=this.config,s=_t({context:"function"!=typeof n&&n?n:{},_nodes:[this.root],children:{},status:"active"},this);if("function"==typeof n){const o={},i=n({spawn:fe(t,s,e,o),input:e.input,self:t.self,actorSources:this.implementations.actorSources}),[r]=S(s,e,t,[]);return i&&(r.context=i),Object.keys(o).length>0&&(r.children={...r.children,...o}),r}return s}getInitialSnapshot(t,e){return this.initialTransition(e,t)[0]}initialTransition(t,e){const n=c(t),s=[],o=this._getPreInitialState(e,n),[i,r]=Y(this.root,o,e,n,s),{snapshot:a,microsteps:u}=st(i,n,e,s);return[a,[...r,...u.flatMap(([,t])=>t)]]}start(t){if(t?.children)for(const e of Object.values(t.children))e._rehydrated&&"active"===e.getSnapshot?.().status&&e.start()}getStateNodeById(t){const e=Et(t),n=e.slice(1),s=P(e[0])?e[0].slice(1):e[0],o=this.idMap.get(s);if(!o)throw new Error(`Child state node '#${s}' does not exist on machine '${this.id}'`);return z(o,n)}getPersistedSnapshot(t,e){return function(t,e){const{_nodes:n,_stateInputs:s,tags:o,machine:i,children:r,context:c,can:a,hasTag:u,matches:f,getMeta:h,getInputs:d,toJSON:l,...p}=t,y={};for(const t in r){const n=r[t];y[t]={snapshot:n.getPersistedSnapshot(e),src:n.src,registryKey:n.registryKey,syncSnapshot:n._syncSnapshot}}const g={...p,context:St(c),children:y,historyValue:wt(p.historyValue)};return void 0!==i.version&&(g.version=i.version),g}(t,e)}restoreSnapshot(t,e){const n=t.version;if(n!==this.version){const e=this.config.migrate;if("function"!=typeof e)throw new Error(`Persisted snapshot version '${n}' does not match machine version '${this.version}'.`);t=e(t,n)}const s=t,o={},i=s.children;for(const t of Object.keys(i)){const n=i[t],s=n.snapshot,r=n.src,c="string"==typeof r?Ct(this,r):r;if(!c)continue;const a=te(c,{id:t,parent:e.self,syncSnapshot:n.syncSnapshot,snapshot:s,src:r,registryKey:n.registryKey});a._rehydrated=!0,o[t]=a}const r=(t=>{if(!t||"object"!=typeof t)return{};const e={};for(const n of Object.keys(t)){const s=t[n];for(const t of s){let s;if(t instanceof ye)s=t;else try{s=this.root.machine.getStateNodeById(t.id)}catch{}s&&(e[n]??=[],e[n].push(s))}}return e})(s.historyValue),c=Array.from(M(B(this.root,s.value))),{version:a,...u}=t,f=_t({...u,children:o,_nodes:c,value:s.value,historyValue:r},this),h=new WeakSet;return function t(e){if(!h.has(e)){h.add(e);for(const n of Object.keys(e)){const s=e[n];if(s&&"object"==typeof s){if("xstate$$type"in s&&s.xstate$$type===Ut){e[n]=o[s.id];continue}t(s)}}}}(f.context),f}}function me(t){return new ve(t)}const xe={actions:{},actorSources:{},guards:{},delays:{}},_e=[],be=[],we=new Set(Object.values(f));function Se(t){return void 0===t?[]:Array.isArray(t)?t:[t]}function $e(t,e){return"function"==typeof t?t(e):t}function Ee(t,e,n){return"function"==typeof t?t({context:e,event:n}):t}function ke(t,e){for(const n of Object.keys(e))if(!Object.prototype.hasOwnProperty.call(t,n)||!Object.is(t[n],e[n]))return{...t,...e};return t}function Ae(t,e,n,s){return{status:t.status,output:t.output,error:t.error,value:e,context:n,input:t.input,children:t.children,_stateInput:s,machine:t.machine}}function je(t){return{status:"stopped",output:void 0,error:void 0,value:t.value,context:t.context,input:void 0,children:t.children,_stateInput:t._stateInput,machine:t.machine}}function Ie(t){const e=A(t);return void 0!==e?e:"string"==typeof t?parseFloat(t)||0:t}function Te(t){return!!t&&"object"==typeof t&&"string"==typeof t["@expr"]}function Oe(t){return Te(t)||function(t){return!!t&&"object"==typeof t&&"string"==typeof t["@code"]}(t)}function Me(t,e){return"number"==typeof e?`${t}[${e}]`:`${t}.${e}`}function Ne(t){const e={};if(!t)return e;for(const n of Object.keys(t)){const s=t[n];"number"==typeof s?e[n]=s:s&&"object"==typeof s&&"number"==typeof s.duration&&(e[n]=s.duration)}return e}function De(t){return void 0===t?[]:Array.isArray(t)?t:[t]}function Pe(t,e){Array.isArray(t)&&t.forEach((n,s)=>{if(void 0===n.when&&s!==t.length-1)throw new Error(`Choice fallback branch at ${e}[${s}] must be last.`)})}let Ce="session_"+Math.random().toString(36).slice(2),Re="";function Le(t,e,n,s){const o=`\nwith (context) {\n return (${e});\n}\n `.trim(),i=new Function("context","_event","_sessionid","_name","_ioprocessors","In",o),r=n&&n._scxmlExternal,c=n&&"@xstate.init"===n.type,a=n&&"string"==typeof n.type&&n.type.startsWith("xstate.done."),u=n&&n._scxmlEventType;return i(t,n&&!c?{name:n.type,type:u||(r?"external":"internal"),sendid:n._scxmlSendId,origin:r?"#_scxml_session":void 0,origintype:r?"http://www.w3.org/TR/scxml/#SCXMLEventProcessor":void 0,invokeid:n._scxmlInvokeId,data:a?n.output:void 0!==n._scxmlEventData?n._scxmlEventData:n}:void 0,Ce,Re,{scxml:{location:`#_scxml_${Ce}`}},t=>{if(!s)return!1;try{const e=s.getSnapshot?.();if(e?._nodes){const n=t.replace(/\./g,"$");return e._nodes.some(t=>t.id===n||t.id.endsWith("."+n))}}catch{}return!1})}function Ke(t,e){const n=Object.keys(t),s=n.map(t=>`let ${t} = context.${t};`).join("\n"),o=n.map(t=>`updates.${t} = ${t};`).join("\n");return new Function("context","updates",`\n${s}\n${e}\n${o}\nreturn updates;\n `)(t,{})}function We(t){return!Oe(t)&&t.type.startsWith("@xstate.")}function Ve(t){if("function"==typeof t)return{"@code":t.toString(),"@lang":"ts"};if(null===t||"object"!=typeof t)return"bigint"==typeof t||"symbol"==typeof t?void 0:t;if(function(t){return!!t&&"object"==typeof t&&"function"==typeof t.transition&&"function"==typeof t.getInitialSnapshot}(t))return;if(function(t){return!!t&&"object"==typeof t&&"~standard"in t}(t))return;if(Array.isArray(t))return t.map(Ve).filter(t=>void 0!==t);if(t.constructor!==Object&&void 0!==t.constructor)return;const e={};for(const n of Object.keys(t)){const s=t[n];if(void 0!==s){const t=Ve(s);void 0!==t&&(e[n]=t)}}return e}function Je(t){if(Array.isArray(t)){const e=t.map(Je).filter(t=>void 0!==t);return e.length?e:void 0}const e=t,n={};for(const t of Object.keys(e)){const s=e[t];void 0!==s&&(n[t]="src"===t&&"string"!=typeof s?s.id:Ve(s),void 0===n[t]&&delete n[t])}return void 0===n.src?void 0:n}function ze(t){if(!t)return;const e={};for(const n of Object.keys(t)){const s=t[n];e[n]="function"==typeof s?void 0:Ve(s),void 0===e[n]&&delete e[n]}return e}function Be(t){const e={};for(const n of Object.keys(t)){if("states"===n||"invoke"===n)continue;const s=t[n];void 0!==s&&(e[n]=Ve(s))}if(void 0!==t.invoke){const n=Je(t.invoke);void 0!==n&&(e.invoke=n)}if(t.states){const n={};for(const e of Object.keys(t.states))n[e]=Be(t.states[e]);e.states=n}return e}function Fe(t){const e=Be(t);if(void 0!==t.internalEvents&&(e.internalEvents=Ve(t.internalEvents)),t.schemas){const n={};for(const e of Object.keys(t.schemas)){const s=t.schemas[e];n[e]=s&&"object"==typeof s&&!("~standard"in s)?ze(s):Ve(s),void 0===n[e]&&delete n[e]}e.schemas=n}for(const n of["actions","guards","actorSources","delays"])if(t[n]){const s=ze(t[n]);s&&Object.keys(s).length?e[n]=s:delete e[n]}return e}function qe(t={}){const{states:e={},schemas:n,actions:s,actorSources:o,guards:i,delays:r}=t;return{extend:e=>qe(function(t,e){return{...t,...e,schemas:Ge(t.schemas,e.schemas),states:Ue(t.states,e.states),actions:Ue(t.actions,e.actions),actorSources:Ue(t.actorSources,e.actorSources),guards:Ue(t.guards,e.guards),delays:Ue(t.delays,e.delays)}}(t,e)),createMachine(t){const e=Ge(t.schemas,n),c=Ue(s,t.actions),a=Ue(o,t.actorSources),u=Ue(i,t.guards),f=Ue(r,t.delays);return new ve({...t,...e?{schemas:e}:void 0,...c?{actions:c}:void 0,...a?{actorSources:a}:void 0,...u?{guards:u}:void 0,...f?{delays:f}:void 0})},createStateConfig:t=>t,states:e}}function Ue(t,e){return t||e?{...t,...e}:void 0}function Ge(t,e){if(t||e)return{...t,...e,events:Ue(t?.events,e?.events),emitted:Ue(t?.emitted,e?.emitted),children:Ue(t?.children,e?.children)}}function Xe(t){const e=te(t);return{self:e,defer:()=>{},id:"",logger:()=>{},sessionId:"",stopChild:()=>{},system:e.system,emit:()=>{},actionExecutor:()=>{}}}let Ze=function(t){return t.Internal="#_internal",t}({});const He={timeout:1/0};t.Actor=Qt,t.SimulatedClock=class{constructor(){this.timeouts=new Map,this._now=0,this._id=0,this._flushing=!1,this._flushingInvalidated=!1}now(){return this._now}getId(){return this._id++}setTimeout(t,e){this._flushingInvalidated=this._flushing;const n=this.getId();return this.timeouts.set(n,{start:this.now(),timeout:e,fn:t}),n}clearTimeout(t){this._flushingInvalidated=this._flushing,this.timeouts.delete(t)}set(t){if(this._now>t)throw new Error("Unable to travel back in time");this._now=t,this.flushTimeouts()}flushTimeouts(){if(this._flushing)return void(this._flushingInvalidated=!0);this._flushing=!0;const t=[...this.timeouts].sort(([t,e],[n,s])=>{const o=e.start+e.timeout;return s.start+s.timeout>o?-1:1});for(const[e,n]of t){if(this._flushingInvalidated)return this._flushingInvalidated=!1,this._flushing=!1,void this.flushTimeouts();this.now()-n.start>=n.timeout&&(this.timeouts.delete(e),n.fn.call(null))}this._flushing=!1,this._flushingInvalidated&&(this._flushingInvalidated=!1,this.flushTimeouts())}increment(t){this._now+=t,this.flushTimeouts()}},t.SpecialTargets=Ze,t.StateMachine=ve,t.StateNode=ye,t.TimeoutError=ce,t.__unsafe_getAllOwnEventDescriptors=function(t){return[...new Set([...t._nodes.flatMap(t=>t.ownEvents)])]},t.assertEvent=function(t,e){const n=It(e);if(!n.some(e=>Rt(t.type,e))){const e=1===n.length?`type matching "${n[0]}"`:`one of types matching "${n.join('", "')}"`;throw new Error(`Expected event ${JSON.stringify(t)} to have ${e}`)}},t.checkStateIn=function(t,e){if("string"==typeof e&&P(e)){const n=t.machine.getStateNodeById(e);return t._nodes.some(t=>t===n)}return t.matches(e)},t.createActor=te,t.createAsyncLogic=function(t){const e=t;return Ft({id:e.id,schemas:e.schemas,context:void 0,run:({event:t,input:s,self:o,system:i},r)=>{switch(t.type){case ie:{const e=t.data??t.output;return{status:"done",output:e,input:void 0,effects:{async:{status:"done",output:e}}}}case re:{const e=t.data??t.error;return{status:"error",error:e,input:void 0,effects:{async:{status:"error",error:e}}}}}if(t.type===n)return r.effect(()=>{const t=o,n=new AbortController,c=e.timeout,a=A(c),u=void 0===a?void 0:i._clock.setTimeout(()=>{"active"===o.getSnapshot().status&&(n.abort(),i._relay(t,t,{type:re,data:new ce(c)}))},a),f=()=>{void 0!==u&&i._clock.clearTimeout(u)};return Promise.resolve(e.run({input:s,system:i,self:o,signal:n.signal},{emit:r.emit,step:async(e,n)=>{const s=o.getSnapshot().effects?.[e];if("done"===s?.status)return s.output;if("error"===s?.status)throw s.error;if("active"===s?.status)return function(t,e){return new Promise((n,s)=>{const o=t.subscribe(t=>{const i=t.effects?.[e];"done"===i?.status?(o.unsubscribe(),n(i.output)):"error"===i?.status&&(o.unsubscribe(),s(i.error))})})}(o,e);i._relay(t,t,{type:Jt,key:e});try{const s=await n();return i._relay(t,t,{type:Wt,key:e,output:s}),s}catch(n){throw i._relay(t,t,{type:Vt,key:e,error:n}),n}}})).then(e=>{f(),"active"===o.getSnapshot().status&&i._relay(t,t,{type:ie,data:e})},e=>{f(),"active"===o.getSnapshot().status&&i._relay(t,t,{type:re,data:e})}),()=>{n.abort(),f()}}),{effects:{async:{status:"active"}}}}})},t.createCallbackLogic=function(t){const e="function"==typeof t?t:t.run;return Ft({schemas:"function"==typeof t?void 0:t.schemas,context:void 0,run:(t,n)=>{const{event:s,input:o,self:i,system:r}=t,c=t.emit,a=ee.get(i);a?.receivers?.forEach(t=>t(s)),n.effect("callback",()=>{const t={receivers:void 0,dispose:void 0};return ee.set(i,t),t.dispose=e({input:o,system:r,self:i,sendBack:t=>{if("stopped"===i.getSnapshot().status)return;const e=i._parent;e&&r._relay(i,e,t)},receive:e=>{t.receivers??=new Set,t.receivers.add(e)},emit:c}),()=>{ee.delete(i),t.receivers?.clear(),t.dispose?.()}})}})},t.createEmptyActor=ue,t.createEventObservableLogic=function(t){const e="function"==typeof t?t:t.run,n={config:e,transition:(t,e)=>{if("active"!==t.status)return[t,[]];switch(e.type){case se:return[{...t,status:"error",error:e.data,input:void 0,_subscription:void 0},[]];case oe:return[{...t,status:"done",input:void 0,_subscription:void 0},[]];case s:return t._subscription.unsubscribe(),[{...t,status:"stopped",input:void 0,_subscription:void 0},[]];default:return[t,[]]}},initialTransition:(t,e)=>[{status:"active",output:void 0,error:void 0,context:void 0,input:t,_subscription:void 0},[]],getInitialSnapshot:(t,e)=>n.initialTransition(e,t)[0],start:(t,{self:n,system:s,emit:o})=>{"done"!==t.status&&(t._subscription=e({input:t.input,system:s,self:n,emit:o}).subscribe({next:t=>{n._parent&&s._relay(n,n._parent,t)},error:t=>{s._relay(n,n,{type:se,data:t})},complete:()=>{s._relay(n,n,{type:oe})}}))},getPersistedSnapshot:({_subscription:t,...e})=>e,restoreSnapshot:t=>({...t,_subscription:void 0})};return n},t.createFSM=function(t){const e={id:t.id??"(fsm)",implementations:xe};!function(t){for(const[e,n]of Object.entries(t.states))for(const[t,s]of Object.entries(n.on??{}))for(const n of Se(s))if("string"==typeof n)throw new Error(`Invalid transition for "${e}.${t}": use { target: "${n}" } instead of a string target.`)}(t);const o=(t,e,n,s)=>{if(!s?.length)return[t,_e];const o=function(t){const e=[];for(const n of t){if(!n||"object"!=typeof n||!("action"in n)||"function"!=typeof n.action||we.has(n.action)||"_special"in n.action)return;e.push({type:n.action.name||"(anonymous)",params:void 0,args:n.args,exec:n.args.length?n.action.bind(null,...n.args):n.action})}return e}(s);return o?[t,o]:S(t,e,n,s)},i=(t,e,n,s,i,r)=>{if(!s)return[t,_e];const c=[],a=b(n,c,r,!0);let u;const f=Array.isArray(s)?s.length:1;for(let o=0;o<f;o++){const r=(Array.isArray(s)?s[o]:s)({context:u??t.context,event:e,input:i,value:t.value,self:n.self,system:n.system,parent:n.self._parent,children:t.children},a);if(void 0!==r?.context){const e=u??t.context,n=ke(e,r.context);n!==e&&(u=n)}}const h=void 0!==u?Ae(t,t.value,u,t._stateInput):t;return o(h,e,n,c)},r=(e,n,s,o)=>{const i=t.states[e.value],r=i?.on?.[n.type];if(!r)return;const c=Array.isArray(r)?r.length:1;for(let t=0;t<c;t++){const i=Array.isArray(r)?r[t]:r,c={context:e.context,event:n,input:e.input,value:e.value,self:s.self,system:s.system,parent:s.self._parent,children:e.children};if("function"==typeof i){const t=[],e=i(c,b(s,t,o,!0));if(!e){if(t.length)return{actions:t};continue}return{target:e.target,context:e.context,input:e.input,actions:t}}if(!("guard"in i)||!i.guard||i.guard(c))return{target:i.target,context:$e(i.context,c),input:i.input,actions:"actions"in i&&i.actions?Se(i.actions):be}}},c=(e,n,c)=>{if("active"!==e.status)return[e,[]];if(n.type===s)return[je(e),_e];const a=t.states[e.value],u=a?.on?.[n.type];if(!u)return[e,_e];if(!(Array.isArray(u)||"function"==typeof u||"guard"in u||"actions"in u&&u.actions||"function"==typeof u.input)){const s=u.target??e.value,o=s!==e.value;if(!o||!a.exit&&!t.states[s]?.entry){const t=void 0!==u.context,i=void 0!==u.input,r=$e(u.context,{context:e.context,event:n,input:e.input,value:e.value,self:c.self,system:c.system,parent:c.self._parent,children:e.children}),a=t&&r?ke(e.context,r):e.context;return o||a!==e.context||i||void 0!==e._stateInput?[Ae(e,s,a,i?Ee(u.input,a,n):void 0),_e]:[e,_e]}}if("function"==typeof u&&u.length<2&&!a?.exit){const s=u({context:e.context,event:n,input:e.input,value:e.value,self:c.self,system:c.system,parent:c.self._parent,children:e.children},void 0);if(s){const o=s.target??e.value;if(!t.states[o]?.entry){const t=void 0!==s.context,i=void 0!==s.input,r=t&&s.context?ke(e.context,s.context):e.context;return o!==e.value||r!==e.context||i||void 0!==e._stateInput?[Ae(e,o,r,i?Ee(s.input,r,n):void 0),_e]:[e,_e]}}}let f=e;const h=[],d=[n];let l=0;for(;d.length;){if(++l>1e3)throw new Error("FSM microstep count exceeded 1000");const e=d.shift(),n=r(f,e,c,d);if(!n)continue;const s=n.target??f.value,a=s!==f.value;if(a){const[n,s]=i(f,e,c,t.states[f.value]?.exit,f._stateInput,d);f=n,h.push(...s)}let u=f.context;void 0!==n.context&&(u=ke(u,n.context));const p=void 0!==n.input,y=p?Ee(n.input,u,e):void 0;(a||u!==f.context||p||void 0!==f._stateInput)&&(f=Ae(f,s,u,y));const[g,v]=o(f,e,c,n.actions);if(f=g,h.push(...v),a){const[n,o]=i(f,e,c,t.states[s]?.entry,y,d);f=n,h.push(...o)}}return[f,h]},a={id:t.id,config:t,transition:c,initialTransition:(s,o)=>{const r=function(t,e){return"function"==typeof t?t({input:e}):t??{}}(t.context,s),a=function(t,e,n,s,o){return{status:"active",output:void 0,error:void 0,value:t,context:e,input:n,children:{},_stateInput:o,machine:s}}(t.initial,r,s,e),u=[];let[f,h]=i(a,{type:n},o,t.states[t.initial]?.entry,void 0,u);for(h.length||(h=[]);u.length;){const[t,e]=c(f,u.shift(),o);f=t,h.push(...e)}return[f,h]},getInitialSnapshot:(t,e)=>a.initialTransition(e,t)[0],getPersistedSnapshot:({machine:t,...e})=>e,restoreSnapshot:t=>({...t,machine:e})};return a},t.createListenerLogic=p,t.createLogic=Ft,t.createMachine=me,t.createMachineFromConfig=function t(e,n={}){const s=function(t,e){return{actions:e.actions??{},guards:e.guards??{},actorSources:e.actorSources??{},delays:{...Ne(t.delays),...e.delays??{}},evaluators:e.evaluators??{}}}(e,n),o=function(t,e){function n(n,s){const o=n["@lang"]??t;if(!o)throw new Error(`Missing @exprLang for expression at ${s}`);const i=e.evaluators[o];if(!i)throw new Error(`Missing evaluator for @lang '${o}' at ${s}`);return i}function s(t,e,s,o){const i=Te(t)?"expr":"code",r=Te(t)?t["@expr"]:t["@code"];return n(t,o)({source:r,kind:i,slot:e,scope:s,path:o})}function o(t,e,n,i){if(Oe(t))return s(t,e,n,i);if(Array.isArray(t))return t.map((t,s)=>o(t,e,n,Me(i,s)));if(!t||"object"!=typeof t)return t;const r={};for(const s of Object.keys(t))r[s]=o(t[s],e,n,Me(i,s));return r}function i(t,e){return{context:t.context,event:t.event,input:t.input,self:t.self,children:t.children,params:t.params,...e}}return{evaluateResolvable:s,resolveValue:o,makeScope:i,getDurationConfig:function(t,e){return Oe(t)?n=>Ie(o(t,"delay",i(n),e)):t},assertResolvable:function t(e,s){if(Oe(e))n(e,s);else if(Array.isArray(e))e.forEach((e,n)=>t(e,Me(s,n)));else if(e&&"object"==typeof e)for(const n of Object.keys(e))t(e[n],Me(s,n))}}}(e["@exprLang"],s),{evaluateResolvable:i,resolveValue:r,makeScope:c,getDurationConfig:a}=o;function u(t,n,s){if(t)return Oe(t)?e=>!!i(t,n,c(e),s):o=>{const i=r(t.params,n,c(o),`${s}.params`),a=e.guards?.[t.type];if(a){const e=u(a.when,"guard",`$.guards.${t.type}.when`);return!!e?.({...o,params:i})}const f=o.guards?.[t.type];if(!f)throw new Error((h=t.type,d=o.guards??{},`Guard '${h}' is not implemented in machine '${e.id??"(machine)"}'. Available guards: ${Object.keys(d).map(t=>`'${t}'`).join(", ")||"(none)"}.`));var h,d;return f(o,i)}}function f(t,e){if(t)return Oe(t)?n=>i(t,"choice",c(n),e):(Pe(t,e),n=>{for(let s=0;s<t.length;s++){const o=t[s],i=u(o.when,"choice",`${e}[${s}].when`);if(!i||i(n))return{target:o.target,context:o.context?r(o.context,"transitionContext",c(n),`${e}[${s}].context`):void 0,input:void 0!==o.input?r(o.input,"input",c(n),`${e}[${s}].input`):void 0,description:o.description,reenter:o.reenter,meta:o.meta}}throw new Error(`Choice state at ${e} did not match any branch.`)})}const h={},d=[];let l=null;const p=[];function y(t,e){if(!t||"function"==typeof t)return t;if(Oe(t))return n=>i(t,"transition",c(n),e);const{guard:n,...s}=t;if(!n)return s;const o=u({type:n},"guard",e);return t=>o(t)?s:void 0}function g(e){return(Array.isArray(e)?e:[e]).map(e=>{const n=e;let o;return o=n._nestedMachineJSON?t(n._nestedMachineJSON,s):s.actorSources[e.src]??e.src,{src:o,id:e.id,registryKey:e.registryKey,input:void 0!==e.input?t=>r(e.input,"input",c(t),"$.invoke.input"):void 0,onDone:e.onDone?_(e.onDone):void 0,onError:e.onError?_(e.onError):void 0,onSnapshot:e.onSnapshot?_(e.onSnapshot):void 0,timeout:a(e.timeout,"$.invoke.timeout"),onTimeout:e.onTimeout?_(e.onTimeout):void 0}})}function v(t,e,n){const s=n instanceof Error?n.message:"string"==typeof n?n:"unknown error";t.enq.raise({type:"error.execution",_scxmlEventType:"platform",_scxmlEventData:{tagname:e,message:s,line:NaN,column:NaN,reason:s}}),t.errored=!0}function m(t,n,s,o,i,r){const c=e.actions?.[t.type];if(!c)return s(n.actions[t.type],o),{context:void 0,errored:i.errored};if(r.includes(t.type))return v(i,"action",new Error(`Circular action reference: ${r.concat(t.type).join(" -> ")}`)),{context:void 0,errored:!0};return x(Array.isArray(c)?c:[c],{...n,params:o},s,i,r.concat(t.type))}function x(t,e,n,s,o=[]){const a=s??{enq:n,errored:!1};let u;for(const s of t){if(a.errored)break;if(Oe(s)){const t=i(s,"action",c(e,{params:e.params,enq:n}),"$.actions");t&&"object"==typeof t&&"context"in t&&t.context&&(u??={},Object.assign(u,t.context))}else if(We(s))switch(s.type){case"@xstate.raise":{const t=void 0!==s.delay,o=r(s.event,"actionParams",c(e,{params:e.params}),"$.actions.event"),i=t||void 0!==s.id?{...o,...t?{_scxmlExternal:!0}:{},...void 0!==s.id?{_scxmlSendId:s.id}:{}}:o;n.raise(i,{id:s.id,delay:s.delay});break}case"@xstate.cancel":n.cancel(s.id);break;case"@xstate.log":n.log(...s.args);break;case"@xstate.emit":n.emit(r(s.event,"actionParams",c(e,{params:e.params}),"$.actions.event"));break;case"@xstate.assign":u??={},Object.assign(u,r(s.context,"transitionContext",c(e,{params:e.params}),"$.actions.context"));break;default:throw new Error(`Unknown built-in action: ${s.type}`)}else if("scxml.assign"===s.type){const t=s,n={...e.context,...u};let o;try{o=Le(n,t.expr,e.event,e.self)}catch(t){v(a,"assign",t);continue}if(t.location.includes(".")){v(a,"assign",new Error(`Invalid assign location: ${t.location}`));continue}u??={},u[t.location]=o}else if("scxml.raise"===s.type){const t=s,o={...e.context,...u};let i,r,c,f;try{if(i=t.eventexpr?Le(o,t.eventexpr,e.event,e.self):t.event||"unknown",r={type:i},t.params)for(const n of t.params)r[n.name]=Le(o,n.expr,e.event,e.self);c=t.targetexpr?Le(o,t.targetexpr,e.event,e.self):t.target;const n="#_internal"===c;f=t.delayexpr?Ie(Le(o,t.delayexpr,e.event,e.self)):n?void 0:t.delay}catch(t){v(a,"send",t);continue}const h="#_internal"===c,d="#_parent"===c;if("string"==typeof c&&c.length>0&&!c.startsWith("#_")){v(a,"send",new Error(`Invalid send target: ${c}`));continue}if(void 0!==t.id&&(r._scxmlSendId=t.id),d&&e.parent)n.sendTo(e.parent,r,void 0!==f?{delay:f}:void 0);else if("string"!=typeof c||!c.startsWith("#_")||d||h)void 0!==f&&(r._scxmlExternal=!0),n.raise(r,{id:t.id,delay:f});else{const s=c.slice(2),o=e.children?.[s];o?n.sendTo(o,r):(void 0!==f&&(r._scxmlExternal=!0),n.raise(r,{id:t.id,delay:f}))}}else if("scxml.cancel"===s.type){const t=s,o={...e.context,...u};try{const s=Le(o,t.sendidexpr,e.event,e.self);s&&n.cancel(s)}catch{}}else if("scxml.script"===s.type){const t=s,n={...e.context,...u};try{const e=Ke(n,t.code);u??={},Object.assign(u,e)}catch(t){v(a,"script",t)}}else if("scxml.foreach"===s.type){const t=s,o={...e.context,...u},i=t=>/^[$_a-zA-Z][$_a-zA-Z0-9]*$/.test(t);if(!i(t.item)||t.index&&!i(t.index)){v(a,"foreach",new Error("foreach item/index is not a legal location"));continue}let r;try{r=Le(o,t.array,e.event,e.self)}catch(t){v(a,"foreach",t);continue}if(!Array.isArray(r)){v(a,"foreach",new Error("foreach array is not iterable"));continue}u??={};for(let s=0;s<r.length;s++){u[t.item]=r[s],t.index&&(u[t.index]=s);const o={...e,context:{...e.context,...u}},i=x(t.actions,o,n,a);if(i.context&&Object.assign(u,i.context),a.errored)break}}else if("scxml.block"===s.type){const t=s,o={...e,context:{...e.context,...u}},i=x(t.actions,o,n);if(i.context){u??={};for(const t of Object.keys(i.context))i.context[t]!==e.context[t]&&(u[t]=i.context[t])}}else if("scxml.if"===s.type){const t=s,o={...e.context,...u};for(const s of t.branches){let t;if(s.cond)try{t=!!Le(o,s.cond,e.event,e.self)}catch(e){v(a,"if",e),t=!1}else t=!0;if(t){if(s.actions.length){const t={...e,context:o},i=x(s.actions,t,n,a);if(i.context){u??={};for(const t of Object.keys(i.context))i.context[t]!==e.context[t]&&(u[t]=i.context[t])}}break}}}else{const t=m(s,e,n,r(s.params,"actionParams",c(e,{params:e.params}),"$.actions.params"),a,o);t.context&&(u??={},Object.assign(u,t.context))}}return{context:u?{...e.context,...u}:void 0,errored:a.errored}}function _(t){const e=Array.isArray(t)?t:[t];return e.map((t,n)=>{if(Oe(t))return(s,o)=>i(t,"transition",c(s,{enq:o}),"$.transition"+(e.length>1?`[${n}]`:""));const s=Array.isArray(t.target)?t.target[0]:t.target,o=t.target,a=u(t.guard,"guard","$.transition.guard"),f=e=>t.context?r(t.context,"transitionContext",c(e),"$.transition.context"):void 0,p=e=>void 0!==t.input?r(t.input,"input",c(e),"$.transition.input"):void 0;return a||t.actions?.length?a&&!t.actions?.length?t.context?{guard:a,to:e=>({target:o,context:f(e),reenter:t.reenter}),description:t.description,meta:t.meta,input:void 0!==t.input?p:void 0}:{target:o,guard:a,description:t.description,reenter:t.reenter,meta:t.meta,input:void 0!==t.input?p:void 0}:s?{guard:a,description:t.description,reenter:t.reenter,meta:t.meta,input:void 0!==t.input?p:void 0,to:(e,n)=>{const i=f(e);if(t.actions?.length){const e=s.replace(/^#/,"");h[e]=t.actions,d.includes(t.actions)||d.push(t.actions)}return{target:o,context:i,reenter:t.reenter}}}:{guard:a,description:t.description,reenter:t.reenter,meta:t.meta,input:void 0!==t.input?p:void 0,to:(e,n)=>{const s=f(e);if(t.actions?.length){d.includes(t.actions)||d.push(t.actions),l??=e.context;const o=x(t.actions,e,n);if(o.context)return{context:{...s,...o.context}}}return s?{context:s}:{}}}:t.context?{to:e=>({target:o,context:f(e),reenter:t.reenter}),description:t.description,meta:t.meta,input:void 0!==t.input?p:void 0}:{target:o,description:t.description,reenter:t.reenter,meta:t.meta,input:void 0!==t.input?p:void 0}})}if(e.delays)for(const t of Object.keys(e.delays)){const n=e.delays[t];"number"==typeof n?s.delays[t]=n:"string"==typeof n?s.delays[t]=Ie(n):n&&"object"==typeof n&&(s.delays[t]=e=>Ie(r(n.duration,"delay",c(e),`$.delays.${t}.duration`)))}!function(t,e,n){const{assertResolvable:s}=n;function o(n,i){if(n)if(Oe(n))s(n,i);else if(s(n.params,`${i}.params`),t.guards?.[n.type])o(t.guards[n.type].when,`$.guards.${n.type}.when`);else if(!e.guards[n.type]&&!["scxml.cond","xstate.stateIn","xstate.not"].includes(n.type))throw new Error(`Missing guard implementation "${n.type}"`)}function i(n,o,r=[]){if(Oe(n))return void s(n,o);if(!n||"string"!=typeof n.type)throw new Error(`Invalid action at ${o}`);if(s(n.params,`${o}.params`),n.type.startsWith("@xstate.")||n.type.startsWith("scxml."))return void s(n,o);const c=t.actions?.[n.type];if(c){if(r.includes(n.type))throw new Error(`Circular action reference: ${r.concat(n.type).join(" -> ")}`);const t=Array.isArray(c)?c:[c];return void t.forEach((e,s)=>i(e,`${o}.actions.${n.type}${t.length>1?`[${s}]`:""}`,r.concat(n.type)))}if(!e.actions[n.type])throw new Error(`Missing action implementation "${n.type}"`)}function r(t,e){De(t).forEach((t,n)=>i(t,`${e}[${n}]`))}function c(t,e){(Array.isArray(t)?t:t?[t]:[]).forEach((n,i)=>{const c=Array.isArray(t)?`${e}[${i}]`:e;Oe(n)?s(n,c):(o(n.guard,`${c}.guard`),r(n.actions,`${c}.actions`),s(n.context,`${c}.context`),s(n.input,`${c}.input`))})}if(t.actions)for(const e of Object.keys(t.actions)){const n=Array.isArray(t.actions[e])?t.actions[e]:[t.actions[e]];n.forEach((t,s)=>i(t,`$.actions.${e}${n.length>1?`[${s}]`:""}`,[e]))}if(t.guards)for(const e of Object.keys(t.guards))o(t.guards[e].when,`$.guards.${e}.when`);if(t.delays)for(const e of Object.keys(t.delays)){const n=t.delays[e];s(n&&"object"==typeof n&&"duration"in n?n.duration:n,`$.delays.${e}`)}!function n(i,a){if(s(i.context,`${a}.context`),s(i.input,`${a}.input`),s(i.output,`${a}.output`),s(i.timeout,`${a}.timeout`),r(i.entry,`${a}.entry`),r(i.exit,`${a}.exit`),"choice"===i.type){if(!i.choice)throw new Error(`Choice state at ${a} must declare choice.`);Array.isArray(i.choice)?(Pe(i.choice,`${a}.choice`),i.choice.forEach((t,e)=>{o(t.when,`${a}.choice[${e}].when`),s(t.context,`${a}.choice[${e}].context`),s(t.input,`${a}.choice[${e}].input`)})):s(i.choice,`${a}.choice`)}if(Oe(i.route)&&s(i.route,`${a}.route`),i.invoke&&(Array.isArray(i.invoke)?i.invoke:[i.invoke]).forEach((t,n)=>{const o=`${a}.invoke${Array.isArray(i.invoke)?`[${n}]`:""}`;if(!t._nestedMachineJSON&&!e.actorSources[t.src])throw new Error(`Missing actorSource implementation "${t.src}"`);s(t.input,`${o}.input`),s(t.timeout,`${o}.timeout`),c(t.onDone,`${o}.onDone`),c(t.onError,`${o}.onError`),c(t.onSnapshot,`${o}.onSnapshot`),c(t.onTimeout,`${o}.onTimeout`)}),i.on)for(const t of Object.keys(i.on))c(i.on[t],`${a}.on.${t}`);if(i.after)for(const n of Object.keys(i.after)){if(Number.isNaN(Number(n))&&void 0===A(n)&&!t.delays?.[n]&&!e.delays[n])throw new Error(`Missing delay implementation "${n}"`);c(i.after[n],`${a}.after.${n}`)}if(c(i.always,`${a}.always`),c(i.onTimeout,`${a}.onTimeout`),i.states)for(const t of Object.keys(i.states))n(i.states[t],`${a}.states.${t}`)}(t,"$")}(e,s,o);const b=function t(e,n){const s=De(e.entry),o=e.id||n;var r,c;return{id:e.id,initial:e.initial,type:e.type,history:e.history,target:e.target,description:e.description,tags:e.tags,input:e.input,timeout:a(e.timeout,`$.states.${n}.timeout`),states:e.states?Object.entries(e.states).reduce((e,[n,s])=>(e[n]=t(s,n),e),{}):void 0,on:e.on?Object.entries(e.on).reduce((t,[e,n])=>(t[e]=_(n),t),{}):void 0,always:e.always?_(e.always):void 0,choice:f(e.choice,`$.states.${n}.choice`),route:y(e.route,`$.states.${n}.route`),after:e.after?Object.entries(e.after).reduce((t,[e,n])=>(t[e]=_(n),t),{}):void 0,onTimeout:e.onTimeout?_(e.onTimeout):void 0,entry:"choice"===e.type?void 0:(t,e)=>{for(;p.length;){const t=p.shift();e.raise({type:"error.execution",_scxmlEventType:"platform",_scxmlEventData:t})}let n;if(l&&d.length>0&&Object.keys(h).length>0){let s=l;for(const n of d){const o=x(n,{...t,context:s},e);o.context&&(s=o.context)}d.length=0,l=null;for(const t of Object.keys(h))delete h[t];n=s}else{const s=o?h[o]:void 0;if(s){delete h[o];const i=x(s,t,e);i.context&&(n=i.context)}l=null,d.length=0}if(s.length){const o=n?{...t,context:n}:t,i=x(s,o,e);i.context&&(n=i.context)}return{context:n}},exit:e.exit?(c=De(e.exit),(t,e)=>x(c,t,e)):void 0,invoke:e.invoke?g(e.invoke):void 0,meta:e.meta,output:e._scxmlDonedata?(r=e._scxmlDonedata,({context:t,event:e,self:n})=>{try{if(r.params&&r.params.length){const s={};for(const o of r.params)s[o.name]=Le(t,o.expr,e,n);return s}if(void 0!==r.contentExpr)return Le(t,r.contentExpr,e,n);if(void 0!==r.contentText)try{return Le(t,r.contentText,e,n)}catch{return r.contentText}}catch(t){const e=t instanceof Error?t.message:"string"==typeof t?t:"unknown error";p.push({tagname:"donedata",message:e,line:NaN,column:NaN,reason:e})}}):Oe(e.output)?({context:t,event:s,self:o})=>i(e.output,"output",{context:t,event:s,self:o},`$.states.${n}.output`):e.output}}(e),w=e.context?{context:t=>r(e.context,"context",t,"$.context")}:{};Re=e.id||"",Ce="session_"+Math.random().toString(36).slice(2);const S={"scxml.cond":({context:t,event:e,self:n},s)=>{const o=s?.expr;if(!o)return!0;try{return!!Le(t,o,e,n)}catch(t){const e=t instanceof Error?t.message:"string"==typeof t?t:"unknown error";return p.push({tagname:"cond",message:e,line:NaN,column:NaN,reason:e}),!1}},"xstate.stateIn":(t,e)=>{const n=e?.stateId;if(!n)return!1;const s=n.replace(/^#/,""),o=t._snapshot;return!!o?._nodes&&o._nodes.some(t=>t.id===s)},"xstate.not":(t,e)=>{const n=e?.guard,s=n&&t.guards?.[n.type];if(!s)throw new Error(`Guard '${n?.type}' referenced by 'xstate.not' is not implemented.`);return!s(t,n.params)}},$=me({...b,...w,version:e.version}).provide({actions:s.actions,actorSources:s.actorSources,guards:{...S,...s.guards},delays:s.delays});return $._json=e,$},t.createObservableLogic=function(t){const e="function"==typeof t?t:t.run;return Ft({schemas:"function"==typeof t?void 0:t.schemas,context:void 0,run:(t,n)=>{const{event:s,input:o,self:i,system:r}=t,c=t.emit;switch(s.type){case ne:return{context:s.data};case se:return{status:"error",error:s.data,input:void 0,effects:{observable:{status:"error",error:s.data}}};case oe:return{status:"done",input:void 0,effects:{observable:{status:"done"}}}}n.effect("observable",()=>{const t=i,n=e({input:o,system:r,self:i,emit:c}).subscribe({next:e=>{r._relay(t,t,{type:ne,data:e})},error:e=>{r._relay(t,t,{type:se,data:e})},complete:()=>{r._relay(t,t,{type:oe})}});return()=>n.unsubscribe()})}})},t.createStateConfig=function(t){return t},t.createSubscriptionLogic=g,t.createSystem=function(t={}){const e={},n=[];return{createActor(t,s){const o=te(t,{...s,_systemRef:e});return(()=>{const t=e.current;if(t)for(const e of n)e.active&&!e.subscription&&(e.subscription=t.inspect(e.observer))})(),o},get:t=>e.current?.get(t),getAll:()=>e.current?.getAll()??{},inspect(t){const s=e.current;if(s)return s.inspect(t);const o={observer:t,active:!0};return n.push(o),{unsubscribe(){o.active=!1,o.subscription?.unsubscribe()}}},setup:t=>t?qe(t):qe()}},t.getInitialMicrosteps=function(t,...[e]){const n=Xe(t),s=c(e),o=[],i=t._getPreInitialState(n,s),r=Y(t.root,i,n,s,o),{microsteps:a}=st(r[0],s,n,o);return[r,...a]},t.getInitialSnapshot=function(t,...[e]){const n=Xe(t);return t.initialTransition(e,n)[0]},t.getMicrosteps=function(t,e,n){const s=Xe(t),{microsteps:o}=st(e,n,s,[]);return o},t.getNextSnapshot=function(t,e,n){const s=Xe(t);s.self._snapshot=e;const o=t.transition(e,n,s);return Array.isArray(o)?o[0]:o},t.getNextTransitions=function(t){const e=[],n=t._nodes.filter(I),s=new Set;for(const t of n)for(const n of[t].concat(O(t,void 0)))if(!s.has(n.id)){s.add(n.id);for(const[,t]of n.transitions.entries())e.push(...t);n.always&&e.push(...n.always)}return e},t.getStateNodes=B,t.initialTransition=function(t,...[e]){const n=Xe(t),[s,o]=t.initialTransition(e,n);return[s,o]},t.isBuiltInExecutableAction=function(t){return Object.prototype.hasOwnProperty.call(f,t.type)},t.isMachineSnapshot=ut,t.isTypeSchema=function(t){return!!t&&"object"==typeof t&&"~standard"in t&&"xstate.types"===t["~standard"].vendor},t.listenerLogic=y,t.machineConfigToJSON=Fe,t.mapState=function(t,e){const n=[],s=(t,e)=>{let n=t;for(const t of e){if(!n?.states)return;const e=n.states;if(!(t in e))return;n=e[t]}return n},o=new Set;for(const i of t._nodes.filter(I)){let r=i;for(;r&&!o.has(r);){o.add(r);const i=s(e,r.path);i?.map&&n.push({stateNode:r,result:i.map(t)}),r=r.parent}}return n},t.matchesState=$t,t.pathToStateValue=At,t.serializeMachine=function(t){return t._json??Fe(t.config)},t.setup=qe,t.subscriptionLogic=v,t.toObserver=Pt,t.toPromise=function(t){return new Promise((e,n)=>{t.subscribe({complete:()=>{e(t.getSnapshot().output)},error:n})})},t.transition=function(t,e,n){const s=Xe(t),[o,i]=t.transition(e,n,s);return[o,i]},t.types=function(){return{"~standard":{version:1,vendor:"xstate.types",validate:t=>({value:t})}}},t.waitFor=function(t,e,n){const s={...He,...n};return new Promise((n,o)=>{const{signal:i}=s;if(i?.aborted)return void o(i.reason);let r=!1;const c=s.timeout===1/0?void 0:setTimeout(()=>{a(),o(new Error(`Timeout of ${s.timeout} ms exceeded`))},s.timeout),a=()=>{clearTimeout(c),r=!0,h?.unsubscribe(),f&&i.removeEventListener("abort",f)};function u(t){e(t)&&(a(),n(t))}let f,h;u(t.getSnapshot()),r||(i&&(f=()=>{a(),o(i.reason)},i.addEventListener("abort",f)),h=t.subscribe({next:u,error:t=>{a(),o(t)},complete:()=>{a(),o(new Error("Actor terminated without satisfying predicate"))}}),r&&h.unsubscribe())})},Object.defineProperty(t,"__esModule",{value:!0})});
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XState={})}(this,function(t){"use strict";class e{constructor(t){this._process=t,this._active=!1,this._current=null,this._last=null}start(){this._active=!0,this.flush()}clear(){this._current&&(this._current.next=null,this._last=this._current)}enqueue(t){const e={value:t,next:null};if(this._current)return this._last.next=e,void(this._last=e);this._current=e,this._last=e,this._active&&this.flush()}flush(){for(;this._current;){const t=this._current;this._process(t.value),this._current=t.next}this._last=null}}const n="@xstate.init",s="@xstate.stop";function o(t,e){return{type:`xstate.after.${t}.${e}`}}function i(t){return{type:`xstate.timeout.actor.${t}`}}function r(t,e){return{type:`xstate.done.state.${t}`,output:e}}function c(t){return{type:n,input:t}}function a(t){setTimeout(()=>{throw t})}const u="function"==typeof Symbol&&Symbol.observable||"@@observable",f={"@xstate.start":t=>{t.start()},"@xstate.raise":(t,e,n)=>{t.system.scheduler.schedule(t.self,t.self,e,n?.delay??0,n?.id)},"@xstate.sendTo":(t,e,n,s)=>{if("string"==typeof n)throw new Error("Only event objects may be used with sendTo");void 0!==s?.delay?t.system.scheduler.schedule(t.self,e,n,s?.delay??0,s?.id):t.defer(()=>{t.system._relay(t.self,e,n)})},"@xstate.cancel":(t,e)=>{t.system.scheduler.cancel(t.self,e)},"@xstate.stop":(t,e)=>{t.stopChild(e)}},h=new WeakMap;function d(t){const e=(t,e)=>[{status:"active",output:void 0,error:void 0,input:t},[]];return{start:(e,{self:n,system:s})=>{const o=e.input.actor;if(!o||"stopped"===o.getSnapshot().status)return;const i=t(e.input,{self:n,system:s});if(!i)return;let r,c=!1;const a=()=>{c||(c=!0,i.unsubscribe(),r?.unsubscribe())},u=n._parent;u&&(r=u.subscribe({complete:a,error:a})),h.set(n,{unsubscribe:a})},transition:(t,e,{self:n})=>e.type===s?(h.get(n)?.unsubscribe(),h.delete(n),[{...t,status:"stopped",error:void 0},[]]):[t,[]],initialTransition:e,getInitialSnapshot:(t,n)=>e(n)[0],getPersistedSnapshot:t=>t,restoreSnapshot:t=>t}}function l(t,e,n){"stopped"!==t.getSnapshot().status&&t._parent&&e._relay(t,t._parent,n())}function p(){return d(({actor:t,eventType:e,mapper:n},{self:s,system:o})=>{const i="*"!==e&&e.endsWith(".*");return t.on(i?"*":e,t=>{i&&!Rt(t.type,e)||l(s,o,()=>n(t))})})}const y=p();function g(){return d(({actor:t,mappers:e},{self:n,system:s})=>{const{done:o,error:i,snapshot:r}=e;return t.subscribe({next:t=>{"done"===t.status&&o?l(n,s,()=>o(t.output)):"error"===t.status&&i?l(n,s,()=>i(t.error)):"active"===t.status&&r&&l(n,s,()=>r(t))},error:t=>{i&&l(n,s,()=>i(t))},complete:()=>{}})})}const v=g();function m(t,e){return{...t,...e}}function x(t,e,...n){t.push({action:e,args:n})}function _(t,e,n){x(t,f["@xstate.start"],e),t.push(function(t,e){return Object.assign(function(n){return{children:{...n.children,[e]:t}}},{_special:!0})}(e,n??e.id))}function b(t,e,n,s=!1,o=!0){const i={cancel:n=>{x(e,f["@xstate.cancel"],t,n)},emit:t=>{e.push(t)},log:(...n)=>{x(e,t.logger,...n)},raise:(s,o)=>{if("string"==typeof s)throw new Error("Only event objects may be used with raise");void 0!==o?.delay?x(e,f["@xstate.raise"],t,s,o):n.push(s)},spawn:(n,s)=>{if(!o)return{id:s?.id??s?.registryKey??n.id};const i=te(n,{...s,parent:t.self});return _(e,i,s?.id),i},sendTo:(n,s,o)=>{n&&x(e,f["@xstate.sendTo"],t,n,s,o)},stop:n=>{n&&(x(e,f["@xstate.stop"],t,n),e.push(function(t){return Object.assign(function(e){const n={...e.children};for(const e of Object.keys(n))n[e]===t&&delete n[e];return{children:n}},{_special:!0})}(n)))}};return s&&Object.assign(i,{listen:(n,s,o)=>{const i=te(y,{input:{actor:n,eventType:s,mapper:o},parent:t.self});return x(e,f["@xstate.start"],i),i},subscribeTo:(n,s)=>{const o=te(v,{input:{actor:n,mappers:"function"==typeof s?{snapshot:s}:s},parent:t.self});return x(e,f["@xstate.start"],o),o}}),$(i,(t,...n)=>{x(e,t,...n)})}function w(t,e){switch(t){case f["@xstate.start"]:{const[t]=e;return{actor:t,id:t.id,logic:t.logic,src:t.src,input:t.options?.input}}case f["@xstate.raise"]:{const[,t,n]=e;return{event:t,id:n?.id,delay:n?.delay}}case f["@xstate.sendTo"]:{const[,t,n,s]=e;return{target:t,event:n,id:s?.id,delay:s?.delay}}case f["@xstate.cancel"]:{const[,t]=e;return{id:t}}case f["@xstate.stop"]:{const[,t]=e;return{actor:t}}default:return}}function S(t,e,n,s){let o=t;const i=[];for(const r of s){const s="function"==typeof r?r:"object"==typeof r&&"action"in r&&"function"==typeof r.action&&r.action.bind(null,...r.args),c={context:o.context,event:e,output:Ot(e),self:n.self,system:n.system,children:o.children,parent:n.self._parent,actions:t.machine.implementations.actions,actorSources:t.machine.implementations.actorSources};let a;if("object"==typeof r&&null!==r){const{type:t,...e}=r;a=e}if(s&&"_special"in s){i.push({type:"object"==typeof r?"action"in r&&"function"==typeof r.action?r.action.name??"(anonymous)":r.type??"(anonymous)":r.name||"(anonymous)",params:a,args:[],exec:void 0});const t=s(c,E);t&&("context"in t||"children"in t)&&(o=bt(o,{...void 0!==t.context?{context:m(o.context,t.context)}:{},..."children"in t?{children:t.children}:{}}));continue}if(!s||!("resolve"in s)){const t="object"==typeof r&&null!==r&&"action"in r&&"function"==typeof r.action?w(r.action,r.args):void 0;i.push({type:"object"==typeof r?"action"in r&&"function"==typeof r.action?r.action.name??"(anonymous)":r.type:r.name||"(anonymous)",params:a,args:"object"==typeof r&&"action"in r?r.args:[],exec:s||("object"==typeof r&&null!==r?()=>n.defer(()=>n.emit(r)):void 0),...t});continue}}return[o,i]}function $(t,e){const n=(t,...n)=>{e(t,...n)};return Object.assign(n,{cancel:()=>{},emit:()=>{},log:()=>{},raise:()=>{},spawn:()=>({}),sendTo:()=>{},stop:()=>{},listen:()=>({}),subscribeTo:()=>({}),...t}),n}const E=$({},()=>{});function k(t){const e=t.trim(),n=e.match(/^(\d+)ms$/i);if(n)return parseInt(n[1],10);const s=e.match(/^(\d*)(\.?)(\d*)s$/i);if(s){const t=s[1]?parseInt(s[1],10):0,e=!!s[2],n=s[3]?parseInt(s[3].padEnd(3,"0").slice(0,3),10):0;return 1e3*t+(e?n:0)}const o=e.match(/^P(?:(?<weeks>\d+(?:[.,]\d+)?)W)?(?:(?<days>\d+(?:[.,]\d+)?)D)?(?:T(?:(?<hours>\d+(?:[.,]\d+)?)H)?(?:(?<minutes>\d+(?:[.,]\d+)?)M)?(?:(?<seconds>\d+(?:[.,]\d+)?)S)?)?$/i);if(!o?.groups)return;const{weeks:i,days:r,hours:c,minutes:a,seconds:u}=o.groups;if(!(i||r||c||a||u))return;const f=t=>t?Number(t.replace(",",".")):0;return 7*f(i)*24*60*60*1e3+24*f(r)*60*60*1e3+60*f(c)*60*1e3+60*f(a)*1e3+1e3*f(u)}function A(t){if(void 0!==t)return"number"==typeof t?t:k(t)}function j(t,e,n){if("function"==typeof t)return t(n);const s=function(t,e){if("string"!=typeof t)return t;const n=e[t];return void 0!==n?n:k(t)??t}(t,e);return"function"==typeof s?s(n):s}function I(t){return"atomic"===t.type||"final"===t.type||"choice"===t.type}function T(t){return Object.values(t.states).filter(t=>"history"!==t.type)}function O(t,e){const n=[];if(e===t)return n;let s=t.parent;for(;s&&s!==e;)n.push(s),s=s.parent;return n}function M(t){const e=new Set(t),n=new Set;for(const t of e)t.parent&&n.add(t.parent);for(const t of e)if("compound"!==t.type||n.has(t)){if("parallel"===t.type)for(const n of T(t))if(!e.has(n)){const t=V(n);for(const n of t)e.add(n)}}else for(const n of V(t))e.add(n);for(const t of e){let n=t.parent;for(;n&&!e.has(n);)e.add(n),n=n.parent}return e}function N(t,e){const n=M(e),s=new Map;for(const t of n)s.has(t)||s.set(t,[]),t.parent&&(s.has(t.parent)||s.set(t.parent,[]),s.get(t.parent).push(t));const o=t=>{const e=s.get(t);if(!e)return{};if("compound"===t.type){const t=e[0];if(!t)return{};if(I(t))return t.key}const n={};for(const t of e)n[t.key]=o(t);return n};return o(t)}function D(t,e){return"compound"===e.type?T(e).some(e=>"final"===e.type&&t.has(e)):"parallel"===e.type?T(e).every(e=>D(t,e)):"final"===e.type}const P=t=>"#"===t[0];function C(t,e,n){const s=e.type,o=t.entry;t.entry=(t,i)=>(i.raise(e,{id:s,delay:n(t)}),"function"==typeof o?o(t,i):void 0);const i=t.exit;return t.exit=(t,e)=>(e.cancel(s),"function"==typeof i?i(t,e):void 0),s}function R(t,e,n){const s=Dt(n.target),o=n.reenter??!1,i=L(t,s),r={...n,target:i,source:t,reenter:o,eventType:e,toJSON:()=>({...r,source:`#${t.id}`,target:i?i.map(t=>`#${t.id}`):void 0})};return r}function L(t,e){if(void 0!==e)return e.map(e=>{if("string"!=typeof e)return e;if(P(e))return t.machine.getStateNodeById(e);const n="."===e[0];if(n&&!t.parent)return z(t,e.slice(1));const s=n?t.key+e:e;if(!t.parent)throw new Error(`Invalid target: "${e}"`);try{return z(t.parent,s)}catch(e){throw new Error(`Invalid transition definition for state node '${t.id}':\n${e.message}`)}})}function K(t){const e=Dt(t.config.target);return e?{target:e.map(e=>"string"==typeof e?z(t.parent,e):e),source:t,reenter:!1,eventType:""}:t.parent.initial}function W(t){return"history"===t.type}function V(t){const e=new Set;!function t(n){if(!e.has(n))if(e.add(n),"compound"===n.type)t(n.initial.target[0]);else if("parallel"===n.type)for(const e of T(n))t(e)}(t);for(const n of e)for(const s of O(n,t))e.add(s);return e}function J(t,e){if(P(e))return t.machine.getStateNodeById(e);if(!t.states)throw new Error(`Unable to retrieve child state '${e}' from '${t.id}'; no child states exist.`);const n=t.states[e];if(!n)throw new Error(`Child state '${e}' does not exist on '${t.id}'`);return n}function z(t,e){if("string"==typeof e&&P(e))try{return t.machine.getStateNodeById(e)}catch{}const n=Et(e).slice();let s=t;for(;n.length;){const t=n.shift();if(!t.length)break;s=J(s,t)}return s}function B(t,e){if("string"==typeof e){const n=t.states[e];if(!n)throw new Error(`State '${e}' does not exist on '${t.id}'`);return[t,n]}const n=Object.keys(e),s=new Array(n.length),o=[t.machine.root,t];for(let e=0;e<n.length;e++){const i=J(t,n[e]);s[e]=i,o.push(i)}for(let t=0;t<n.length;t++)o.push(...B(s[t],e[n[t]]));return o}function F(t,e,n,s,o){if("string"==typeof e){const i=J(t,e).next(n,s,o);return i&&i.length?i:t.next(n,s,o)}const i=Object.keys(e),r=i[0];if(1===i.length){const i=F(J(t,r),e[r],n,s,o);return i&&i.length?i:t.next(n,s,o)}const c=[];for(const r of i){const i=e[r];if(!i)continue;const a=F(J(t,r),i,n,s,o);a&&c.push(...a)}return c.length?c:t.next(n,s,o)}function q(t,e){let n=t;for(;n.parent&&n.parent!==e;)n=n.parent;return n.parent===e}function U(t,e){for(const n of t)if(q(n,e))return!0;return!1}function G(t,e){const n=t instanceof Set?t.size:Array.isArray(t)?t.length:void 0,s=e instanceof Set?e.size:Array.isArray(e)?e.length:void 0;void 0!==n&&void 0!==s&&s<n&&([t,e]=[e,t]);const o=t instanceof Set?t:new Set(t);for(const t of e)if(o.has(t))return!0;return!1}function X(t,e,n,s,o){const i=new Set,r=new Map,c=t=>{let i=r.get(t);return i||(i=Q([t],e,n,s,o),r.set(t,i)),i};for(const e of t){let t=!1;const n=new Set;for(const s of i)if(G(c(e),c(s))){if(!q(e.source,s.source)){t=!0;break}n.add(s)}if(!t){for(const t of n)i.delete(t);i.add(e)}}return Array.from(i)}function Z(t,e,n,s){const o=e.historyValue,{targets:i}=nt(t,e,n,s,{resolveActions:!1});if(!i)return[];const r=new Set;for(const t of i)if(W(t))if(o[t.id])for(const e of o[t.id])r.add(e);else for(const o of Z(K(t),e,n,s))r.add(o);else r.add(t);return[...r]}function H(t,e,n,s){const o=Z(t,e,n,s),{reenter:i}=nt(t,e,n,s,{resolveActions:!1});if(!i&&o.every(e=>e===t.source||q(e,t.source)))return t.source;const[r,...c]=o.concat(t.source);for(const t of O(r,void 0))if(c.every(e=>q(e,t)))return t;return i?void 0:t.source.machine.root}function Q(t,e,n,s,o){const i=new Set;for(const r of t){const{targets:t}=nt(r,n,s,o,{resolveActions:!1});if(t?.length){const t=H(r,n,s,o);r.reenter&&r.source===t&&i.add(t);for(const n of e)q(n,t)&&i.add(n)}}return[...i]}function Y(t,e,n,s,o){return et([{target:[...V(t)],source:t,reenter:!0,eventType:null,toJSON:null}],e,n,s,!0,o)}function tt(t,e){return{...t,...e}}function et(t,e,n,s,o,i){const c=[];if(!t.length)return[e,c];{const a=new Set(e._nodes);let u=e.historyValue;const h=e.context,d=X(t,a,e,s,n),l=t=>nt(t,e,s,n),p=(t,o,i,r)=>{if(2===t.length){const c=[],a=[];let u;const f=b(n,c,a,!0),h=t({context:o,event:s,parent:n.self._parent,self:n.self,children:i,system:n.system,actions:e.machine.implementations.actions,actorSources:e.machine.implementations.actorSources,guards:e.machine.implementations.guards,delays:e.machine.implementations.delays,input:r},f);return void 0!==h?.context&&(u=tt(o,h.context)),[c,u,a]}return[[Object.assign((e,n)=>t({...e,input:r},n),"_special"in t?{_special:!0}:{})],void 0,void 0]};let y=e;const g=()=>{const t=Q(d,a,e,s,n);let o;t.sort((t,e)=>e.order-t.order);const r=[...a];for(const e of t)for(const t of Object.values(e.states)){if("history"!==t.type)continue;const n="deep"===t.history?t=>I(t)&&q(t,e):t=>t.parent===e;o??={...u},o[t.id]=r.filter(n)}for(const o of t){const t=e._stateInputs?.[o.id],[r,u,f]=o.exit?p(o.exit,y.context,e.children,t):[[]];f?.length&&i.push(...f),u&&(y=bt(y,{context:u}));const[h,d]=S(y,s,n,r);y=h,c.push(...d);for(const t of o.invoke){const e=y.children[t.id];e&&!e._isExternal&&n.stopChild(e),delete y.children[t.id]}a.delete(o)}u=o||u};o||g();let v=y.context;const m=[],x=[];for(const t of d){t.actions&&m.push(...It(t.actions));const e=l(t);void 0!==e.context&&(v=tt(v,e.context)),e.actions&&m.push(...e.actions),e.internalEvents&&x.push(...e.internalEvents)}x.length&&i.push(...x);const _=()=>{const t=t=>{const e=y.machine.root;if(void 0===e.output)return;let o;if(void 0!==t.output&&t.parent)o=Tt(t.output,y.context,s,n.self);else if("parallel"===t.type){const e=`xstate.done.state.${t.id}`,n=i.find(t=>t.type===e);o=n?.output}return Tt(e.output,y.context,r(t.id,o),n.self)},h=new Set,g=new Set,v=(t,e)=>{for(const n of t)if(e&&!q(n,e)||h.add(n),"parallel"===n.type)for(const t of T(n))U(h,t)||(h.add(t),m(t))},m=t=>{if(W(t))if(u[t.id]){const e=u[t.id];for(const t of e)h.add(t),m(t);for(const n of e)v(O(n,t.parent),void 0)}else{const e=K(t),{targets:n}=l(e);for(const s of n??[])h.add(s),e===t.parent?.initial&&g.add(t.parent),m(s);for(const e of n??[])v(O(e,t.parent),void 0)}else{if("compound"===t.type){const[e]=l(t.initial).targets;return W(e)||(h.add(e),g.add(e)),m(e),void v(O(e,t),void 0)}if("parallel"===t.type)for(const e of T(t))U(h,e)||(h.add(e),g.add(e),m(e))}};for(const t of d){const o=H(t,e,s,n),{targets:i,reenter:r}=nt(t,e,s,n,{resolveActions:!1});for(const e of i??[])W(e)||t.source===e&&t.source===o&&!r||(h.add(e),g.add(e)),m(e);const c=Z(t,e,s,n);for(const e of c){const n=O(e,o);"parallel"===o?.type&&n.push(o),v(n,!t.source.parent&&r?void 0:o)}}o&&g.add(e.machine.root);const x={...e._stateInputs};for(const t of d){const{targets:o,input:i}=nt(t,e,s,n,{resolveActions:!1});if(i&&o)for(const t of o)x[t.id]=i}const _=new Set,b={...e.children};for(const o of[...h].sort((t,e)=>t.order-e.order)){a.add(o);const u=[];let h=!1;for(const t of o.invoke){h=!0;let o=t.logic;"function"==typeof o&&(o=o({actorSources:e.machine.implementations.actorSources,context:y.context,event:s,self:n.self}));const i="string"==typeof o?Ct(e.machine,o):o;if(!i)throw new Error(`Actor logic '${"string"==typeof o?o:"inline"}' not implemented in machine '${e.machine.id}'`);const r="function"==typeof t.input?t.input({self:n.self,context:y.context,event:s,output:Ot(s)}):t.input,c=te(i,{...t,input:r,parent:n.self,syncSnapshot:!!t.onSnapshot});u.push({action:f["@xstate.start"],args:[c]}),t.id&&(b[t.id]=c)}h&&(y=bt(y,{children:b}));let d=y.context,l=!1;const v=x[o.id];if(o.entry){const[t,e,n]=p(o.entry,d,b,v);u.push(...t),n?.length&&i.push(...n),e&&(d=e,l=!0)}if(l&&(y.context=d),g.has(o)){const{actions:t,input:e}=nt(o.initial,y,s,n);if(t&&u.push(...t),e&&o.initial?.target)for(const t of o.initial.target)x[t.id]=e}const[m,w]=S(y,s,n,u);if(y=m,u.length=0,c.push(...w),"final"!==o.type)continue;const $=o.parent;let E="parallel"===$?.type?$:$?.parent,k=E||o;for("compound"===$?.type&&i.push(r($.id,void 0!==o.output?Tt(o.output,y.context,s,n.self):void 0));"parallel"===E?.type&&!_.has(E)&&D(a,E);){_.add(E);const t={};for(const e of T(E)){if("final"===e.type){t[e.key]=void 0!==e.output?Tt(e.output,y.context,s,n.self):void 0;continue}if("parallel"===e.type){const n=`xstate.done.state.${e.id}`,s=i.find(t=>t.type===n);t[e.key]=s?.output;continue}const o=T(e).find(t=>"final"===t.type&&a.has(t));t[e.key]=void 0!==o?.output?Tt(o.output,y.context,s,n.self):void 0}i.push(r(E.id,t)),k=E,E=E.parent}E||(y=bt(y,{status:"done",output:t(k)}))}JSON.stringify(x)!==JSON.stringify(e._stateInputs||{})&&(y=bt(y,{_stateInputs:x}))},[w,$]=S(y,s,n,m);y=w,c.push(...$),v&&v!==e.context&&(y=bt(y,{context:v})),_();const E=[...a];if("done"===y.status){const t=[];E.sort((t,e)=>e.order-t.order).forEach(e=>{if(e.exit){const n=y._stateInputs?.[e.id],[s,,o]=p(e.exit,y.context,y.children,n);t.push(...s),o?.length&&i.push(...o)}});const[e,o]=S(y,s,n,t);y=e,c.push(...o)}return u===e.historyValue&&e._nodes.length===a.size&&e._nodes.every(t=>a.has(t))?y.context!==h?[bt(y),c]:[y,c]:[bt(y,{_nodes:E,historyValue:u}),c]}}function nt(t,e,n,s,o){const i={context:e.context,event:n,output:Ot(n),value:e.value,children:e.children,system:s.system,parent:s.self._parent,self:s.self,actions:e.machine.implementations.actions,actorSources:e.machine.implementations.actorSources,guards:e.machine.implementations.guards,delays:e.machine.implementations.delays};if(t.to){const r=[],c=[],a=b(s,r,c,!1,o?.resolveActions??!0),u=t.to(i,a),f=u?.target?L(t.source,It(u.target)):void 0,h=u?.input??("function"==typeof t.input?t.input({context:e.context,event:n,output:Ot(n)}):t.input);return{targets:f,context:u?.context,reenter:u?.reenter,actions:r,internalEvents:c,input:h}}const r="function"==typeof t.input?t.input({context:e.context,event:n,output:Ot(n)}):t.input,c="function"==typeof t.context?t.context(i):t.context;return{targets:t.target,context:c,reenter:t.reenter,actions:void 0,internalEvents:void 0,input:r}}function st(t,e,o,i){let r=t;const c=[];function a(t,e){const n=o.self._collectedMicrosteps||[];n.push(...e),o.self._collectedMicrosteps=n,c.push(t)}if(e.type===s)return r=bt(rt(r,o),{status:"stopped"}),a([r,[]],[]),{snapshot:r,microsteps:c};let u=e;if(u.type!==n){const e=u,n=function(t){return t.type.startsWith("xstate.error.actor")}(e),s=r.machine.getTransitionData(r,e,o.self);if(n&&!s.length)return r=bt(t,{status:"error",error:e.error}),a([r,[]],[]),{snapshot:r,microsteps:c};const f=et(s,t,o,u,!1,i);r=f[0],a(f,s)}let f=!0;const h=t.machine.options?.maxIterations??1/0;let d=0,l=0;for(;"active"===r.status;){if(l++,l>1e3)throw new Error("Microstep count exceeded 1000");if(d++,d>h)throw new Error(`Infinite loop detected (>${h} microsteps)`);let t=f?ct(r,u,o):[];const e=t.length?r:void 0;if(!t.length){if(!i.length)break;u=i.shift(),t=r.machine.getTransitionData(r,u,o.self)}const n=et(t,r,o,u,!1,i);r=n[0],f=r!==e,a(n,t)}return"active"!==r.status&&r.children&&rt(r,o),{snapshot:r,microsteps:c}}function ot(t,e,n,s,o){return!!t.to&&it(t.to,e,n,s,o,s.machine.implementations)}function it(t,e,n,s,o,i){let r,c=!1;try{const a=()=>{throw c=!0,new Error("Effect triggered")};r=t({context:e,event:n,output:Ot(n),self:o,system:o.system,value:s.value,children:s.children,parent:{send:a},actions:i.actions,actorSources:i.actorSources,guards:i.guards,delays:i.delays},function(t,e){const n=(t,...n)=>{e(t,...n)};return Object.assign(n,{cancel:()=>{},emit:()=>{},log:()=>{},raise:()=>{},spawn:()=>({}),sendTo:()=>{},stop:()=>{},listen:()=>({}),subscribeTo:()=>({}),...t}),n}({emit:a,cancel:a,log:a,raise:a,spawn:a,sendTo:a,stop:a},a))}catch(t){if(c)return!0;throw t}return void 0!==r}function rt(t,e){let n;if(!t.children||0===(n=Object.values(t.children).filter(Boolean)).length)return t;for(const t of n)e.stopChild(t);return bt(t,{children:{}})}function ct(t,e,n){const s=new Set,o=t._nodes.filter(I);for(const i of o)t:for(let o=i;o;o=o.parent)if(o.always)for(const i of o.always)if(at(i,e,t,o,n.self)){s.add(i);break t}return X(Array.from(s),new Set(t._nodes),t,e,n)}function at(t,e,n,s,o){if(t.guard){const i={context:n.context,event:e,output:Ot(e),self:o,parent:o._parent,children:n.children,actions:s.machine.implementations.actions,actorSources:s.machine.implementations.actorSources,guards:s.machine.implementations.guards,delays:s.machine.implementations.delays,_snapshot:n};if(!t.guard(i))return!1}return!t.to||it(t.to,n.context,e,n,o,s.machine.implementations)}function ut(t){return!!t&&"object"==typeof t&&"machine"in t&&"value"in t}let ft,ht;function dt(){return ft??=ue()}const lt=function(t){return $t(t,this.value)},pt=function(t){return this.tags.has(t)},yt=function(t){const e=dt(),n=function(){if(ht)return ht;const t=dt();return ht={self:t,logger:()=>{},id:"",sessionId:"",defer:()=>{},system:t.system,stopChild:()=>{},emit:()=>{},actionExecutor:()=>{}},ht}(),s=this.machine.getTransitionData(this,t,e);if(!s?.length)return!1;for(const o of s){if(void 0!==o.target)return!0;const s=nt(o,this,t,n,{resolveActions:!1});if(s.targets?.length||s.context||ot(o,this.context,t,this,e))return!0}return!1},gt=function(){const{_nodes:t,_stateInputs:e,tags:n,machine:s,getMeta:o,getInputs:i,toJSON:r,can:c,hasTag:a,matches:u,...f}=this;return{...f,tags:Array.from(n)}},vt=function(){const t={};for(const e of this._nodes)void 0!==e.meta&&(t[e.id]=e.meta);return t},mt=function(){return this._stateInputs};function xt(t){const e=new Set;for(const n of t)for(const t of n.tags)e.add(t);return e}function _t(t,e){return{status:t.status,output:t.output,error:t.error,machine:e,context:t.context,_nodes:t._nodes,value:t.value??N(e.root,t._nodes),tags:xt(t._nodes),children:t.children,historyValue:t.historyValue||{},_stateInputs:t._stateInputs||{},matches:lt,hasTag:pt,can:yt,getMeta:vt,getInputs:mt,toJSON:gt}}function bt(t,e={}){const n={...t,...e};return(e._nodes??t._nodes)===t._nodes?{status:n.status,output:n.output,error:n.error,machine:t.machine,context:n.context,_nodes:t._nodes,value:t.value,tags:t.tags,children:n.children,historyValue:n.historyValue||{},_stateInputs:n._stateInputs||{},matches:lt,hasTag:pt,can:yt,getMeta:vt,getInputs:mt,toJSON:gt}:_t({...n,value:void 0},t.machine)}function wt(t){const e={};for(const n in t){const s=t[n];Array.isArray(s)&&(e[n]=s.map(t=>({id:t.id})))}return e}function St(t){let e;for(const n in t){const s=t[n];if(s&&"object"==typeof s)if("sessionId"in s&&"send"in s&&"ref"in s)e??=Array.isArray(t)?t.slice():{...t},e[n]={xstate$$type:Ut,id:s.id};else{const o=St(s);o!==s&&(e??=Array.isArray(t)?t.slice():{...t},e[n]=o)}}return e??t}function $t(t,e){const n=kt(t),s=kt(e);return"string"==typeof s?"string"==typeof n&&s===n:"string"==typeof n?n in s:Object.keys(n).every(t=>t in s&&$t(n[t],s[t]))}function Et(t){if(Mt(t))return t;const e=[];let n="";for(let s=0;s<t.length;s++){switch(t.charCodeAt(s)){case 92:n+=t[s+1],s++;continue;case 46:e.push(n),n="";continue}n+=t[s]}return e.push(n),e}function kt(t){if(ut(t))return t.value;if("string"!=typeof t)return t;return At(Et(t))}function At(t){if(1===t.length)return t[0];const e={};let n=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)n[t[e]]=t[e+1];else{const s=n;n={},s[t[e]]=n}return e}function jt(t){return Mt(t)?t:[t]}function It(t){return void 0===t?[]:jt(t)}function Tt(t,e,n,s){if("function"==typeof t){return t({context:e,event:n,output:Ot(n),self:s})}return t}function Ot(t){if(function(t){return t.type.startsWith("xstate.done.actor.")||t.type.startsWith("xstate.done.state.")}(t)){return t.output}}function Mt(t){return Array.isArray(t)}function Nt(t){return jt(t).map(t=>void 0===t||"string"==typeof t?{target:t}:"function"==typeof t?{to:t}:t)}function Dt(t){if(void 0!==t&&""!==t)return It(t)}function Pt(t,e,n){const s="object"==typeof t,o=s?t:void 0;return{next:(s?t.next:t)?.bind(o),error:(s?t.error:e)?.bind(o),complete:(s?t.complete:n)?.bind(o)}}function Ct(t,e){const n=e.match(/^xstate\.invoke\.(\d+)\.(.*)/);if(!n)return t.implementations.actorSources[e];const[,s,o]=n,i=t.getStateNodeById(o).config.invoke,r=(Array.isArray(i)?i[s]:i).src;return"string"==typeof r?t.implementations.actorSources[r]:r}function Rt(t,e){if(e===t)return!0;if("*"===e)return!0;if(!e.endsWith(".*"))return!1;const n=e.split("."),s=t.split(".");for(let t=0;t<n.length;t++){const e=n[t],o=s[t];if("*"===e){return t===n.length-1}if(e!==o)return!1}return!0}function Lt(t,e){return`${t.sessionId}.${e}`}const Kt=new WeakMap,Wt="xstate.logic.effect.resolve",Vt="xstate.logic.effect.reject",Jt="xstate.logic.effect.start";function zt(t){let e=Kt.get(t);return e||(e=new Map,Kt.set(t,e)),e}function Bt(t,e){if(t?.length)for(const n of t)switch(n.type){case"emit":e.emit(n.event);break;case"sendBack":{const t=e.self._parent;t&&e.system._relay(e.self,t,n.event);break}case"raise":e.system._relay(e.self,e.self,n.event);break;case"effect":{if(!n.key){const t=n.exec();"function"==typeof t&&zt(e.self).set(Symbol(),{cleanup:t});break}const t=zt(e.self);if(t.has(n.key))break;const s=n.exec();t.set(n.key,{cleanup:"function"==typeof s?s:void 0});break}case"cleanupEffects":{const t=Kt.get(e.self);if(!t)break;for(const{cleanup:e}of t.values())e?.();Kt.delete(e.self);break}}}function Ft(t){const e=(e,n,o)=>{if("active"!==e.status)return[e,[]];if(n.type===s)return[{...e,status:"stopped",input:void 0},[{type:"cleanupEffects"}]];if(n.type===Jt)return[{...e,effects:{...e.effects,[n.key]:{status:"active"}}},[]];if(n.type===Wt)return[{...e,effects:{...e.effects,[n.key]:{status:"done",output:n.output}}},[]];if(n.type===Vt)return[{...e,effects:{...e.effects,[n.key]:{status:"error",error:n.error}}},[]];const i=[],r={},c={emit:t=>{i.push({type:"emit",event:t})},sendBack:t=>{i.push({type:"sendBack",event:t})},raise:t=>{i.push({type:"raise",event:t})},effect:(t,n)=>{if("string"==typeof t)return s=t,o=n,void(e.effects?.[s]||(i.push({type:"effect",key:s,exec:o}),r[s]={status:"active"}));var s,o;i.push({type:"effect",exec:t})}},a=t.run({context:e.context,event:n,input:e.input,system:o.system,self:o.self,emit:o.emit},c),u={...e,...a||{}};return("context"in e||void 0!==a?.context)&&(u.context=a?.context??e.context),(a?.effects||Object.keys(r).length)&&(u.effects={...e.effects,...a?.effects,...r}),[u,i]},n={id:t.id,config:t,transition:e,start:(t,n)=>{const[s,o]=e(t,c(t.input),n);Object.assign(t,s),Bt(o,n)},initialTransition:(e,n)=>{const s=function(t,e){return"function"==typeof t?t({input:e}):t}(t.context,e),o={status:"active",output:void 0,error:void 0,input:e};return void 0!==s&&(o.context=s),[{...o},[]]},getInitialSnapshot:(t,e)=>n.initialTransition(e,t)[0],getPersistedSnapshot:t=>t,restoreSnapshot:t=>t};return n}let qt=!1;const Ut=1;let Gt=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const Xt={clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console)};function Zt(t){return"object"==typeof t&&null!==t&&"args"in t&&"exec"in t}function Ht(t,e){if(!t?.length)return[];const n=[];for(const s of t)Zt(s)?e.actionExecutor(s):n.push(s);return n}class Qt{constructor(t,n){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this._boundProcess=this._process.bind(this),this.mailbox=new e(this._boundProcess),this.observers=new Set,this.eventListeners=new Map,this.logger=void 0,this._processingStatus=Gt.NotStarted,this._forceDeferredActions=!1,this._parent=void 0,this._syncSnapshot=void 0,this.ref=void 0,this._actorScope=void 0,this._lastSourceRef=void 0,this._collectedMicrosteps=[],this._collectedActions=[],this._collectedSent=[],this._initialEffects=void 0,this.registryKey=void 0,this.sessionId=void 0,this.system=void 0,this.trigger=void 0,this.src=void 0,this._deferred=[],this._pendingEffects=void 0;const s={...Xt,...n},{clock:o,logger:i,parent:r,syncSnapshot:c,id:u,registryKey:f,inspect:h}=s;this.system=r?r.system:s._systemRef?.current??function(t,e){let n=0;const s=new Map,o=new Map,i=new WeakMap,r=new Set,c={},{clock:a,logger:u}=e,f=(t,e,n,s,o)=>{r.size&&t&&(t._collectedSent??=[]).push({targetRef:e,targetId:e.id,event:n,delay:s,id:o})},h={schedule:(t,e,n,s,o=Math.random().toString(36).slice(2))=>{f(t,e,n,s,o);const i={source:t,target:e,event:n,delay:s,id:o,startedAt:Date.now()},r=Lt(t,o);l._snapshot._scheduledEvents[r]=i;const u=a.setTimeout(()=>{delete c[r],delete l._snapshot._scheduledEvents[r],d(t,e,n)},s);c[r]=u},cancel:(t,e)=>{const n=Lt(t,e),s=c[n];delete c[n],delete l._snapshot._scheduledEvents[n],void 0!==s&&a.clearTimeout(s)},cancelAll:t=>{for(const e in l._snapshot._scheduledEvents){const n=l._snapshot._scheduledEvents[e];n.source===t&&h.cancel(t,n.id)}}},d=(t,e,n)=>{const s=e.logic;if("function"==typeof s?.isInternalEventType&&s.isInternalEventType(n.type)&&t!==e)throw new Error(`Internal event "${n.type}" cannot be sent to actor "${e.id}" from outside.`);e._lastSourceRef=t,e._send(n)},l={children:s,reverseKeyedActors:i,keyedActors:o,_snapshot:{_scheduledEvents:(e?.snapshot&&e.snapshot.scheduler)??{}},_bookId:()=>"x:"+n++,_register:(t,e)=>(s.set(t,e),t),_unregister:t=>{s.delete(t.sessionId);const e=i.get(t);void 0!==e&&(o.delete(e),i.delete(t))},get:t=>o.get(t),getAll:()=>Object.fromEntries(o.entries()),_set:(t,e)=>{const n=o.get(t);if(n&&n!==e)throw new Error(`Actor with registry key '${t}' already exists.`);o.set(t,e),i.set(e,t)},inspect:t=>{const e=Pt(t);return r.add(e),{unsubscribe(){r.delete(e)}}},_sendInspectionEvent:e=>{if(!r.size)return;const n={...e,rootId:t.sessionId};r.forEach(t=>t.next?.(n))},_relay:(t,e,n)=>{f(t,e,n),d(t,e,n)},scheduler:h,getSnapshot:()=>({_scheduledEvents:{...l._snapshot._scheduledEvents}}),start:()=>{const t=l._snapshot._scheduledEvents;l._snapshot._scheduledEvents={};for(const e in t){const{source:n,target:s,event:o,delay:i,id:r}=t[e];h.schedule(n,s,o,i,r)}},_clock:a,_logger:u,_timerStrategy:e.timers};return l}(this,{clock:o,logger:i,timers:s.timers}),r||!s._systemRef||s._systemRef.current||(s._systemRef.current=this.system),h&&!r&&this.system.inspect(Pt(h)),this.sessionId=this.system._bookId(),this.id=u??this.sessionId,this.logger=n?.logger??this.system._logger,this.clock=n?.clock??this.system._clock,this._parent=r,this._syncSnapshot=c,this.options=s,this.src=s.src??t,this.ref=this,this._actorScope={self:this,id:this.id,sessionId:this.sessionId,logger:this.logger,defer:t=>{this._deferred.push(t)},system:this.system,stopChild:t=>{if(t._parent!==this)throw new Error(`Cannot stop child actor ${t.id} of ${this.id} because it is not a child`);t._stop()},emit:t=>{const e=this.eventListeners.get(t.type),n=this.eventListeners.get("*");if(e||n){if(e)for(const n of e)try{n(t)}catch(t){a(t)}if(n)for(const e of n)try{e(t)}catch(t){a(t)}}},actionExecutor:t=>{const e=()=>{if(this._collectedActions.push({type:t.type,params:t.params}),!t.exec)return;const e=qt;try{qt=!0,t.exec()}finally{qt=e}};this._processingStatus!==Gt.Running||this._forceDeferredActions?this._deferred.push(e):e()}},this.send=this.send.bind(this),this.trigger=new Proxy({},{get:(t,e)=>t=>{this.send({...t,type:e})}});const d=f;d&&(this.registryKey=d,this.system._set(d,this)),this._collectedMicrosteps=[];let l=n?.snapshot??n?.state;if(l&&"object"==typeof l&&"_pendingEffects"in l){const{_pendingEffects:t,...e}=l;this._pendingEffects=t,l=e}try{if(l)this._snapshot=this.logic.restoreSnapshot?this.logic.restoreSnapshot(l,this._actorScope):l;else{const[t,e]=this.logic.initialTransition(this.options?.input,this._actorScope);this._snapshot=t,this._initialEffects=e}}catch(t){this._snapshot={status:"error",output:void 0,error:t},this._deferred.length=0}d&&"active"!==this._snapshot.status&&this.system._unregister(this),this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this,parentRef:this._parent,id:this.id,src:this.src,snapshot:this._snapshot})}_setErrorSnapshot(t,e=this._snapshot){this._snapshot={...e,status:"error",error:t}}_next(t){for(const e of this.observers)try{e.next?.(t)}catch(t){a(t)}}update(t,e){this._snapshot=t;for(let e=0;e<this._deferred.length;e++){const n=this._deferred[e];try{n()}catch(e){this._deferred.length=0,this._setErrorSnapshot(e,t);break}}switch(this._deferred.length=0,this._snapshot.status){case"active":this._next(t);break;case"done":{this._next(t),this._stopProcedure(),this._complete();const e=(n=this.id,s=this._snapshot.output,{type:`xstate.done.actor.${n}`,output:s,actorId:n});this._parent&&this.system._relay(this,this._parent,e);break}case"error":this._error(this._snapshot.error)}var n,s;this.system._sendInspectionEvent({type:"@xstate.transition",actorRef:this,event:e,sourceRef:this._lastSourceRef,targetRef:this,snapshot:t,microsteps:this._collectedMicrosteps,actions:this._collectedActions,sent:this._collectedSent,eventType:e.type}),this._collectedMicrosteps=[],this._collectedActions=[],this._collectedSent=[]}_flushInitialEffects(){if(!this._initialEffects)return!0;this._forceDeferredActions=!0;try{return Bt(Ht(this._initialEffects,this._actorScope),this._actorScope),this._initialEffects=void 0,!0}catch(t){return this._initialEffects=void 0,this._deferred.length=0,this._setErrorSnapshot(t),this._error(t),!1}finally{this._forceDeferredActions=!1}}subscribe(t,e,n){const s=Pt(t,e,n);if(this._processingStatus!==Gt.Stopped)this.observers.add(s);else switch(this._snapshot.status){case"done":try{s.complete?.()}catch(t){a(t)}break;case"error":{const t=this._snapshot.error;if(s.error)try{s.error(t)}catch(t){a(t)}else a(t);break}}return{unsubscribe:()=>{this.observers.delete(s)}}}on(t,e){let n=this.eventListeners.get(t);return n||(n=new Set,this.eventListeners.set(t,n)),n.add(e),{unsubscribe:()=>{n.delete(e)}}}select(t,e=Object.is){return{subscribe:(n,s,o)=>{const i=Pt(n,s,o);let r=t(this.getSnapshot());return this.subscribe({next:n=>{const s=t(n);e(r,s)||(r=s,i.next?.(s))},error:i.error,complete:i.complete})},get:()=>t(this.getSnapshot())}}start(){if(this._processingStatus===Gt.Running)return this;this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:`xstate.snapshot.${this.id}`,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this.registryKey&&this.system._set(this.registryKey,this),this._processingStatus=Gt.Running;const t=c(this.options.input);this._lastSourceRef=this._parent;switch(this._snapshot.status){case"done":return this._flushInitialEffects()?(this.update(this._snapshot,t),this):this;case"error":return this._error(this._snapshot.error),this}if(this._parent||this.system.start(),this.logic.start)try{this.logic.start(this._snapshot,this._actorScope)}catch(t){return this._setErrorSnapshot(t),this._error(t),this}if(!this._flushInitialEffects())return this;if(this.update(this._snapshot,t),this._pendingEffects){const t=this.options.timers??this.system._timerStrategy??"resume";for(const e of this._pendingEffects)"@xstate.raise"===e.type&&this.system.scheduler.schedule(this,this,e.event,Yt(t,e),e.id);this._pendingEffects=void 0}return this.mailbox.start(),this}_process(t){let e,n;try{e=this.logic.transition(this._snapshot,t,this._actorScope)}catch(t){n={err:t}}if(n){const{err:t}=n;return this._setErrorSnapshot(t),void this._error(t)}try{const[n,s]=e,o=Ht(s,this._actorScope);this.update(n,t),Bt(o,this._actorScope)}catch(t){return this._setErrorSnapshot(t),void this._error(t)}t.type===s&&(this._stopProcedure(),this._complete())}_stop(){return this._processingStatus===Gt.Stopped?this:(this.mailbox.clear(),this._processingStatus===Gt.NotStarted?(this._processingStatus=Gt.Stopped,this):(this.mailbox.enqueue({type:s}),this.system._unregister(this),this))}stop(){if(this._parent)throw new Error("A non-root actor cannot be stopped directly.");return this._stop()}_complete(){for(const t of this.observers)try{t.complete?.()}catch(t){a(t)}this.observers.clear(),this.eventListeners.clear()}_error(t){if(this._stopProcedure(),this.observers.size){let e=!1;for(const n of this.observers){const s=n.error;e||=!s;try{s?.(t)}catch(t){a(t)}}this.observers.clear(),this.eventListeners.clear(),e&&a(t)}else this._parent||a(t),this.eventListeners.clear();var e;this._parent&&this.system._relay(this,this._parent,{type:`xstate.error.actor.${e=this.id}`,error:t,actorId:e})}_stopProcedure(){this._processingStatus===Gt.Running&&(this.system.scheduler.cancelAll(this),this.mailbox.clear(),this.mailbox=new e(this._boundProcess),this._processingStatus=Gt.Stopped,this.system._unregister(this))}_send(t){this._processingStatus!==Gt.Stopped&&this.mailbox.enqueue(t)}send(t){this.system._relay(void 0,this,t)}toJSON(){return{xstate$$type:Ut,id:this.id}}getPersistedSnapshot(t){const e=this.logic.getPersistedSnapshot(this._snapshot,t),n=this.system._snapshot._scheduledEvents;let s;const o=Date.now();for(const t in n){const e=n[t];e.source===this&&e.target===this&&(s??=[]).push({type:"@xstate.raise",event:e.event,id:e.id,delay:e.delay,startedAt:e.startedAt,elapsed:Math.max(0,o-e.startedAt)})}return s??=this._pendingEffects,s?{...e,_pendingEffects:s}:e}[u](){return this}getSnapshot(){return this._snapshot}}function Yt(t,e){if("function"==typeof t)return t(e);switch(t){case"restart":return e.delay;case"absolute":return Math.max(0,e.startedAt+e.delay-Date.now());default:return Math.max(0,e.delay-e.elapsed)}}function te(t,e){return new Qt(t,e)}const ee=new WeakMap;const ne="xstate.observable.next",se="xstate.observable.error",oe="xstate.observable.complete";const ie="xstate.async.resolve",re="xstate.async.reject";class ce extends Error{constructor(t){super(`Async logic timed out after ${t}.`),this.name="TimeoutError"}}const ae=Ft({context:void 0,run:()=>{}});function ue(){return te(ae)}function fe(t,{machine:e,context:n},s,o){return(i,r)=>{const c=((i,r)=>{if("string"==typeof i){const c=Ct(e,i);if(!c)throw new Error(`Actor logic '${i}' not implemented in machine '${e.id}'`);const a=te(c,{id:r?.id,parent:t.self,syncSnapshot:r?.syncSnapshot,input:"function"==typeof r?.input?r.input({context:n,event:s,self:t.self}):r?.input,src:i,registryKey:r?.registryKey});return o[a.id]=a,a}return te(i,{id:r?.id,parent:t.self,syncSnapshot:r?.syncSnapshot,input:r?.input,src:i,registryKey:r?.registryKey})})(i,r);return o[c.id]=c,t.defer(()=>{c._processingStatus!==Gt.Stopped&&c.start()}),c}}const he=new WeakMap;function de(t,e,n){let s=he.get(t);return s?e in s||(s[e]=n()):(s={[e]:n()},he.set(t,s)),s[e]}const le={},pe=["invoke","after","on","entry","exit","always","states","initial","onDone","timeout","onTimeout","history","target","output"];class ye{constructor(t,e){this.config=t,this.key=void 0,this.id=void 0,this.type=void 0,this.path=void 0,this.states=void 0,this.history=void 0,this.entry=void 0,this.exit=void 0,this.parent=void 0,this.machine=void 0,this.meta=void 0,this.output=void 0,this.order=-1,this.description=void 0,this.tags=[],this.transitions=void 0,this.always=void 0,this.invoke=void 0,this.on=void 0,this.after=void 0,this.events=void 0,this.ownEvents=void 0,this.parent=e._parent,this.key=e._key,this.machine=e._machine,this.path=this.parent?this.parent.path.concat(this.key):[];const n=this.config.states?Object.keys(this.config.states)[0]:void 0;if(this.id=this.config.id||[this.machine.id,...this.path].join("."),this.type=this.config.type||(void 0!==n?"compound":this.config.history?"history":"atomic"),this.description=this.config.description,function(t){const e=t.config;if("choice"!==t.type){if(void 0!==e.choice)throw new Error(`State "${t.id}" has \`choice\`, but \`choice\` can only be used with \`type: 'choice'\`.`);return}if("function"!=typeof e.choice)throw new Error(`Choice state "${t.id}" must declare a \`choice\` function.`);for(const n of pe)if(void 0!==e[n])throw new Error(`Choice state "${t.id}" cannot declare \`${n}\`.`)}(this),this.order=this.machine.idMap.size,this.machine.idMap.set(this.id,this),this.states=this.config.states?function(t,e){const n={},s=Object.keys(t);for(let o=0;o<s.length;o++){const i=s[o];n[i]=e(t[i],i,t,o)}return n}(this.config.states,(t,e)=>new ye(t,{_parent:this,_key:e,_machine:this.machine})):le,"compound"===this.type&&!this.config.initial)throw new Error(`No initial state specified for compound state node "#${this.id}".`);this.history=!0===this.config.history?"shallow":this.config.history||!1,this.entry=this.config.entry,this.exit=this.config.exit,this.entry&&(this.entry._special=!0),this.exit&&(this.exit._special=!0),this.meta=this.config.meta,this.output="final"!==this.type&&this.parent?void 0:this.config.output,this.tags=It(t.tags).slice(),this.invoke=It(this.config.invoke).map((t,e)=>{const{src:n,registryKey:s}=t,o=(i=this.id,`${e}.${i}`);var i;const r=t.id??o,c="string"==typeof n?n:`xstate.invoke.${o}`;return{...t,src:c,logic:n,id:r,registryKey:s}})}_initialize(){this.after=function(t){const e=t.config.after,n=t.config.timeout,s=t.config.onTimeout,r=t.invoke.filter(t=>void 0!==t.timeout);if(!e&&void 0===n&&0===r.length)return[];const c=[];if(e)for(const n of Object.keys(e)){const s=Number.isNaN(+n)?n:+n;c.push({event:o(s,t.id),delay:s,transitions:e[n],fromDelaysMap:!0})}var a;void 0!==n&&s&&c.push({event:(a=t.id,{type:`xstate.timeout.${a}`}),delay:n,transitions:s,fromDelaysMap:!0});for(const t of r)c.push({event:i(t.id),delay:t.timeout,transitions:t.onTimeout,fromDelaysMap:!1});const u=[];for(const{event:e,delay:n,transitions:s,fromDelaysMap:o}of c){const i=C(t,e,e=>j(n,o?e.delays:{},{context:e.context,event:e.event,stateNode:t}));for(const t of Nt(s))u.push({...t,event:i,delay:n})}return u.map(e=>({...R(t,e.event,e),delay:e.delay}))}(this),this.transitions=function(t){const e=new Map;if(t.config.on)for(const n of Object.keys(t.config.on)){if(""===n)throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.');const s=t.config.on[n];e.set(n,ge(s,e=>R(t,n,e)))}if(t.config.onDone){const n=`xstate.done.state.${t.id}`;e.set(n,ge(t.config.onDone,e=>R(t,n,e)))}const n=(e,n)=>R(t,e,{to:(t,e)=>(e.cancel(n),{})}),s=(e,n,s)=>{const{target:o,to:i,reenter:r,...c}=n;return R(t,e,{...c,reenter:r,to:(t,e)=>{if(i){let n=!1;const o=new Proxy(e,{apply:(t,e,s)=>(n=!0,Reflect.apply(t,e,s)),get(t,e,s){const o=Reflect.get(t,e,s);return"function"!=typeof o?o:(...e)=>(n=!0,o.apply(t,e))}}),r=i(t,o);return(void 0!==r||n)&&e.cancel(s),r}return e.cancel(s),{target:o,reenter:r}}})};for(const o of t.invoke){const r=void 0!==o.timeout?i(o.id).type:void 0;if(o.onDone){const i=`xstate.done.actor.${o.id}`,c=ge(o.onDone,e=>r?s(i,e,r):R(t,i,e));r&&c.push(n(i,r)),e.set(i,c)}else if(r){const t=`xstate.done.actor.${o.id}`;e.set(t,[n(t,r)])}if(o.onError){const n=`xstate.error.actor.${o.id}`;e.set(n,ge(o.onError,e=>r?s(n,e,r):R(t,n,e)))}if(o.onSnapshot){const n=`xstate.snapshot.${o.id}`;e.set(n,ge(o.onSnapshot,e=>R(t,n,e)))}}for(const n of t.after){let t=e.get(n.eventType);t||(t=[],e.set(n.eventType,t)),t.push(n)}return e}(this),"choice"===this.type?this.always=function(t){const e=t.config.choice,n=e=>{if(!e||void 0===e.target)throw new Error(`Choice state "${t.id}" must resolve to a target.`);for(const n of["actions","to"])if(void 0!==e[n])throw new Error(`Choice state "${t.id}" cannot declare \`${n}\` on a choice.`);return e};return[R(t,"",{to:t=>n(e(t))})]}(this):this.config.always&&(this.always=ge(this.config.always,t=>R(this,"",t)));for(const t of Object.keys(this.states))this.states[t]._initialize();this._refreshEventMetadata()}_refreshEventMetadata(){const t={},e=[];for(const[n,s]of this.transitions)t[n]=s.slice(),s.some(t=>t.target||t.reenter||t.to)&&e.push(n);this.on=t,this.ownEvents=e;const n=new Set(e);for(const t of Object.values(this.states))for(const e of t.events)n.add(e);this.events=Array.from(n)}get initial(){return de(this,"initial",()=>function(t,e){const n="object"==typeof e&&null!==e?e.target:e,s="object"==typeof e&&null!==e?e.input:void 0,o="string"==typeof n?t.states[n]:void 0;if(!o&&n)throw new Error(`Initial state node "${n}" not found on parent state node #${t.id}`);const i={source:t,target:o?[o]:void 0,input:s};return i}(this,this.config.initial))}next(t,e,n){const s=e.type,o=de(this,`candidates-${s}`,()=>function(t,e){const n=t.transitions.get(e),s=[...t.transitions.keys()].filter(t=>t!==e&&Rt(e,t)).sort((t,e)=>e.length-t.length).flatMap(e=>t.transitions.get(e));return n?[...n,...s]:s}(this,s));for(const s of o){if(at(s,e,t,this,n))return[s]}}}function ge(t,e){const n=Nt(t),s=new Array(n.length);for(let t=0;t<n.length;t++)s[t]=e(n[t]);return s}class ve{constructor(t,e){this.config=t,this.version=void 0,this.schemas=void 0,this.implementations=void 0,this.options=void 0,this.idMap=new Map,this.root=void 0,this.id=void 0,this.states=void 0,this.events=void 0,this.internalEventDescriptors=void 0,this._json=void 0,this.id=t.id||"(machine)",this.implementations={actorSources:t.actorSources??{},actions:t.actions??{},delays:t.delays??{},guards:t.guards??{},...e},this.version=this.config.version,this.schemas=this.config.schemas,this.internalEventDescriptors=this.config.internalEvents??[],this.options={maxIterations:1/0,...this.config.options},this.transition=this.transition.bind(this),this.initialTransition=this.initialTransition.bind(this),this.getInitialSnapshot=this.getInitialSnapshot.bind(this),this.getPersistedSnapshot=this.getPersistedSnapshot.bind(this),this.restoreSnapshot=this.restoreSnapshot.bind(this),this.start=this.start.bind(this),this.root=new ye(t,{_key:this.id,_machine:this}),this.root._initialize(),function(t){const e=[],n=s=>{Object.values(s).forEach(s=>{if(s.config.route&&s.config.id){const o=s.config.id,i=s.config.route,r=({event:t})=>t.to===`#${o}`;if("function"==typeof i)return e.push(R(t,"xstate.route",{guard:r,to:t=>{const e=i(t);if(e)return{...!0===e?{}:e,target:`#${o}`}}})),void(s.states&&n(s.states));const{guard:c,...a}=i,u={...a,guard:r,target:`#${o}`};e.push(R(t,"xstate.route",u))}s.states&&n(s.states)})};n(t.states),e.length>0&&t.transitions.set("xstate.route",e)}(this.root),this.root._refreshEventMetadata(),this.states=this.root.states,this.events=this.root.events}provide(t){const{actions:e,guards:n,actorSources:s,delays:o}=this.implementations,i=new ve(this.config,{actions:{...e,...t.actions},guards:{...n,...t.guards},actorSources:{...s,...t.actorSources},delays:{...o,...t.delays}});return i._json=this._json,i}resolveState(t){const e=(n=this.root,s=t.value,N(n,M(B(n,s))));var n,s;const o=M(B(this.root,e));return _t({_nodes:[...o],value:e,context:t.context||{},children:{},status:D(o,this.root)?"done":t.status||"active",output:t.output,error:t.error,historyValue:t.historyValue},this)}transition(t,e,n){const s=this._transitionFast(t,e,n);if(s)return[s,[]];const{snapshot:o,microsteps:i}=st(t,e,n,[]);return[o,i.flatMap(([,t])=>t)]}_transitionFast(t,e,n){if("active"!==t.status||"string"!=typeof t.value||this.root.always?.length)return;const s=this.root.states[t.value];if(!s||"atomic"!==s.type||s.exit||s.invoke.length||s.always?.length||s.after?.length)return;const o=s.transitions.get(e.type);if(1!==o?.length)return;const i=o[0];if(i.guard||i.actions||i.to||i.reenter||i.input||"function"==typeof i.context||i.target&&1!==i.target.length)return;const r=i.target?.[0]??s,c=r!==s;if(r.parent!==this.root||"atomic"!==r.type||c&&(r.entry||r.invoke.length||r.always?.length||r.after?.length))return;const a=void 0!==i.context?{...t.context,...i.context}:t.context,u=n.self._collectedMicrosteps||[];return u.push(i),n.self._collectedMicrosteps=u,bt(t,{...a!==t.context?{context:a}:{},...c?{_nodes:[this.root,r]}:{}})}microstep(t,e,n){const{microsteps:s}=st(t,e,n,[]),o=new Array(s.length);for(let t=0;t<s.length;t++)o[t]=s[t][0];return o}getTransitionData(t,e,n){return F(this.root,t.value,t,e,n)||[]}isInternalEventType(t){for(const e of this.internalEventDescriptors)if(Rt(t,e))return!0;return!1}_getPreInitialState(t,e){const{context:n}=this.config,s=_t({context:"function"!=typeof n&&n?n:{},_nodes:[this.root],children:{},status:"active"},this);if("function"==typeof n){const o={},i=n({spawn:fe(t,s,e,o),input:e.input,self:t.self,actorSources:this.implementations.actorSources}),[r]=S(s,e,t,[]);return i&&(r.context=i),Object.keys(o).length>0&&(r.children={...r.children,...o}),r}return s}getInitialSnapshot(t,e){return this.initialTransition(e,t)[0]}initialTransition(t,e){const n=c(t),s=[],o=this._getPreInitialState(e,n),[i,r]=Y(this.root,o,e,n,s),{snapshot:a,microsteps:u}=st(i,n,e,s);return[a,[...r,...u.flatMap(([,t])=>t)]]}start(t){if(t?.children)for(const e of Object.values(t.children))e._rehydrated&&"active"===e.getSnapshot?.().status&&e.start()}getStateNodeById(t){const e=Et(t),n=e.slice(1),s=P(e[0])?e[0].slice(1):e[0],o=this.idMap.get(s);if(!o)throw new Error(`Child state node '#${s}' does not exist on machine '${this.id}'`);return z(o,n)}getPersistedSnapshot(t,e){return function(t,e){const{_nodes:n,_stateInputs:s,tags:o,machine:i,children:r,context:c,can:a,hasTag:u,matches:f,getMeta:h,getInputs:d,toJSON:l,...p}=t,y={};for(const t in r){const n=r[t];y[t]={snapshot:n.getPersistedSnapshot(e),src:n.src,registryKey:n.registryKey,syncSnapshot:n._syncSnapshot}}const g={...p,context:St(c),children:y,historyValue:wt(p.historyValue)};return void 0!==i.version&&(g.version=i.version),g}(t,e)}restoreSnapshot(t,e){const n=t.version;if(n!==this.version){const e=this.config.migrate;if("function"!=typeof e)throw new Error(`Persisted snapshot version '${n}' does not match machine version '${this.version}'.`);t=e(t,n)}const s=t,o={},i=s.children;for(const t of Object.keys(i)){const n=i[t],s=n.snapshot,r=n.src,c="string"==typeof r?Ct(this,r):r;if(!c)continue;const a=te(c,{id:t,parent:e.self,syncSnapshot:n.syncSnapshot,snapshot:s,src:r,registryKey:n.registryKey});a._rehydrated=!0,o[t]=a}const r=(t=>{if(!t||"object"!=typeof t)return{};const e={};for(const n of Object.keys(t)){const s=t[n];for(const t of s){let s;if(t instanceof ye)s=t;else try{s=this.root.machine.getStateNodeById(t.id)}catch{}s&&(e[n]??=[],e[n].push(s))}}return e})(s.historyValue),c=Array.from(M(B(this.root,s.value))),{version:a,...u}=t,f=_t({...u,children:o,_nodes:c,value:s.value,historyValue:r},this),h=new WeakSet;return function t(e){if(!h.has(e)){h.add(e);for(const n of Object.keys(e)){const s=e[n];if(s&&"object"==typeof s){if("xstate$$type"in s&&s.xstate$$type===Ut){e[n]=o[s.id];continue}t(s)}}}}(f.context),f}}function me(t){return new ve(t)}const xe={actions:{},actorSources:{},guards:{},delays:{}},_e=[],be=[],we=new Set(Object.values(f));function Se(t){return void 0===t?[]:Array.isArray(t)?t:[t]}function $e(t,e){return"function"==typeof t?t(e):t}function Ee(t,e,n){return"function"==typeof t?t({context:e,event:n}):t}function ke(t,e){for(const n of Object.keys(e))if(!Object.prototype.hasOwnProperty.call(t,n)||!Object.is(t[n],e[n]))return{...t,...e};return t}function Ae(t,e,n,s){return{status:t.status,output:t.output,error:t.error,value:e,context:n,input:t.input,children:t.children,_stateInput:s,machine:t.machine}}function je(t){return{status:"stopped",output:void 0,error:void 0,value:t.value,context:t.context,input:void 0,children:t.children,_stateInput:t._stateInput,machine:t.machine}}function Ie(t){const e=A(t);return void 0!==e?e:"string"==typeof t?parseFloat(t)||0:t}function Te(t){return!!t&&"object"==typeof t&&"string"==typeof t["@expr"]}function Oe(t){return Te(t)||function(t){return!!t&&"object"==typeof t&&"string"==typeof t["@code"]}(t)}function Me(t,e){return"number"==typeof e?`${t}[${e}]`:`${t}.${e}`}function Ne(t){const e={};if(!t)return e;for(const n of Object.keys(t)){const s=t[n];"number"==typeof s?e[n]=s:s&&"object"==typeof s&&"number"==typeof s.duration&&(e[n]=s.duration)}return e}function De(t){return void 0===t?[]:Array.isArray(t)?t:[t]}function Pe(t,e){Array.isArray(t)&&t.forEach((n,s)=>{if(void 0===n.when&&s!==t.length-1)throw new Error(`Choice fallback branch at ${e}[${s}] must be last.`)})}let Ce="session_"+Math.random().toString(36).slice(2),Re="";function Le(t,e,n,s){const o=`\nwith (context) {\n return (${e});\n}\n `.trim(),i=new Function("context","_event","_sessionid","_name","_ioprocessors","In",o),r=n&&n._scxmlExternal,c=n&&"@xstate.init"===n.type,a=n&&"string"==typeof n.type&&n.type.startsWith("xstate.done."),u=n&&n._scxmlEventType;return i(t,n&&!c?{name:n.type,type:u||(r?"external":"internal"),sendid:n._scxmlSendId,origin:r?"#_scxml_session":void 0,origintype:r?"http://www.w3.org/TR/scxml/#SCXMLEventProcessor":void 0,invokeid:n._scxmlInvokeId,data:a?n.output:void 0!==n._scxmlEventData?n._scxmlEventData:n}:void 0,Ce,Re,{scxml:{location:`#_scxml_${Ce}`}},t=>{if(!s)return!1;try{const e=s.getSnapshot?.();if(e?._nodes){const n=t.replace(/\./g,"$");return e._nodes.some(t=>t.id===n||t.id.endsWith("."+n))}}catch{}return!1})}function Ke(t,e){const n=Object.keys(t),s=n.map(t=>`let ${t} = context.${t};`).join("\n"),o=n.map(t=>`updates.${t} = ${t};`).join("\n");return new Function("context","updates",`\n${s}\n${e}\n${o}\nreturn updates;\n `)(t,{})}function We(t){return!Oe(t)&&t.type.startsWith("@xstate.")}function Ve(t){if("function"==typeof t)return{"@code":t.toString(),"@lang":"ts"};if(null===t||"object"!=typeof t)return"bigint"==typeof t||"symbol"==typeof t?void 0:t;if(function(t){return!!t&&"object"==typeof t&&"function"==typeof t.transition&&"function"==typeof t.getInitialSnapshot}(t))return;if(function(t){return!!t&&"object"==typeof t&&"~standard"in t}(t))return;if(Array.isArray(t))return t.map(Ve).filter(t=>void 0!==t);if(t.constructor!==Object&&void 0!==t.constructor)return;const e={};for(const n of Object.keys(t)){const s=t[n];if(void 0!==s){const t=Ve(s);void 0!==t&&(e[n]=t)}}return e}function Je(t){if(Array.isArray(t)){const e=t.map(Je).filter(t=>void 0!==t);return e.length?e:void 0}const e=t,n={};for(const t of Object.keys(e)){const s=e[t];void 0!==s&&(n[t]="src"===t&&"string"!=typeof s?s.id:Ve(s),void 0===n[t]&&delete n[t])}return void 0===n.src?void 0:n}function ze(t){if(!t)return;const e={};for(const n of Object.keys(t)){const s=t[n];e[n]="function"==typeof s?void 0:Ve(s),void 0===e[n]&&delete e[n]}return e}function Be(t){const e={};for(const n of Object.keys(t)){if("states"===n||"invoke"===n)continue;const s=t[n];void 0!==s&&(e[n]=Ve(s))}if(void 0!==t.invoke){const n=Je(t.invoke);void 0!==n&&(e.invoke=n)}if(t.states){const n={};for(const e of Object.keys(t.states))n[e]=Be(t.states[e]);e.states=n}return e}function Fe(t){const e=Be(t);if(void 0!==t.internalEvents&&(e.internalEvents=Ve(t.internalEvents)),t.schemas){const n={};for(const e of Object.keys(t.schemas)){const s=t.schemas[e];n[e]=s&&"object"==typeof s&&!("~standard"in s)?ze(s):Ve(s),void 0===n[e]&&delete n[e]}e.schemas=n}for(const n of["actions","guards","actorSources","delays"])if(t[n]){const s=ze(t[n]);s&&Object.keys(s).length?e[n]=s:delete e[n]}return e}function qe(t={}){const{states:e={},schemas:n,actions:s,actorSources:o,guards:i,delays:r}=t;return{extend:e=>qe(function(t,e){return{...t,...e,schemas:Ge(t.schemas,e.schemas),states:Ue(t.states,e.states),actions:Ue(t.actions,e.actions),actorSources:Ue(t.actorSources,e.actorSources),guards:Ue(t.guards,e.guards),delays:Ue(t.delays,e.delays)}}(t,e)),createMachine(t){const e=Ge(t.schemas,n),c=Ue(s,t.actions),a=Ue(o,t.actorSources),u=Ue(i,t.guards),f=Ue(r,t.delays);return new ve({...t,...e?{schemas:e}:void 0,...c?{actions:c}:void 0,...a?{actorSources:a}:void 0,...u?{guards:u}:void 0,...f?{delays:f}:void 0})},createStateConfig:(...t)=>t.length>1?t[1]:t[0],states:e}}function Ue(t,e){return t||e?{...t,...e}:void 0}function Ge(t,e){if(t||e)return{...t,...e,events:Ue(t?.events,e?.events),emitted:Ue(t?.emitted,e?.emitted),children:Ue(t?.children,e?.children)}}function Xe(t){const e=te(t);return{self:e,defer:()=>{},id:"",logger:()=>{},sessionId:"",stopChild:()=>{},system:e.system,emit:()=>{},actionExecutor:()=>{}}}let Ze=function(t){return t.Internal="#_internal",t}({});const He={timeout:1/0};t.Actor=Qt,t.SimulatedClock=class{constructor(){this.timeouts=new Map,this._now=0,this._id=0,this._flushing=!1,this._flushingInvalidated=!1}now(){return this._now}getId(){return this._id++}setTimeout(t,e){this._flushingInvalidated=this._flushing;const n=this.getId();return this.timeouts.set(n,{start:this.now(),timeout:e,fn:t}),n}clearTimeout(t){this._flushingInvalidated=this._flushing,this.timeouts.delete(t)}set(t){if(this._now>t)throw new Error("Unable to travel back in time");this._now=t,this.flushTimeouts()}flushTimeouts(){if(this._flushing)return void(this._flushingInvalidated=!0);this._flushing=!0;const t=[...this.timeouts].sort(([t,e],[n,s])=>{const o=e.start+e.timeout;return s.start+s.timeout>o?-1:1});for(const[e,n]of t){if(this._flushingInvalidated)return this._flushingInvalidated=!1,this._flushing=!1,void this.flushTimeouts();this.now()-n.start>=n.timeout&&(this.timeouts.delete(e),n.fn.call(null))}this._flushing=!1,this._flushingInvalidated&&(this._flushingInvalidated=!1,this.flushTimeouts())}increment(t){this._now+=t,this.flushTimeouts()}},t.SpecialTargets=Ze,t.StateMachine=ve,t.StateNode=ye,t.TimeoutError=ce,t.__unsafe_getAllOwnEventDescriptors=function(t){return[...new Set([...t._nodes.flatMap(t=>t.ownEvents)])]},t.assertEvent=function(t,e){const n=It(e);if(!n.some(e=>Rt(t.type,e))){const e=1===n.length?`type matching "${n[0]}"`:`one of types matching "${n.join('", "')}"`;throw new Error(`Expected event ${JSON.stringify(t)} to have ${e}`)}},t.checkStateIn=function(t,e){if("string"==typeof e&&P(e)){const n=t.machine.getStateNodeById(e);return t._nodes.some(t=>t===n)}return t.matches(e)},t.createActor=te,t.createAsyncLogic=function(t){const e=t;return Ft({id:e.id,schemas:e.schemas,context:void 0,run:({event:t,input:s,self:o,system:i},r)=>{switch(t.type){case ie:{const e=t.data??t.output;return{status:"done",output:e,input:void 0,effects:{async:{status:"done",output:e}}}}case re:{const e=t.data??t.error;return{status:"error",error:e,input:void 0,effects:{async:{status:"error",error:e}}}}}if(t.type===n)return r.effect(()=>{const t=o,n=new AbortController,c=e.timeout,a=A(c),u=void 0===a?void 0:i._clock.setTimeout(()=>{"active"===o.getSnapshot().status&&(n.abort(),i._relay(t,t,{type:re,data:new ce(c)}))},a),f=()=>{void 0!==u&&i._clock.clearTimeout(u)};return Promise.resolve(e.run({input:s,system:i,self:o,signal:n.signal},{emit:r.emit,step:async(e,n)=>{const s=o.getSnapshot().effects?.[e];if("done"===s?.status)return s.output;if("error"===s?.status)throw s.error;if("active"===s?.status)return function(t,e){return new Promise((n,s)=>{const o=t.subscribe(t=>{const i=t.effects?.[e];"done"===i?.status?(o.unsubscribe(),n(i.output)):"error"===i?.status&&(o.unsubscribe(),s(i.error))})})}(o,e);i._relay(t,t,{type:Jt,key:e});try{const s=await n();return i._relay(t,t,{type:Wt,key:e,output:s}),s}catch(n){throw i._relay(t,t,{type:Vt,key:e,error:n}),n}}})).then(e=>{f(),"active"===o.getSnapshot().status&&i._relay(t,t,{type:ie,data:e})},e=>{f(),"active"===o.getSnapshot().status&&i._relay(t,t,{type:re,data:e})}),()=>{n.abort(),f()}}),{effects:{async:{status:"active"}}}}})},t.createCallbackLogic=function(t){const e="function"==typeof t?t:t.run;return Ft({schemas:"function"==typeof t?void 0:t.schemas,context:void 0,run:(t,n)=>{const{event:s,input:o,self:i,system:r}=t,c=t.emit,a=ee.get(i);a?.receivers?.forEach(t=>t(s)),n.effect("callback",()=>{const t={receivers:void 0,dispose:void 0};return ee.set(i,t),t.dispose=e({input:o,system:r,self:i,sendBack:t=>{if("stopped"===i.getSnapshot().status)return;const e=i._parent;e&&r._relay(i,e,t)},receive:e=>{t.receivers??=new Set,t.receivers.add(e)},emit:c}),()=>{ee.delete(i),t.receivers?.clear(),t.dispose?.()}})}})},t.createEmptyActor=ue,t.createEventObservableLogic=function(t){const e="function"==typeof t?t:t.run,n={config:e,transition:(t,e)=>{if("active"!==t.status)return[t,[]];switch(e.type){case se:return[{...t,status:"error",error:e.data,input:void 0,_subscription:void 0},[]];case oe:return[{...t,status:"done",input:void 0,_subscription:void 0},[]];case s:return t._subscription.unsubscribe(),[{...t,status:"stopped",input:void 0,_subscription:void 0},[]];default:return[t,[]]}},initialTransition:(t,e)=>[{status:"active",output:void 0,error:void 0,context:void 0,input:t,_subscription:void 0},[]],getInitialSnapshot:(t,e)=>n.initialTransition(e,t)[0],start:(t,{self:n,system:s,emit:o})=>{"done"!==t.status&&(t._subscription=e({input:t.input,system:s,self:n,emit:o}).subscribe({next:t=>{n._parent&&s._relay(n,n._parent,t)},error:t=>{s._relay(n,n,{type:se,data:t})},complete:()=>{s._relay(n,n,{type:oe})}}))},getPersistedSnapshot:({_subscription:t,...e})=>e,restoreSnapshot:t=>({...t,_subscription:void 0})};return n},t.createFSM=function(t){const e={id:t.id??"(fsm)",implementations:xe};!function(t){for(const[e,n]of Object.entries(t.states))for(const[t,s]of Object.entries(n.on??{}))for(const n of Se(s))if("string"==typeof n)throw new Error(`Invalid transition for "${e}.${t}": use { target: "${n}" } instead of a string target.`)}(t);const o=(t,e,n,s)=>{if(!s?.length)return[t,_e];const o=function(t){const e=[];for(const n of t){if(!n||"object"!=typeof n||!("action"in n)||"function"!=typeof n.action||we.has(n.action)||"_special"in n.action)return;e.push({type:n.action.name||"(anonymous)",params:void 0,args:n.args,exec:n.args.length?n.action.bind(null,...n.args):n.action})}return e}(s);return o?[t,o]:S(t,e,n,s)},i=(t,e,n,s,i,r)=>{if(!s)return[t,_e];const c=[],a=b(n,c,r,!0);let u;const f=Array.isArray(s)?s.length:1;for(let o=0;o<f;o++){const r=(Array.isArray(s)?s[o]:s)({context:u??t.context,event:e,input:i,value:t.value,self:n.self,system:n.system,parent:n.self._parent,children:t.children},a);if(void 0!==r?.context){const e=u??t.context,n=ke(e,r.context);n!==e&&(u=n)}}const h=void 0!==u?Ae(t,t.value,u,t._stateInput):t;return o(h,e,n,c)},r=(e,n,s,o)=>{const i=t.states[e.value],r=i?.on?.[n.type];if(!r)return;const c=Array.isArray(r)?r.length:1;for(let t=0;t<c;t++){const i=Array.isArray(r)?r[t]:r,c={context:e.context,event:n,input:e.input,value:e.value,self:s.self,system:s.system,parent:s.self._parent,children:e.children};if("function"==typeof i){const t=[],e=i(c,b(s,t,o,!0));if(!e){if(t.length)return{actions:t};continue}return{target:e.target,context:e.context,input:e.input,actions:t}}if(!("guard"in i)||!i.guard||i.guard(c))return{target:i.target,context:$e(i.context,c),input:i.input,actions:"actions"in i&&i.actions?Se(i.actions):be}}},c=(e,n,c)=>{if("active"!==e.status)return[e,[]];if(n.type===s)return[je(e),_e];const a=t.states[e.value],u=a?.on?.[n.type];if(!u)return[e,_e];if(!(Array.isArray(u)||"function"==typeof u||"guard"in u||"actions"in u&&u.actions||"function"==typeof u.input)){const s=u.target??e.value,o=s!==e.value;if(!o||!a.exit&&!t.states[s]?.entry){const t=void 0!==u.context,i=void 0!==u.input,r=$e(u.context,{context:e.context,event:n,input:e.input,value:e.value,self:c.self,system:c.system,parent:c.self._parent,children:e.children}),a=t&&r?ke(e.context,r):e.context;return o||a!==e.context||i||void 0!==e._stateInput?[Ae(e,s,a,i?Ee(u.input,a,n):void 0),_e]:[e,_e]}}if("function"==typeof u&&u.length<2&&!a?.exit){const s=u({context:e.context,event:n,input:e.input,value:e.value,self:c.self,system:c.system,parent:c.self._parent,children:e.children},void 0);if(s){const o=s.target??e.value;if(!t.states[o]?.entry){const t=void 0!==s.context,i=void 0!==s.input,r=t&&s.context?ke(e.context,s.context):e.context;return o!==e.value||r!==e.context||i||void 0!==e._stateInput?[Ae(e,o,r,i?Ee(s.input,r,n):void 0),_e]:[e,_e]}}}let f=e;const h=[],d=[n];let l=0;for(;d.length;){if(++l>1e3)throw new Error("FSM microstep count exceeded 1000");const e=d.shift(),n=r(f,e,c,d);if(!n)continue;const s=n.target??f.value,a=s!==f.value;if(a){const[n,s]=i(f,e,c,t.states[f.value]?.exit,f._stateInput,d);f=n,h.push(...s)}let u=f.context;void 0!==n.context&&(u=ke(u,n.context));const p=void 0!==n.input,y=p?Ee(n.input,u,e):void 0;(a||u!==f.context||p||void 0!==f._stateInput)&&(f=Ae(f,s,u,y));const[g,v]=o(f,e,c,n.actions);if(f=g,h.push(...v),a){const[n,o]=i(f,e,c,t.states[s]?.entry,y,d);f=n,h.push(...o)}}return[f,h]},a={id:t.id,config:t,transition:c,initialTransition:(s,o)=>{const r=function(t,e){return"function"==typeof t?t({input:e}):t??{}}(t.context,s),a=function(t,e,n,s,o){return{status:"active",output:void 0,error:void 0,value:t,context:e,input:n,children:{},_stateInput:o,machine:s}}(t.initial,r,s,e),u=[];let[f,h]=i(a,{type:n},o,t.states[t.initial]?.entry,void 0,u);for(h.length||(h=[]);u.length;){const[t,e]=c(f,u.shift(),o);f=t,h.push(...e)}return[f,h]},getInitialSnapshot:(t,e)=>a.initialTransition(e,t)[0],getPersistedSnapshot:({machine:t,...e})=>e,restoreSnapshot:t=>({...t,machine:e})};return a},t.createListenerLogic=p,t.createLogic=Ft,t.createMachine=me,t.createMachineFromConfig=function t(e,n={}){const s=function(t,e){return{actions:e.actions??{},guards:e.guards??{},actorSources:e.actorSources??{},delays:{...Ne(t.delays),...e.delays??{}},evaluators:e.evaluators??{}}}(e,n),o=function(t,e){function n(n,s){const o=n["@lang"]??t;if(!o)throw new Error(`Missing @exprLang for expression at ${s}`);const i=e.evaluators[o];if(!i)throw new Error(`Missing evaluator for @lang '${o}' at ${s}`);return i}function s(t,e,s,o){const i=Te(t)?"expr":"code",r=Te(t)?t["@expr"]:t["@code"];return n(t,o)({source:r,kind:i,slot:e,scope:s,path:o})}function o(t,e,n,i){if(Oe(t))return s(t,e,n,i);if(Array.isArray(t))return t.map((t,s)=>o(t,e,n,Me(i,s)));if(!t||"object"!=typeof t)return t;const r={};for(const s of Object.keys(t))r[s]=o(t[s],e,n,Me(i,s));return r}function i(t,e){return{context:t.context,event:t.event,input:t.input,self:t.self,children:t.children,params:t.params,...e}}return{evaluateResolvable:s,resolveValue:o,makeScope:i,getDurationConfig:function(t,e){return Oe(t)?n=>Ie(o(t,"delay",i(n),e)):t},assertResolvable:function t(e,s){if(Oe(e))n(e,s);else if(Array.isArray(e))e.forEach((e,n)=>t(e,Me(s,n)));else if(e&&"object"==typeof e)for(const n of Object.keys(e))t(e[n],Me(s,n))}}}(e["@exprLang"],s),{evaluateResolvable:i,resolveValue:r,makeScope:c,getDurationConfig:a}=o;function u(t,n,s){if(t)return Oe(t)?e=>!!i(t,n,c(e),s):o=>{const i=r(t.params,n,c(o),`${s}.params`),a=e.guards?.[t.type];if(a){const e=u(a.when,"guard",`$.guards.${t.type}.when`);return!!e?.({...o,params:i})}const f=o.guards?.[t.type];if(!f)throw new Error((h=t.type,d=o.guards??{},`Guard '${h}' is not implemented in machine '${e.id??"(machine)"}'. Available guards: ${Object.keys(d).map(t=>`'${t}'`).join(", ")||"(none)"}.`));var h,d;return f(o,i)}}function f(t,e){if(t)return Oe(t)?n=>i(t,"choice",c(n),e):(Pe(t,e),n=>{for(let s=0;s<t.length;s++){const o=t[s],i=u(o.when,"choice",`${e}[${s}].when`);if(!i||i(n))return{target:o.target,context:o.context?r(o.context,"transitionContext",c(n),`${e}[${s}].context`):void 0,input:void 0!==o.input?r(o.input,"input",c(n),`${e}[${s}].input`):void 0,description:o.description,reenter:o.reenter,meta:o.meta}}throw new Error(`Choice state at ${e} did not match any branch.`)})}const h={},d=[];let l=null;const p=[];function y(t,e){if(!t||"function"==typeof t)return t;if(Oe(t))return n=>i(t,"transition",c(n),e);const{guard:n,...s}=t;if(!n)return s;const o=u({type:n},"guard",e);return t=>o(t)?s:void 0}function g(e){return(Array.isArray(e)?e:[e]).map(e=>{const n=e;let o;return o=n._nestedMachineJSON?t(n._nestedMachineJSON,s):s.actorSources[e.src]??e.src,{src:o,id:e.id,registryKey:e.registryKey,input:void 0!==e.input?t=>r(e.input,"input",c(t),"$.invoke.input"):void 0,onDone:e.onDone?_(e.onDone):void 0,onError:e.onError?_(e.onError):void 0,onSnapshot:e.onSnapshot?_(e.onSnapshot):void 0,timeout:a(e.timeout,"$.invoke.timeout"),onTimeout:e.onTimeout?_(e.onTimeout):void 0}})}function v(t,e,n){const s=n instanceof Error?n.message:"string"==typeof n?n:"unknown error";t.enq.raise({type:"error.execution",_scxmlEventType:"platform",_scxmlEventData:{tagname:e,message:s,line:NaN,column:NaN,reason:s}}),t.errored=!0}function m(t,n,s,o,i,r){const c=e.actions?.[t.type];if(!c)return s(n.actions[t.type],o),{context:void 0,errored:i.errored};if(r.includes(t.type))return v(i,"action",new Error(`Circular action reference: ${r.concat(t.type).join(" -> ")}`)),{context:void 0,errored:!0};return x(Array.isArray(c)?c:[c],{...n,params:o},s,i,r.concat(t.type))}function x(t,e,n,s,o=[]){const a=s??{enq:n,errored:!1};let u;for(const s of t){if(a.errored)break;if(Oe(s)){const t=i(s,"action",c(e,{params:e.params,enq:n}),"$.actions");t&&"object"==typeof t&&"context"in t&&t.context&&(u??={},Object.assign(u,t.context))}else if(We(s))switch(s.type){case"@xstate.raise":{const t=void 0!==s.delay,o=r(s.event,"actionParams",c(e,{params:e.params}),"$.actions.event"),i=t||void 0!==s.id?{...o,...t?{_scxmlExternal:!0}:{},...void 0!==s.id?{_scxmlSendId:s.id}:{}}:o;n.raise(i,{id:s.id,delay:s.delay});break}case"@xstate.cancel":n.cancel(s.id);break;case"@xstate.log":n.log(...s.args);break;case"@xstate.emit":n.emit(r(s.event,"actionParams",c(e,{params:e.params}),"$.actions.event"));break;case"@xstate.assign":u??={},Object.assign(u,r(s.context,"transitionContext",c(e,{params:e.params}),"$.actions.context"));break;default:throw new Error(`Unknown built-in action: ${s.type}`)}else if("scxml.assign"===s.type){const t=s,n={...e.context,...u};let o;try{o=Le(n,t.expr,e.event,e.self)}catch(t){v(a,"assign",t);continue}if(t.location.includes(".")){v(a,"assign",new Error(`Invalid assign location: ${t.location}`));continue}u??={},u[t.location]=o}else if("scxml.raise"===s.type){const t=s,o={...e.context,...u};let i,r,c,f;try{if(i=t.eventexpr?Le(o,t.eventexpr,e.event,e.self):t.event||"unknown",r={type:i},t.params)for(const n of t.params)r[n.name]=Le(o,n.expr,e.event,e.self);c=t.targetexpr?Le(o,t.targetexpr,e.event,e.self):t.target;const n="#_internal"===c;f=t.delayexpr?Ie(Le(o,t.delayexpr,e.event,e.self)):n?void 0:t.delay}catch(t){v(a,"send",t);continue}const h="#_internal"===c,d="#_parent"===c;if("string"==typeof c&&c.length>0&&!c.startsWith("#_")){v(a,"send",new Error(`Invalid send target: ${c}`));continue}if(void 0!==t.id&&(r._scxmlSendId=t.id),d&&e.parent)n.sendTo(e.parent,r,void 0!==f?{delay:f}:void 0);else if("string"!=typeof c||!c.startsWith("#_")||d||h)void 0!==f&&(r._scxmlExternal=!0),n.raise(r,{id:t.id,delay:f});else{const s=c.slice(2),o=e.children?.[s];o?n.sendTo(o,r):(void 0!==f&&(r._scxmlExternal=!0),n.raise(r,{id:t.id,delay:f}))}}else if("scxml.cancel"===s.type){const t=s,o={...e.context,...u};try{const s=Le(o,t.sendidexpr,e.event,e.self);s&&n.cancel(s)}catch{}}else if("scxml.script"===s.type){const t=s,n={...e.context,...u};try{const e=Ke(n,t.code);u??={},Object.assign(u,e)}catch(t){v(a,"script",t)}}else if("scxml.foreach"===s.type){const t=s,o={...e.context,...u},i=t=>/^[$_a-zA-Z][$_a-zA-Z0-9]*$/.test(t);if(!i(t.item)||t.index&&!i(t.index)){v(a,"foreach",new Error("foreach item/index is not a legal location"));continue}let r;try{r=Le(o,t.array,e.event,e.self)}catch(t){v(a,"foreach",t);continue}if(!Array.isArray(r)){v(a,"foreach",new Error("foreach array is not iterable"));continue}u??={};for(let s=0;s<r.length;s++){u[t.item]=r[s],t.index&&(u[t.index]=s);const o={...e,context:{...e.context,...u}},i=x(t.actions,o,n,a);if(i.context&&Object.assign(u,i.context),a.errored)break}}else if("scxml.block"===s.type){const t=s,o={...e,context:{...e.context,...u}},i=x(t.actions,o,n);if(i.context){u??={};for(const t of Object.keys(i.context))i.context[t]!==e.context[t]&&(u[t]=i.context[t])}}else if("scxml.if"===s.type){const t=s,o={...e.context,...u};for(const s of t.branches){let t;if(s.cond)try{t=!!Le(o,s.cond,e.event,e.self)}catch(e){v(a,"if",e),t=!1}else t=!0;if(t){if(s.actions.length){const t={...e,context:o},i=x(s.actions,t,n,a);if(i.context){u??={};for(const t of Object.keys(i.context))i.context[t]!==e.context[t]&&(u[t]=i.context[t])}}break}}}else{const t=m(s,e,n,r(s.params,"actionParams",c(e,{params:e.params}),"$.actions.params"),a,o);t.context&&(u??={},Object.assign(u,t.context))}}return{context:u?{...e.context,...u}:void 0,errored:a.errored}}function _(t){const e=Array.isArray(t)?t:[t];return e.map((t,n)=>{if(Oe(t))return(s,o)=>i(t,"transition",c(s,{enq:o}),"$.transition"+(e.length>1?`[${n}]`:""));const s=Array.isArray(t.target)?t.target[0]:t.target,o=t.target,a=u(t.guard,"guard","$.transition.guard"),f=e=>t.context?r(t.context,"transitionContext",c(e),"$.transition.context"):void 0,p=e=>void 0!==t.input?r(t.input,"input",c(e),"$.transition.input"):void 0;return a||t.actions?.length?a&&!t.actions?.length?t.context?{guard:a,to:e=>({target:o,context:f(e),reenter:t.reenter}),description:t.description,meta:t.meta,input:void 0!==t.input?p:void 0}:{target:o,guard:a,description:t.description,reenter:t.reenter,meta:t.meta,input:void 0!==t.input?p:void 0}:s?{guard:a,description:t.description,reenter:t.reenter,meta:t.meta,input:void 0!==t.input?p:void 0,to:(e,n)=>{const i=f(e);if(t.actions?.length){const e=s.replace(/^#/,"");h[e]=t.actions,d.includes(t.actions)||d.push(t.actions)}return{target:o,context:i,reenter:t.reenter}}}:{guard:a,description:t.description,reenter:t.reenter,meta:t.meta,input:void 0!==t.input?p:void 0,to:(e,n)=>{const s=f(e);if(t.actions?.length){d.includes(t.actions)||d.push(t.actions),l??=e.context;const o=x(t.actions,e,n);if(o.context)return{context:{...s,...o.context}}}return s?{context:s}:{}}}:t.context?{to:e=>({target:o,context:f(e),reenter:t.reenter}),description:t.description,meta:t.meta,input:void 0!==t.input?p:void 0}:{target:o,description:t.description,reenter:t.reenter,meta:t.meta,input:void 0!==t.input?p:void 0}})}if(e.delays)for(const t of Object.keys(e.delays)){const n=e.delays[t];"number"==typeof n?s.delays[t]=n:"string"==typeof n?s.delays[t]=Ie(n):n&&"object"==typeof n&&(s.delays[t]=e=>Ie(r(n.duration,"delay",c(e),`$.delays.${t}.duration`)))}!function(t,e,n){const{assertResolvable:s}=n;function o(n,i){if(n)if(Oe(n))s(n,i);else if(s(n.params,`${i}.params`),t.guards?.[n.type])o(t.guards[n.type].when,`$.guards.${n.type}.when`);else if(!e.guards[n.type]&&!["scxml.cond","xstate.stateIn","xstate.not"].includes(n.type))throw new Error(`Missing guard implementation "${n.type}"`)}function i(n,o,r=[]){if(Oe(n))return void s(n,o);if(!n||"string"!=typeof n.type)throw new Error(`Invalid action at ${o}`);if(s(n.params,`${o}.params`),n.type.startsWith("@xstate.")||n.type.startsWith("scxml."))return void s(n,o);const c=t.actions?.[n.type];if(c){if(r.includes(n.type))throw new Error(`Circular action reference: ${r.concat(n.type).join(" -> ")}`);const t=Array.isArray(c)?c:[c];return void t.forEach((e,s)=>i(e,`${o}.actions.${n.type}${t.length>1?`[${s}]`:""}`,r.concat(n.type)))}if(!e.actions[n.type])throw new Error(`Missing action implementation "${n.type}"`)}function r(t,e){De(t).forEach((t,n)=>i(t,`${e}[${n}]`))}function c(t,e){(Array.isArray(t)?t:t?[t]:[]).forEach((n,i)=>{const c=Array.isArray(t)?`${e}[${i}]`:e;Oe(n)?s(n,c):(o(n.guard,`${c}.guard`),r(n.actions,`${c}.actions`),s(n.context,`${c}.context`),s(n.input,`${c}.input`))})}if(t.actions)for(const e of Object.keys(t.actions)){const n=Array.isArray(t.actions[e])?t.actions[e]:[t.actions[e]];n.forEach((t,s)=>i(t,`$.actions.${e}${n.length>1?`[${s}]`:""}`,[e]))}if(t.guards)for(const e of Object.keys(t.guards))o(t.guards[e].when,`$.guards.${e}.when`);if(t.delays)for(const e of Object.keys(t.delays)){const n=t.delays[e];s(n&&"object"==typeof n&&"duration"in n?n.duration:n,`$.delays.${e}`)}!function n(i,a){if(s(i.context,`${a}.context`),s(i.input,`${a}.input`),s(i.output,`${a}.output`),s(i.timeout,`${a}.timeout`),r(i.entry,`${a}.entry`),r(i.exit,`${a}.exit`),"choice"===i.type){if(!i.choice)throw new Error(`Choice state at ${a} must declare choice.`);Array.isArray(i.choice)?(Pe(i.choice,`${a}.choice`),i.choice.forEach((t,e)=>{o(t.when,`${a}.choice[${e}].when`),s(t.context,`${a}.choice[${e}].context`),s(t.input,`${a}.choice[${e}].input`)})):s(i.choice,`${a}.choice`)}if(Oe(i.route)&&s(i.route,`${a}.route`),i.invoke&&(Array.isArray(i.invoke)?i.invoke:[i.invoke]).forEach((t,n)=>{const o=`${a}.invoke${Array.isArray(i.invoke)?`[${n}]`:""}`;if(!t._nestedMachineJSON&&!e.actorSources[t.src])throw new Error(`Missing actorSource implementation "${t.src}"`);s(t.input,`${o}.input`),s(t.timeout,`${o}.timeout`),c(t.onDone,`${o}.onDone`),c(t.onError,`${o}.onError`),c(t.onSnapshot,`${o}.onSnapshot`),c(t.onTimeout,`${o}.onTimeout`)}),i.on)for(const t of Object.keys(i.on))c(i.on[t],`${a}.on.${t}`);if(i.after)for(const n of Object.keys(i.after)){if(Number.isNaN(Number(n))&&void 0===A(n)&&!t.delays?.[n]&&!e.delays[n])throw new Error(`Missing delay implementation "${n}"`);c(i.after[n],`${a}.after.${n}`)}if(c(i.always,`${a}.always`),c(i.onTimeout,`${a}.onTimeout`),i.states)for(const t of Object.keys(i.states))n(i.states[t],`${a}.states.${t}`)}(t,"$")}(e,s,o);const b=function t(e,n){const s=De(e.entry),o=e.id||n;var r,c;return{id:e.id,initial:e.initial,type:e.type,history:e.history,target:e.target,description:e.description,tags:e.tags,input:e.input,timeout:a(e.timeout,`$.states.${n}.timeout`),states:e.states?Object.entries(e.states).reduce((e,[n,s])=>(e[n]=t(s,n),e),{}):void 0,on:e.on?Object.entries(e.on).reduce((t,[e,n])=>(t[e]=_(n),t),{}):void 0,always:e.always?_(e.always):void 0,choice:f(e.choice,`$.states.${n}.choice`),route:y(e.route,`$.states.${n}.route`),after:e.after?Object.entries(e.after).reduce((t,[e,n])=>(t[e]=_(n),t),{}):void 0,onTimeout:e.onTimeout?_(e.onTimeout):void 0,entry:"choice"===e.type?void 0:(t,e)=>{for(;p.length;){const t=p.shift();e.raise({type:"error.execution",_scxmlEventType:"platform",_scxmlEventData:t})}let n;if(l&&d.length>0&&Object.keys(h).length>0){let s=l;for(const n of d){const o=x(n,{...t,context:s},e);o.context&&(s=o.context)}d.length=0,l=null;for(const t of Object.keys(h))delete h[t];n=s}else{const s=o?h[o]:void 0;if(s){delete h[o];const i=x(s,t,e);i.context&&(n=i.context)}l=null,d.length=0}if(s.length){const o=n?{...t,context:n}:t,i=x(s,o,e);i.context&&(n=i.context)}return{context:n}},exit:e.exit?(c=De(e.exit),(t,e)=>x(c,t,e)):void 0,invoke:e.invoke?g(e.invoke):void 0,meta:e.meta,output:e._scxmlDonedata?(r=e._scxmlDonedata,({context:t,event:e,self:n})=>{try{if(r.params&&r.params.length){const s={};for(const o of r.params)s[o.name]=Le(t,o.expr,e,n);return s}if(void 0!==r.contentExpr)return Le(t,r.contentExpr,e,n);if(void 0!==r.contentText)try{return Le(t,r.contentText,e,n)}catch{return r.contentText}}catch(t){const e=t instanceof Error?t.message:"string"==typeof t?t:"unknown error";p.push({tagname:"donedata",message:e,line:NaN,column:NaN,reason:e})}}):Oe(e.output)?({context:t,event:s,self:o})=>i(e.output,"output",{context:t,event:s,self:o},`$.states.${n}.output`):e.output}}(e),w=e.context?{context:t=>r(e.context,"context",t,"$.context")}:{};Re=e.id||"",Ce="session_"+Math.random().toString(36).slice(2);const S={"scxml.cond":({context:t,event:e,self:n},s)=>{const o=s?.expr;if(!o)return!0;try{return!!Le(t,o,e,n)}catch(t){const e=t instanceof Error?t.message:"string"==typeof t?t:"unknown error";return p.push({tagname:"cond",message:e,line:NaN,column:NaN,reason:e}),!1}},"xstate.stateIn":(t,e)=>{const n=e?.stateId;if(!n)return!1;const s=n.replace(/^#/,""),o=t._snapshot;return!!o?._nodes&&o._nodes.some(t=>t.id===s)},"xstate.not":(t,e)=>{const n=e?.guard,s=n&&t.guards?.[n.type];if(!s)throw new Error(`Guard '${n?.type}' referenced by 'xstate.not' is not implemented.`);return!s(t,n.params)}},$=me({...b,...w,version:e.version}).provide({actions:s.actions,actorSources:s.actorSources,guards:{...S,...s.guards},delays:s.delays});return $._json=e,$},t.createObservableLogic=function(t){const e="function"==typeof t?t:t.run;return Ft({schemas:"function"==typeof t?void 0:t.schemas,context:void 0,run:(t,n)=>{const{event:s,input:o,self:i,system:r}=t,c=t.emit;switch(s.type){case ne:return{context:s.data};case se:return{status:"error",error:s.data,input:void 0,effects:{observable:{status:"error",error:s.data}}};case oe:return{status:"done",input:void 0,effects:{observable:{status:"done"}}}}n.effect("observable",()=>{const t=i,n=e({input:o,system:r,self:i,emit:c}).subscribe({next:e=>{r._relay(t,t,{type:ne,data:e})},error:e=>{r._relay(t,t,{type:se,data:e})},complete:()=>{r._relay(t,t,{type:oe})}});return()=>n.unsubscribe()})}})},t.createStateConfig=function(t){return t},t.createSubscriptionLogic=g,t.createSystem=function(t={}){const e={},n=[];return{createActor(t,s){const o=te(t,{...s,_systemRef:e});return(()=>{const t=e.current;if(t)for(const e of n)e.active&&!e.subscription&&(e.subscription=t.inspect(e.observer))})(),o},get:t=>e.current?.get(t),getAll:()=>e.current?.getAll()??{},inspect(t){const s=e.current;if(s)return s.inspect(t);const o={observer:t,active:!0};return n.push(o),{unsubscribe(){o.active=!1,o.subscription?.unsubscribe()}}},setup:t=>t?qe(t):qe()}},t.getInitialMicrosteps=function(t,...[e]){const n=Xe(t),s=c(e),o=[],i=t._getPreInitialState(n,s),r=Y(t.root,i,n,s,o),{microsteps:a}=st(r[0],s,n,o);return[r,...a]},t.getInitialSnapshot=function(t,...[e]){const n=Xe(t);return t.initialTransition(e,n)[0]},t.getMicrosteps=function(t,e,n){const s=Xe(t),{microsteps:o}=st(e,n,s,[]);return o},t.getNextSnapshot=function(t,e,n){const s=Xe(t);s.self._snapshot=e;const o=t.transition(e,n,s);return Array.isArray(o)?o[0]:o},t.getNextTransitions=function(t){const e=[],n=t._nodes.filter(I),s=new Set;for(const t of n)for(const n of[t].concat(O(t,void 0)))if(!s.has(n.id)){s.add(n.id);for(const[,t]of n.transitions.entries())e.push(...t);n.always&&e.push(...n.always)}return e},t.getStateNodes=B,t.initialTransition=function(t,...[e]){const n=Xe(t),[s,o]=t.initialTransition(e,n);return[s,o]},t.isBuiltInExecutableAction=function(t){return Object.prototype.hasOwnProperty.call(f,t.type)},t.isMachineSnapshot=ut,t.isTypeSchema=function(t){return!!t&&"object"==typeof t&&"~standard"in t&&"xstate.types"===t["~standard"].vendor},t.listenerLogic=y,t.machineConfigToJSON=Fe,t.mapState=function(t,e){const n=[],s=(t,e)=>{let n=t;for(const t of e){if(!n?.states)return;const e=n.states;if(!(t in e))return;n=e[t]}return n},o=new Set;for(const i of t._nodes.filter(I)){let r=i;for(;r&&!o.has(r);){o.add(r);const i=s(e,r.path);i?.map&&n.push({stateNode:r,result:i.map(t)}),r=r.parent}}return n},t.matchesState=$t,t.pathToStateValue=At,t.serializeMachine=function(t){return t._json??Fe(t.config)},t.setup=qe,t.subscriptionLogic=v,t.toObserver=Pt,t.toPromise=function(t){return new Promise((e,n)=>{t.subscribe({complete:()=>{e(t.getSnapshot().output)},error:n})})},t.transition=function(t,e,n){const s=Xe(t),[o,i]=t.transition(e,n,s);return[o,i]},t.types=function(){return{"~standard":{version:1,vendor:"xstate.types",validate:t=>({value:t})}}},t.waitFor=function(t,e,n){const s={...He,...n};return new Promise((n,o)=>{const{signal:i}=s;if(i?.aborted)return void o(i.reason);let r=!1;const c=s.timeout===1/0?void 0:setTimeout(()=>{a(),o(new Error(`Timeout of ${s.timeout} ms exceeded`))},s.timeout),a=()=>{clearTimeout(c),r=!0,h?.unsubscribe(),f&&i.removeEventListener("abort",f)};function u(t){e(t)&&(a(),n(t))}let f,h;u(t.getSnapshot()),r||(i&&(f=()=>{a(),o(i.reason)},i.addEventListener("abort",f)),h=t.subscribe({next:u,error:t=>{a(),o(t)},complete:()=>{a(),o(new Error("Actor terminated without satisfying predicate"))}}),r&&h.unsubscribe())})},Object.defineProperty(t,"__esModule",{value:!0})});
2
2
  //# sourceMappingURL=xstate.umd.min.js.map