xstate 6.0.0-beta.2 → 6.0.0-beta.3

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 (65) hide show
  1. package/README.md +4 -2
  2. package/dist/{createActor-20a97166.esm.js → createActor-49ea55ba.esm.js} +17 -8
  3. package/dist/{createActor-c91c9ffe.cjs.js → createActor-73aa92ba.cjs.js} +17 -7
  4. package/dist/{createActor-0250284a.development.esm.js → createActor-a085b20b.development.esm.js} +17 -8
  5. package/dist/{createActor-c029d29f.development.cjs.js → createActor-dfb6b790.development.cjs.js} +17 -7
  6. package/dist/declarations/src/fsm.d.ts +7 -7
  7. package/dist/{effectDescriptor-47f73e7e.cjs.js → effectDescriptor-3402f63e.cjs.js} +1 -1
  8. package/dist/{effectDescriptor-e93df910.esm.js → effectDescriptor-65821048.development.esm.js} +1 -1
  9. package/dist/{effectDescriptor-5ac7dccb.development.esm.js → effectDescriptor-72c62058.esm.js} +1 -1
  10. package/dist/{effectDescriptor-3fff5c51.development.cjs.js → effectDescriptor-d0ea44ad.development.cjs.js} +1 -1
  11. package/dist/{legacySnapshot-ae6594a7.development.esm.js → legacySnapshot-11aba2d2.development.esm.js} +1 -1
  12. package/dist/{legacySnapshot-ebf0b2b8.development.cjs.js → legacySnapshot-2e2c1d3b.development.cjs.js} +1 -1
  13. package/dist/{legacySnapshot-8d595687.cjs.js → legacySnapshot-a85dfbe2.cjs.js} +1 -1
  14. package/dist/{legacySnapshot-9e06feb4.esm.js → legacySnapshot-fffe81a0.esm.js} +1 -1
  15. package/dist/xstate-actors.cjs.js +30 -5
  16. package/dist/xstate-actors.development.cjs.js +30 -5
  17. package/dist/xstate-actors.development.esm.js +31 -6
  18. package/dist/xstate-actors.esm.js +31 -6
  19. package/dist/xstate-actors.umd.min.js +1 -1
  20. package/dist/xstate-actors.umd.min.js.map +1 -1
  21. package/dist/xstate-durable.cjs.js +3 -3
  22. package/dist/xstate-durable.development.cjs.js +3 -3
  23. package/dist/xstate-durable.development.esm.js +3 -3
  24. package/dist/xstate-durable.esm.js +3 -3
  25. package/dist/xstate-durable.umd.min.js +1 -1
  26. package/dist/xstate-durable.umd.min.js.map +1 -1
  27. package/dist/xstate-fsm.cjs.js +61 -31
  28. package/dist/xstate-fsm.development.cjs.js +61 -31
  29. package/dist/xstate-fsm.development.esm.js +61 -31
  30. package/dist/xstate-fsm.esm.js +61 -31
  31. package/dist/xstate-fsm.umd.min.js +1 -1
  32. package/dist/xstate-fsm.umd.min.js.map +1 -1
  33. package/dist/xstate-graph.cjs.js +1 -1
  34. package/dist/xstate-graph.development.cjs.js +1 -1
  35. package/dist/xstate-graph.development.esm.js +2 -2
  36. package/dist/xstate-graph.esm.js +2 -2
  37. package/dist/xstate-persist.cjs.js +2 -2
  38. package/dist/xstate-persist.development.cjs.js +2 -2
  39. package/dist/xstate-persist.development.esm.js +3 -3
  40. package/dist/xstate-persist.esm.js +3 -3
  41. package/dist/xstate-persist.umd.min.js +1 -1
  42. package/dist/xstate-persist.umd.min.js.map +1 -1
  43. package/dist/xstate-validation.cjs.js +1 -1
  44. package/dist/xstate-validation.development.cjs.js +1 -1
  45. package/dist/xstate-validation.development.esm.js +1 -1
  46. package/dist/xstate-validation.esm.js +1 -1
  47. package/dist/xstate.cjs.js +26 -8
  48. package/dist/xstate.development.cjs.js +26 -8
  49. package/dist/xstate.development.esm.js +28 -10
  50. package/dist/xstate.esm.js +28 -10
  51. package/dist/xstate.umd.min.js +1 -1
  52. package/dist/xstate.umd.min.js.map +1 -1
  53. package/docs/actor-logic.md +1 -1
  54. package/docs/api-stability.json +93 -81
  55. package/docs/faq.md +1 -1
  56. package/docs/fsm.md +53 -10
  57. package/docs/index.md +2 -1
  58. package/docs/inspect-actor-systems.md +20 -3
  59. package/docs/inspection.md +5 -1
  60. package/docs/invoke.md +2 -2
  61. package/docs/migrate-inspector.md +141 -0
  62. package/docs/system-transitions.md +2 -0
  63. package/docs/transitions.md +5 -1
  64. package/docs/xstate-v5-to-v6.md +6 -4
  65. 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).XStatePersist={})}(this,function(t){"use strict";const e={$actor:["@actor",["incarnation"]],$error:["@error",["message"]],$stateNode:["@stateNode",[]],$systemMapper:["@systemMapper",[]]},s={_stateInputs:"stateInputs",_nextTimerId:"nextTimerId",_nextActorIds:"nextActorIds"};function n(t){if(!t||"object"!=typeof t)return!1;const e=Object.getPrototypeOf(t);return e===Object.prototype||null===e||Array.isArray(t)}function r(t,s){if(!n(t))return t;if(Array.isArray(t)){const e=t.map(t=>r(t,s));return e.some((e,s)=>e!==t[s])?e:t}const o=Object.keys(t),i=o.find(t=>s.includes(t));if(i&&"string"==typeof t[i]&&o.every(t=>t===i||e[i][1].includes(t))){const{[i]:s,...n}=t;return{[e[i][0]]:s,...n}}let c;for(const e of o){const n=r(t[e],s);n!==t[e]&&((c??={...t})[e]=n)}return c??t}function o(t){return t?.machine&&"function"==typeof t.matches?t:n(e=t)&&"_nextTimerId"in e&&!("nextTimerId"in e)?r(function(t){if(!n(t)||"function"==typeof t.matches)return t;let e;for(const[n,r]of Object.entries(s))if(n in t&&!(r in t)){const{[n]:s,...o}=e??t;e={...o,[r]:s}}return e??t}(t),["$actor"]):t;var e}function i(t,...e){return`XState error E${t}${e.length?` (${e.map(t=>"string"==typeof t?JSON.stringify(t):String(t)).join(", ")})`:""}. See https://github.com/statelyai/xstate/blob/next/docs/errors.md#e${t}`}const c=new Set(["clock","logger","syncSnapshot","id","registryKey","input","src","inspect","onRejectedEvent","onUnhandledEvent","_systemRef","_sessionId","_inert","_restore"]);function a(t){if(t&&"object"==typeof t)for(const e of Object.keys(t))if(!c.has(e))throw new Error(i(8,e))}const u="xstate.init",h="@xstate.spawn",d="xstate.stop",f="@xstate.terminate",l="xstate.timer",p={unsubscribe(){}};function y(t){return!0===t._remote}function g(t){if("object"!=typeof t||null===t||Array.isArray(t)||"string"!=typeof t["@actor"])return!1;for(const e in t)if("@actor"!==e&&"incarnation"!==e)return!1;const e=t.incarnation;return void 0===e||"string"==typeof e}function v(t){const e=t.slice(t.lastIndexOf("/")+1);return e.includes("%")?e.replaceAll("%2F","/").replaceAll("%25","%"):e}function _(t){return new Error(i(50,...void 0===t.incarnation?[t["@actor"]]:[t["@actor"],t.incarnation]))}function m(t,e){const s=t._getByAddress?.(e["@actor"]);return!s||void 0!==e.incarnation&&s.sessionId!==e.incarnation?void 0:s}const x=Object.freeze({status:"active",output:void 0,error:void 0});function w(t,e){const s={_remote:!0,ref:void 0,id:e.id,address:e.address,src:e.src,registryKey:e.registryKey,sessionId:e.incarnation,system:t,_parent:e.parent,_syncSnapshot:e.syncSnapshot,send(e){t.sendEvent(void 0,n,e)},_send(t){throw new Error(i(52,e.address,t.type))},getSnapshot:()=>x,getPersistedSnapshot(){throw new Error(i(53,e.address))},start(){},_stop(){},stop(){throw new Error(i(54,e.address))},select(){throw new Error(i(55,e.address))},get trigger(){throw new Error(i(56,e.address))},subscribe:()=>p,on:()=>p,_isRunning:()=>!1,toJSON:()=>({"@actor":e.address})},n=s;return s.ref=n,n}function b(t,e,s){return t.system._notifyEmitted(t,e),s.emitEvent(t,e)}const S=new WeakMap;async function I(t,e,s,n=e=>{t.system.sendEvent(t,t,e)}){const r=function(t,e){const s=t.getSnapshot().effects;return s&&Object.prototype.hasOwnProperty.call(s,e)?s[e]:void 0}(t,e);if("done"===r?.status)return r.output;if("error"===r?.status)throw r.error;const o=S.get(t)?.get(e);if(o)return o;if("active"!==t.getSnapshot().status)throw new Error(i(72,e));let c,a;const u=new Promise((t,e)=>{c=t,a=e});let h,d=S.get(t);d||(d=new Map,S.set(t,d)),d.set(e,u);const f=()=>{h?.unsubscribe(),d.delete(e),d.size||S.delete(t)};return u.then(f,f),h=t.subscribe({error:a,complete:()=>a(new Error(i(72,e)))}),(async()=>{try{n({type:"xstate.logic.effect.start",key:e});const r=await s();"active"===t.getSnapshot().status&&(n({type:"xstate.logic.effect.resolve",key:e,output:r}),c(r))}catch(s){"active"===t.getSnapshot().status&&n({type:"xstate.logic.effect.reject",key:e,error:s}),a(s)}})().catch(a),u}function A(t,e,s){const n=function(t,e,s){const n=e;if(t!==e&&n.logic?.isInternalEventType?.(s.type))return new Error(i(73,s.type,e.id))}(t,e,s);return!!n&&(e.system.deadLetter(t,e,s,"internalEvent",{error:n}),!0)}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}}function k(t,e){return{type:"xstate.after",delay:t,stateId:e}}function O(t,e){return{type:"xstate.timeout.actor",actorId:t,sessionId:e}}function j(t,e){return{type:"xstate.done.state",stateId:t,output:e}}function T(t,e){return{type:`xstate.error.${t}`,error:e}}function M(t){return{type:u,input:t}}function R(t){setTimeout(()=>{throw t})}const P="function"==typeof Symbol&&Symbol.observable||"@@observable",$=[],K=new Error("Transition effect");function q(t,e="abort"){const s={mode:e,parent:t,requiresReevaluation:!1};return $.push(s),{frame:s,close(){$.pop()}}}function D(t){const{stateId:e,delay:s,actorId:n}=t;switch(t.type){case"xstate.after":return{type:t.type,delay:s,stateId:e};case"xstate.timeout.state":return{type:t.type,stateId:e};case"xstate.timeout.actor":return{type:t.type,actorId:n}}}function N(t){return void 0!==t.id?{id:t.id}:void 0!==t.seq?{seq:t.seq}:{event:D(t.event)??t.event}}function z(t){const e=t;return"string"==typeof e.id?{id:e.id}:"number"==typeof e.seq?{seq:e.seq}:e.event&&"object"==typeof e.event&&"string"==typeof e.event.type?{event:e.event}:void 0}function L(t,e){if("id"in e)return t.id===e.id;if(void 0!==t.id)return!1;if("seq"in e)return t.seq===e.seq;if(void 0!==t.seq)return!1;const s=t.event,n=e.event;for(const t in n)if(s[t]!==n[t])return!1;return!0}function V(t,e){return`${t.sessionId}|${function(t){return"id"in t?JSON.stringify(["id",t.id]):"seq"in t?JSON.stringify(["seq",t.seq]):JSON.stringify(["event",D(t.event)??t.event])}(e)}`}const C=new WeakMap;function W(t){return C.get(t)??t.scheduledAt}function U(t){if("string"==typeof t)throw new Error(i(70,t))}const J={"@xstate.spawn":t=>t.system.spawnActor(t._parent,t),"@xstate.start":t=>t.system.startActor(t),"@xstate.raise":(t,e,s)=>t.system.scheduleTimer(t.self,N({id:s.id,seq:s.seq,event:e}),s?.delay??0),"@xstate.sendTo":(t,e,s,n)=>(U(s),void 0!==n?.delay?t.system.scheduleTimer(t.self,N({id:n.id,seq:n.seq,event:s}),n?.delay??0):t.system._relay(t.self,e,s)),"@xstate.cancel":(t,e)=>t.system.cancelTimer(t.self,e),"@xstate.stop":(t,e)=>t.system.stopActor(e),"@xstate.terminate":(t,e)=>t.system.terminateActor(t,e)},H=new WeakMap;function B(t,e){let s=H.get(t);if(!s){s=new Map;for(const e of Object.keys(t)){const n=t[e];s.has(n)||s.set(n,e)}H.set(t,s)}return s.get(e)}const F=Symbol("xstate.systemLogicMetadata"),X=new WeakMap;function G(t,e){const s=(t,e)=>[{status:"active",output:void 0,error:void 0,input:t},[]],n={id:e,start:(e,{self:s,system:n})=>{const r=e.input.actor;if(!r||"stopped"===r.getSnapshot().status)return;const o=t(e.input,{self:s,system:n});if(!o)return;let i,c=!1;const a=()=>{c||(c=!0,o.unsubscribe(),i?.unsubscribe())},u=s._parent;u&&(i=u.subscribe({complete:a,error:a})),X.set(s,{unsubscribe:a})},transition:(t,e,{self:s})=>e.type===d?(X.get(s)?.unsubscribe(),X.delete(s),[{...t,status:"stopped",error:void 0},[]]):[t,[]],initialTransition:s,getInitialSnapshot:(t,e)=>s(e)[0],getPersistedSnapshot:t=>t,restoreSnapshot:t=>t};return Object.defineProperty(n,F,{value:{kind:e}}),n}function Q(t,e,s){"stopped"!==t.getSnapshot().status&&t._parent&&e._relay(t,t._parent,s())}function Y(){return G(({actor:t,eventType:e,mapper:s},{self:n,system:r})=>{const o="*"!==e&&e.endsWith(".*");return t.on(o?"*":e,t=>{o&&!Ws(t.type,e)||Q(n,r,()=>s(t))})},"@xstate.listener")}const Z=Y();function tt(){return G(({actor:t,mappers:e},{self:s,system:n})=>{const{done:r,error:o,snapshot:i}=e;return t.subscribe({next:t=>{"done"===t.status&&r?Q(s,n,()=>r(t.output)):"error"===t.status&&o?Q(s,n,()=>o(t.error)):"active"===t.status&&i&&Q(s,n,()=>i(t))},error:t=>{o&&Q(s,n,()=>o(t))},complete:()=>{}})},"@xstate.subscription")}const et=tt();function st(){const t=this.action?.(...this.args);return t}function nt(t=this.source.system){return b(this.source,this.event,t)}function rt(t=(this.target??this.source).system){return t.deadLetter(this.source,this.target,this.event,this.reason,this.detail)}function ot(t,e,s,n,r){return{kind:"builtin",type:"@xstate.deadLetter",exec:rt,source:e,target:t.self,event:s,reason:n,detail:r,params:void 0,args:[]}}function it(t,e,s){return{kind:"builtin",type:"@xstate.deadLetter",exec:rt,source:t.self,target:void 0,event:e,reason:"missingTarget",detail:{targetId:s,error:new Error(i(65,s))},params:void 0,args:[]}}function ct(t=this.actor.system){return t.spawnActor(this.source,this.actor)}function at(t=this.actor.system){if(2!==this.actor._processingStatus)return t.startActor(this.actor)}function ut(t=this.source.system){return t.stopActor(this.actor)}function ht(t=this.actor.system){const e="done"===this.status?{status:"done",output:this.output,error:void 0}:{status:"error",output:void 0,error:this.error};return t.terminateActor(this.actor,e)}function dt(t=this.source.system){return t.scheduleTimer(this.source,N(this),this.delay??0)}function ft(t=this.source.system){return U(this.event),void 0!==this.delay?t.scheduleTimer(this.source,N(this),this.delay):t.sendEvent(this.source,this.target,this.event)}function lt(t=this.source.system){return t.cancelTimer(this.source,z(this))}function pt(t,e,s){if(!Lt(e))return t;if("@xstate.cancel"===e.type){const s=z(e);return t.timers?.some(t=>L(t,s))?{...t,timers:t.timers.filter(t=>!L(t,s))}:t}if("@xstate.raise"!==e.type&&"@xstate.sendTo"!==e.type||void 0===e.delay)return t;let n=t._nextTimerId??0;if(void 0===e.id&&!D(e.event)){e.seq=n++;const t=e.args;t[t.length-1]={...t[t.length-1],seq:e.seq}}const r="@xstate.raise"===e.type||e.target===s.self?"self":e.target,o={...void 0!==e.id&&{id:e.id},...void 0!==e.seq&&{seq:e.seq},delay:e.delay,type:e.type,event:e.event,target:r},i=N(o),c=t.timers??[],a=c.findIndex(t=>L(t,i));return{...t,timers:-1===a?[...c,o]:c.map((t,e)=>e===a?o:t),_nextTimerId:n}}function yt(t,e){return{...t,...e}}function gt(t,e,...s){const n={action:e,args:s};return t.push(n),n}function vt(t,e){const s={...t};for(const t of Object.keys(e))s[t]=Math.max(s[t]??0,e[t]);return s}function _t(t,e,s){if("add"===e.type)return{...t,children:{...t.children,[e.id]:e.actor},...e.counters&&{_nextActorIds:vt(t._nextActorIds,e.counters)}};const n={...t.children};let r=e.actor._parent===s.self;for(const t of Object.keys(n))n[t]===e.actor&&(r=!0,delete n[t]);if(!r)throw new Error(i(47,e.actor.id,s.self.id));return s.system._unregister(e.actor),{...t,children:n}}function mt(t){if("object"==typeof t&&null!==t&&"action"in t&&"function"==typeof t.action)return t}const xt=new WeakMap,wt=()=>({counters:new Map,explicitIds:new Set,stoppedIds:new Set});function bt(t){xt.set(t,wt())}function St(t){return t.self._snapshot}function It(t,e,s){return Math.max(e.counters.get(s)??0,St(t)?._nextActorIds?.[s]??0)}function At(t,e,s){let n=It(t,e,s);const r=St(t)?.children;for(;e.explicitIds.has(`${s}:${n}`)||r?.[`${s}:${n}`];)n++;return n}function Et(t,e,s){const n=xt.get(t)??s??wt(),r=Bs(e),o=At(t,n,r);return n.counters.set(r,o+1),{id:`${r}:${o}`,counters:{[r]:o+1}}}function kt(t,e,s){const n=xt.get(t)??s??wt(),r=St(t)?.children?.[e],o=void 0!==r&&!n.stoppedIds.has(e)&&"active"===r.getSnapshot().status;if(n.explicitIds.has(e)||o)throw new Error(i(67,e,t.self.id));n.explicitIds.add(e)}function Ot(t,e,s){const n=Xs(e);if(!n)return;n.prefix;const r=xt.get(t)??s??wt(),o=Math.max(It(t,r,n.prefix),n.index+1);return r.counters.set(n.prefix,o),{[n.prefix]:o}}const jt=new WeakSet;function Tt(t){if("string"==typeof t)throw new Error(i(68,t))}function Mt(t,e,s,n=!1,r=!0){const o=xt.get(t)??wt(),c={cancel:s=>{gt(e,J["@xstate.cancel"],t,"string"==typeof s?{id:s}:s)},emit:t=>{e.push(t)},log:(...s)=>{gt(e,t.logger,...s)},raise:(n,r)=>{Tt(n),void 0!==r?.delay?gt(e,J["@xstate.raise"],t,n,r):s.push(n)},spawn:(s,n)=>{const{logic:c,src:a}=function(t,e){const s=function(t){return t.self.logic?.sources?.actors}(t);if("string"==typeof e){const t=s?.[e];if(!t)throw new Error(i(66,e));return{logic:t,src:e}}return s?{logic:e,src:B(s,e)}:{logic:e,src:void 0}}(t,s);if(!r){const e=Bs(a??c),s=n?.id??`${e}:${At(t,xt.get(t)??o,e)}`;return function(t){const{id:e,address:s}=t,n={ref:void 0,id:e,address:s,src:t.src,sessionId:void 0,send(){},getSnapshot:()=>x,subscribe:()=>p,on:()=>p,toJSON(){if(void 0===s)throw new Error(i(51,e));return{"@actor":s}}},r=n;return n.ref=r,r}({id:s,address:jt.has(t)?void 0:`${t.self.address}/${Fs(s)}`,src:a})}let u,h=n?.id;void 0===h?({id:h,counters:u}=Et(t,a??c,o)):(kt(t,h,o),u=Ot(t,h,o));const d=t.system.createActorRef(c,{...n,...void 0!==a&&{src:a},id:h,parent:t.self});return function(t,e,s,n){gt(t,J["@xstate.spawn"],e).childUpdate={type:"add",actor:e,id:s,counters:n}}(e,d,h,u),d},sendTo:(s,n,r)=>{gt(e,J["@xstate.sendTo"],t,s,n,r)},stop:s=>{if(s){const n=s;gt(e,J["@xstate.stop"],t,n).childUpdate={type:"remove",actor:n},function(t,e){const s=xt.get(t);s&&(s.stoppedIds.add(e.id),s.explicitIds.delete(e.id))}(t,n)}}};n&&Object.assign(c,{listen:(s,n,o)=>{if(!r)return{id:void 0};const i={actor:s,eventType:n,mapper:o},c=t.system.createActorRef(Z,{input:i,parent:t.self});return gt(e,J["@xstate.spawn"],c),c},subscribeTo:(s,n)=>{if(!r)return{id:void 0};const o={actor:s,mappers:"function"==typeof n?{snapshot:n}:n},i=t.system.createActorRef(et,{input:o,parent:t.self});return gt(e,J["@xstate.spawn"],i),i}});let a=!1;const u=t=>(...e)=>a?$t():t(...e);for(const t of Object.keys(c))c[t]=u(c[t]);const h=Ct(c,u((t,...s)=>{"function"==typeof t&&gt(e,t,...s)}));return Rt.set(h,()=>{a=!0}),h}const Rt=new WeakMap;function Pt(t){Rt.get(t)?.()}function $t(){}function Kt(t,e){switch(t){case J["@xstate.spawn"]:{const[t]=e;return{type:"@xstate.spawn",kind:"builtin",exec:ct,source:t._parent,actor:t,id:t.id,logic:t.logic,src:t.src,input:t.options?.input}}case J["@xstate.raise"]:{const[,t,s]=e;return{type:"@xstate.raise",kind:"builtin",exec:dt,source:e[0].self,event:t,id:s?.id,delay:s?.delay}}case J["@xstate.sendTo"]:{const[,t,s,n]=e;return{type:"@xstate.sendTo",kind:"builtin",exec:ft,source:e[0].self,target:t,event:s,id:n?.id,delay:n?.delay}}case J["@xstate.cancel"]:{const[,t]=e;return{type:"@xstate.cancel",kind:"builtin",exec:lt,source:e[0].self,...t}}case J["@xstate.stop"]:{const[,t]=e;return{type:"@xstate.stop",kind:"builtin",exec:ut,source:e[0].self,actor:t,id:t.id}}default:return}}function qt(t){const e=[t];return{kind:"builtin",exec:at,type:"@xstate.start",source:t._parent,params:void 0,args:e,actor:t,id:t.id}}function Dt(t,e,s){const n=[t,e,s,{}];return{kind:"builtin",exec:ft,type:"@xstate.sendTo",source:t.self,target:e,event:s,id:void 0,delay:void 0,params:void 0,args:n}}function Nt(t,e){if("done"!==e.status&&"error"!==e.status)throw new Error(i(69,t.self.id,e.status));const s="done"===e.status?{status:"done",output:e.output,error:void 0}:{status:"error",output:void 0,error:e.error},n=[t.self,s];return{kind:"builtin",exec:ht,type:f,source:t.self,actor:t.self,id:t.self.id,...s,params:void 0,args:n}}function zt(t,e,[s,n]){const r="done"===s.status||"error"===s.status,o="done"===e?.status||"error"===e?.status,i=n.some(t=>"object"==typeof t&&null!==t&&"type"in t&&t.type===f);return!r||o||i?[s,n]:[s,[...n,Nt(t,s)]]}function Lt(t){return"builtin"===t.kind}function Vt(t,e,s,n){let r=t;const o=[];for(const t of n){const e="function"==typeof t,n=mt(t);let i=n?.args;if(n?.action===J["@xstate.sendTo"]&&(void 0===n.args[1]||"string"==typeof n.args[1])){const t=n.args[1],e=void 0!==t&&Object.hasOwn(r.children,t)?r.children[t]:void 0;if(!e){o.push(it(s,n.args[2],t));continue}i=[n.args[0],e,...n.args.slice(2)]}const c=e?t:!!n&&n.action.bind(null,...i);let a;if("object"==typeof t&&null!==t){const{type:e,childUpdate:s,...n}=t;a=n}if(n?.childUpdate&&(r=_t(r,n.childUpdate,s)),!c||!("resolve"in c)){const c="object"==typeof t&&null!==t&&"action"in t&&"function"==typeof t.action?Kt(t.action,i):void 0,u="object"==typeof t&&null!==t&&!n,h={kind:c?"builtin":u?"emit":"action",type:"object"==typeof t?"action"in t&&"function"==typeof t.action?t.action.name??"(anonymous)":t.type:t.name||"(anonymous)",params:c?void 0:a,args:"object"==typeof t&&"action"in t?i:[],...c?{}:u?{source:s.self,event:t}:{action:n?.action??(e?t:void 0)},...c?{}:{exec:u?nt:st},...c};r=pt(r,h,s),o.push(h);continue}}return[r,o]}function Ct(t,e){const s=(t,...s)=>{e(t,...s)};return Object.assign(s,{cancel:Wt,emit:Wt,log:Wt,raise:Wt,spawn:Ut,sendTo:Wt,stop:Wt,listen:Ut,subscribeTo:Ut,...t}),s}const Wt=()=>{},Ut=()=>({});let Jt;function Ht(){return Jt}const Bt=new WeakMap,Ft=["runtime","_timerMap","_rejectionListeners","_inspectionObservers","_emissionListeners"],Xt=new WeakMap;let Gt;const Qt=new WeakMap,Yt=new WeakMap,Zt=new Map;function te(t){const e=t._peekKeyedActors;return e?e.call(t)??Zt:t.keyedActors}function ee(t){t._snapshotVersion++}function se(t){return t._snapshotVersion}function ne(t,e,s,n){const r=Bt.get(t),o=r?.executionSystem??t,c=r?.rootSystem??t,a=void 0!==n&&Xt.get(o)!==n,u=!!n&&!!n.inspect,h=function(t,e){if(e){const t=new Map(e.children);return e.root?.sessionId&&t.set(e.root.sessionId,e.root),t}return new Map(t.children)}(t,s),d=new Map(s?.keyedActors??te(t)),f=new WeakMap,l=()=>Bt.get(y).executionSystem,p=()=>Bt.get(y).forwardInspection,y=Object.assign(Object.create(o),{children:h,keyedActors:d,reverseKeyedActors:f,_snapshot:{...s?.snapshot??t._snapshot},_snapshotVersion:s?.sourceVersion??t._snapshotVersion,_register:(t,e)=>(h.set(t,e),ee(y),t),_unregister:t=>{t.sessionId&&h.delete(t.sessionId);const e=f.get(t);void 0!==e&&d.get(e)===t&&d.delete(e),f.delete(t),ee(y)},_set:(t,e)=>{const s=d.get(t);if(s&&s!==e)throw new Error(i(45,String(t)));d.set(t,e),f.set(e,t),ee(y)},get:t=>d.get(t),getAll:()=>Object.fromEntries(d),onEmitted:t=>c.onEmitted.call(l(),t),_notifyEmitted:(t,e)=>c._notifyEmitted.call(l(),t,e),_hasInspectionObservers:()=>p()&&(c._hasInspectionObservers?.call(l())??!1),_sendInspectionEvent:t=>{p()&&c._sendInspectionEvent.call(l(),t)}}),g=a?y:o;if(a)!function(t,e,s){const n=e._timerMap;for(const e of Ft)Object.defineProperty(t,e,{configurable:!0,writable:!0,value:"runtime"===e?s.runtime:"_timerMap"===e&&n?{...n}:void 0});Xt.set(t,s),s.inspect&&t.inspect(s.inspect)}(y,o,n);else for(const t of Ft)Object.defineProperty(y,t,{configurable:!0,get:()=>l()[t],set:e=>{l()[t]=e}});Bt.set(y,{executionSystem:g,rootSystem:c,forwardInspection:u});for(const[t,e]of d)e&&f.set(e,t);for(const t of Object.values(e)){if(!t)continue;t.sessionId&&h.set(t.sessionId,t);const e=t.registryKey;e&&(d.set(e,t),f.set(t,e))}return y}function re(){const t=Gt;return Gt=void 0,t}function oe(t){const e=Qt.get(t);if(e)return e;const s=Yt.get(t);if(!s)return;const n=s();return Qt.set(t,n),Yt.delete(t),n}function ie(t,e){const s=Qt.get(t);if(s)return Qt.set(e,s),!0;const n=Yt.get(t);return!!n&&(Yt.set(e,n),!0)}function ce(t,e,s=e.system,n){Yt.delete(t);const r=se(s),o=n?oe(n):void 0,i=o?.actor===e?o:oe(t);if(i?.actor===e&&i.systemState.sourceSystem===s&&i.systemState.sourceVersion===r)return void Qt.set(t,i);const c=function(t){const e=t._getRootActor?.(),s=t._peekChildren?.();if(!e&&!s){if(t._getRootActor&&t._peekChildren)return{};const e=new Map(t.children);return e.size?{children:e}:{}}return s?.size?{children:new Map(t.children)}:e?{root:e}:{}}(s);let a=c.children;const u=te(s);let h=u.size?new Map(u):Zt;for(const e of Object.values(function(t){return"children"in t&&t.children?t.children:{}}(t))){if(!e)continue;e.sessionId&&(a??=new Map).set(e.sessionId,e);const t=e.registryKey;t&&(h===Zt&&(h=new Map),h.set(t,e))}Qt.set(t,{actor:e,systemState:{...c,children:a,keyedActors:h,snapshot:{...s._snapshot},sourceSystem:s,sourceVersion:r}})}const ae=Symbol();function ue(t){return!!t[ae]}function he(t){return ue(t)?t._parent:t.self._parent}function de(t,e){if(!ue(e)){const s=t,n=e.self;return s.self=n,s.system=e.system,s.parent=n._parent,s}return Object.defineProperties(t,{self:{enumerable:!0,get:()=>e.self},system:{enumerable:!0,get:()=>e.system},parent:{enumerable:!0,get:()=>he(e)}}),t}function fe(t,e,s=!0){const n=_e(t);if(n){n.snapshot=e;const t=n.materialized??n.identityScope;t&&(t.self._snapshot=e)}else t.self._snapshot=e;return s&&e&&"object"==typeof e&&ce(e,t.self),e}function le(t){return!!_e(t)||!!t.self.options?._inert}function pe(t,e){fe(t,e,!1);const s=_e(t);ve.set(e,t);const n=()=>(ce(e,t.self,t.system),oe(e));return s&&(s.materialized?s.identityProvider=n:s.identityProvider??=n),function(t,e){Qt.delete(t),Yt.set(t,e)}(e,n),e}function ye(t,e){return t.sourceRef=void 0,t.sourceChildren={},t.materialized=e,t.materialize=()=>e,e}const ge=Symbol(),ve=new WeakMap;function _e(t){return t[ge]}function me(t){return _e(t).materialize()}const xe={[ae]:!0,get _parent(){const t=_e(this);return t.parentKnown?t.parent:me(this).self._parent},get self(){return me(this).self},get defer(){return me(this).defer},get id(){return me(this).id},get logger(){return me(this).logger},get sessionId(){return me(this).sessionId},get stopChild(){return me(this).stopChild},get system(){return me(this).system},get emit(){return me(this).emit},get actionExecutor(){return me(this).actionExecutor}};function we(t,e,s,n,r,o){const i=e?.(),c=r??i?.actor,a=c?.system,u=c&&a?ne(a,s,r?void 0:i?.systemState,o):void 0,h=fn(t,{_inert:!0,...c?{id:c.id,_sessionId:c.sessionId}:{},...u?{_systemRef:{current:u}}:{}});c?._parent&&(h._parent=c._parent,h._address=void 0);const d=Object.create(h._actorScope,{defer:{value:()=>{}},stopChild:{value:t=>t._stop()},actionExecutor:{value:()=>{}},_snapshot:{enumerable:!0,configurable:!0,get:()=>h._snapshot,set:t=>{h._snapshot=t}}});return n&&(d._snapshot=n),d}function be(t,e,s,n){const r=n??(e&&"object"==typeof e?ve.get(e):void 0),o=r?_e(r):void 0,i=e&&"object"==typeof e?function(t){return Qt.get(t)}(e):void 0,c=o?.identityProvider??(e&&"object"==typeof e?function(t){const e=Qt.get(t);return e?()=>e:Yt.get(t)}(e):void 0),a={snapshot:e,sourceRef:c,sourceChildren:_s(e)?e.children:{},identityProvider:o?.identityProvider??c,parent:s?._parent??o?.parent??i?.actor._parent,owner:n?o?.owner:re(),parentKnown:!(!s&&!o?.parentKnown&&!i&&e),materialize:()=>{const e=we(t,a.sourceRef,a.sourceChildren,a.snapshot,s,a.owner);return ye(a,e)}};if(!a.identityProvider&&!e){const e={};let n;const r=()=>(a.identityScope||(a.identityScope=we(t,a.sourceRef,a.sourceChildren,void 0,s,a.owner),void 0!==a.snapshot&&(a.identityScope.self._snapshot=a.snapshot)),a.identityScope);a.materialize=()=>{const t=r();return void 0!==a.snapshot&&(t.self._snapshot=a.snapshot),ye(a,t)},a.identityProvider=()=>{if(n)return n;const t=r();return ce(e,t.self,t.system),n=oe(e)}}const u=Object.create(xe);return u[ge]=a,u}function Se(t,e){if("string"!=typeof t)return t;const s=e[t];if(void 0!==s)return s;const n=function(t){const e=t.trim(),s=e.match(/^(\d+)ms$/i);if(s)return parseInt(s[1],10);const n=e.match(/^(\d*)(\.?)(\d*)s$/i);if(n){const t=n[1]?parseInt(n[1],10):0,e=!!n[2],s=n[3]?parseInt(n[3].padEnd(3,"0").slice(0,3),10):0;return 1e3*t+(e?s:0)}const r=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(!r?.groups)return;const{weeks:o,days:i,hours:c,minutes:a,seconds:u}=r.groups;if(!(o||i||c||a||u))return;const h=t=>t?Number(t.replace(",",".")):0;return 7*h(o)*24*60*60*1e3+24*h(i)*60*60*1e3+60*h(c)*60*1e3+60*h(a)*1e3+1e3*h(u)}(t);if(void 0===n)throw new Error(i(9,t));return n}function Ie(t,e,s){if("function"==typeof t)return t(s);const n=Se(t,e);return"function"==typeof n?n(s):n}function Ae(t,e){return t._stateInputs?.[e]}function Ee(t){return"atomic"===t.type||"final"===t.type||"choice"===t.type}function ke(t){return Object.values(t.states).filter(t=>"history"!==t.type)}function Oe(t,e){const s=[];if(e===t)return s;let n=t.parent;for(;n&&n!==e;)s.push(n),n=n.parent;return s}function je(t){const e=new Set(t),s=new Set;for(const t of e)t.parent&&s.add(t.parent);for(const t of e)if("compound"!==t.type||s.has(t)){if("parallel"===t.type)for(const s of ke(t))if(!e.has(s)){const t=We(s);for(const s of t)e.add(s)}}else for(const s of We(t))e.add(s);for(const t of e){let s=t.parent;for(;s&&!e.has(s);)e.add(s),s=s.parent}return e}function Te(t,e){const s=je(e),n=new Map;for(const t of s)n.has(t)||n.set(t,[]),t.parent&&(n.has(t.parent)||n.set(t.parent,[]),n.get(t.parent).push(t));const r=t=>{const e=n.get(t);if(!e)return{};if("compound"===t.type){const t=e[0];if(!t)return{};if(Ee(t))return t.key}const s={};for(const t of e)s[t.key]=r(t);return s};return r(t)}function Me(t,e){return"compound"===e.type?ke(e).some(e=>"final"===e.type&&t.has(e)):"parallel"===e.type?ke(e).every(e=>Me(t,e)):"final"===e.type}const Re=t=>"#"===t[0];function Pe(t,e,s){const n=e.children[s];return!n||!("sessionId"in t)||(void 0===n.sessionId||n.sessionId===t.sessionId)}function $e(t,e,s,n){const r=t.entry;t.entry=(t,e)=>{const o="function"==typeof r?r(t,e):void 0,i=void 0!==o?.context?yt(t.context,o.context):t.context;return e.raise(s(t),{delay:n({context:i,event:t.event,delays:t.delays,input:t.input})}),o};const o=t.exit;t.exit=(t,s)=>(s.cancel({event:D(e)}),"function"==typeof o?o(t,s):void 0)}function Ke(t,e,s){const n=function(t){const{cond:e,actions:s,guard:n}=t;if(void 0!==e)return'"cond"';if(void 0!==s)return'"actions"';if(void 0!==n&&"function"!=typeof n)return'an object-form "guard"';return}(s);if(n)throw new Error(i(10,e||"always",t.id,n));const r=zs(s.target),o=s.reenter??!1,c=Le(t,r);ze(t,c);const a={...s,target:c,source:t,reenter:o,eventType:e,toJSON:()=>qe(a)};return a}function qe(t){const{definition:e,...s}=t;return{...s,source:`#${t.source.id}`,target:t.target?.map(t=>`#${t.id}`)}}function De(t,e){for(let s=t.parent;s;s=s.parent)if(s===e)return!0;return!1}function Ne(t,e){const s=new Set;for(let e=t;e;e=e.parent)s.add(e);for(let t=e;t;t=t.parent)if(s.has(t))return t}function ze(t,e){if(e&&!(e.length<2))for(let s=0;s<e.length;s++)for(let n=s+1;n<e.length;n++){const r=e[s],o=e[n],c=Ne(r,o);if(r===o||De(r,o)||De(o,r)||"parallel"!==c?.type)throw new Error(i(11,t.id))}}function Le(t,e){if(void 0!==e)return e.map(e=>{if("string"!=typeof e)return e;if(Re(e))return t.machine.getStateNodeById(e);const s="."===e[0];if(s&&!t.parent)return Je(t,e.slice(1));const n=s?t.key+e:e;if(!t.parent)throw new Error(i(13,e));try{return Je(t.parent,n)}catch(e){throw new Error(i(12,t.id,e.message))}})}function Ve(t){const e=zs(t.config.target);if(!e)return"parallel"===t.parent.type?{target:[t.parent],source:t,reenter:!1,eventType:""}:t.parent.initial;const s=e.map(e=>"string"==typeof e?Je(t.parent,e):e);return ze(t,s),{target:s,source:t,reenter:!1,eventType:"",to:t.config._historyDefaultTransition}}function Ce(t){return"history"===t.type}function We(t){const e=new Set;!function t(s){if(!e.has(s))if(e.add(s),"compound"===s.type)for(const e of s.initial.target??[])t(e);else if("parallel"===s.type)for(const e of ke(s))t(e)}(t);for(const s of e)for(const n of Oe(s,t))e.add(n);return e}function Ue(t,e){const s=t.states?.[e];if(!s)throw new Error(i(14,e,t.id));return s}function Je(t,e){if("string"==typeof e&&Re(e))return t.machine.getStateNodeById(e);const s=Ms(e).slice();let n=t;for(;s.length;){const t=s.shift();if(!t.length)break;n=Ue(n,t)}return n}function He(t,e){if("string"==typeof e){const s=t.states[e];if(!s)throw new Error(i(14,e,t.id));return[t,s]}const s=Object.keys(e),n=new Array(s.length),r=[t.machine.root,t];for(let e=0;e<s.length;e++){const o=Ue(t,s[e]);n[e]=o,r.push(o)}for(let t=0;t<s.length;t++)r.push(...He(n[t],e[s[t]]));return r}function Be(t,e,s,n,r,o){if("string"==typeof e){const i=Ue(t,e).next(s,n,r,o);return i&&i.length?i:t.next(s,n,r,o)}const i=Object.keys(e),c=i[0];if(1===i.length){const i=Be(Ue(t,c),e[c],s,n,r,o);return i&&i.length?i:t.next(s,n,r,o)}const a=[];for(const c of i){const i=e[c];if(!i)continue;const u=Be(Ue(t,c),i,s,n,r,o);u&&a.push(...u)}return a.length?a:t.next(s,n,r,o)}function Fe(t,e){let s=t;for(;s.parent&&s.parent!==e;)s=s.parent;return s.parent===e}function Xe(t,e){for(const s of t)if(Fe(s,e))return!0;return!1}function Ge(t,e){const s=t instanceof Set?t.size:Array.isArray(t)?t.length:void 0,n=e instanceof Set?e.size:Array.isArray(e)?e.length:void 0;void 0!==s&&void 0!==n&&n<s&&([t,e]=[e,t]);const r=t instanceof Set?t:new Set(t);for(const t of e)if(r.has(t))return!0;return!1}function Qe(t,e,s,n){const r=new Set,o=new Map,i=t=>{let r=o.get(t);return r||(r=ss([t],e,s,n),o.set(t,r)),r};for(const e of t){let t=!1;const s=new Set;for(const n of r)if(Ge(i(e),i(n))){if(!Fe(e.source,n.source)){t=!0;break}s.add(n)}if(!t){for(const t of s)r.delete(t);r.add(e)}}return Array.from(r)}function Ye(t,e,s,n,r){let o;return(i,c)=>{if(!i.to&&!c)return{targets:i.target,context:void 0,reenter:i.reenter,actions:void 0,internalEvents:void 0,input:void 0};let a=o?.get(i);return a||(a=rs(i,t,e,s,{resolveActions:n,selectionResult:r?.get(i)}),(o??=new Map).set(i,a)),a}}function Ze(t,e,s){const n=e.historyValue,{targets:r}=s(t);if(!r)return[];const o=new Set;for(const t of r)if(Ce(t))if(n[t.id])for(const e of n[t.id])o.add(e);else for(const n of Ze(Ve(t),e,s))o.add(n);else o.add(t);return[...o]}function ts(t,e){let s=t;for(;"parallel"===s.type;){const t=ke(s).find(t=>e.every(e=>e===t||Fe(e,t)));if(!t)break;s=t}return s}function es(t,e,s){const n=Ze(t,e,s),{targets:r,reenter:o}=s(t);if(t._transitionDomain){if("internal"===t._transitionDomain&&"compound"===t.source.type&&n.every(e=>Fe(e,t.source)))return t.source;const[e,...s]=n.concat(t.source);for(const t of Oe(e,void 0))if("compound"===t.type&&s.every(e=>Fe(e,t)))return t;return}if(!(r?.some(Ce)&&n.some(e=>e===t.source))&&n.every(e=>e===t.source||Fe(e,t.source)))return o?t.source:ts(t.source,n);const[i,...c]=n.concat(t.source);for(const t of Oe(i,void 0))if(c.every(e=>Fe(e,t)))return o?t:ts(t,n);return o?void 0:ts(t.source.machine.root,n)}function ss(t,e,s,n){const r=new Set;for(const o of t){const{targets:t,reenter:i}=n(o);if(t?.length){const t=es(o,s,n);i&&o.source===t&&r.add(t);for(const s of e)Fe(s,t)&&r.add(s)}}return[...r]}function ns(t,e,s,n,r,o,c){const a=[],u=e.machine._microstepHooks?.drain(s.self);if(u?.length&&o.push(...u),!t.length)return[e,a];{const u=new Set(e.nodes);let h=e.historyValue;const d=e.context,f=1===t.length?t:Qe(t,u,e,Ye(e,n,s,!1,c)),l=Ye(e,n,s,!0,c),p=f.some(t=>{const{targets:e,reenter:s}=l(t);return!!e?.length||!!s}),y=(t,r,o,i,c,a)=>{const u=[],h=[];let d;const f=Mt(s,u,h,!0),l=ue(s)?de({context:r,event:n,output:qs(n),children:o,actions:e.machine.sources.actions,actors:e.machine.sources.actors,guards:e.machine.sources.guards,delays:e.machine.sources.delays,input:i,stateNode:a},s):{context:r,event:n,output:qs(n),parent:s.self._parent,self:s.self,children:o,system:s.system,actions:e.machine.sources.actions,actors:e.machine.sources.actors,guards:e.machine.sources.guards,delays:e.machine.sources.delays,input:i,stateNode:a};let p;try{p=t(l,f)}finally{Pt(f)}return as(p,n,a.id,c),void 0!==p?.context&&(d=yt(r,p.context)),[u,d,h,p?.children]};let g=e;const v=(t,e)=>{const n=[],r=Mt(s,n,[]);for(const s of t.invoke){const t=e[s.id];t&&r.stop(t)}return n},_=()=>{const t=ss(f,u,e,l);let n;t.sort((t,e)=>e.order-t.order);const r=[...u];for(const e of t)for(const t of Object.values(e.states)){if("history"!==t.type)continue;const s="deep"===t.history?t=>Ee(t)&&Fe(t,e):t=>t.parent===e;n??={...h},n[t.id]=r.filter(s)}for(const n of t){const t=Ae(e,n.id),[r,i,c,h]=n.exit?y(n.exit,g.context,g.children,t,"exit",n):[[],void 0,void 0,void 0];c?.length&&o.push(...c),(i||h)&&(g=ks(g,{...i?{context:i}:{},...h?{children:h}:{}}));const[d,f]=Vt(g,0,s,r);g=d,a.push(...f);const l=v(n,g.children);if(l.length){const[t,e]=Vt(g,0,s,l);g=t,a.push(...e)}u.delete(n)}h=n||h};!r&&p&&_();let m=g.context;const x=[],w=[],b=[];for(const t of f){const e=l(t,!0);if(t.to){const s={...t,target:e.targets,definition:t};s.toJSON=()=>qe(s),b.push(s)}else b.push(t);void 0!==e.context&&(m=yt(m,e.context)),e.actions&&x.push(...e.actions),e.internalEvents&&w.push(...e.internalEvents)}w.length&&o.push(...w);const S=()=>{const t=t=>{const e=g.machine.root;let r;if(void 0!==t.output&&t.parent){const i=o.find(t=>"xstate.done.state"===t.type&&t.stateId===e.id);r=i&&t.parent===e?i.output:Ks(t.output,g.context,n,s.self,x[t.id])}else if("parallel"===t.type){const e=o.find(e=>"xstate.done.state"===e.type&&e.stateId===t.id);r=e?.output}return void 0===e.output?t.parent===e?r:void 0:Ks(e.output,g.context,j(t.id,r),s.self)},c=new Set,d=new Set,p=new Map,v=(t,e)=>{for(const s of t)if(e&&!Fe(s,e)||c.add(s),"parallel"===s.type)for(const t of ke(s))Xe(c,t)||(c.add(t),_(t))},_=t=>{if(Ce(t))if(h[t.id]){const e=h[t.id];for(const t of e)c.add(t),_(t);for(const s of e)v(Oe(s,t.parent),void 0)}else{const e=Ve(t),s=t.parent;d.add(s);const n=p.get(s)??[];n.push(e),p.set(s,n);const{targets:r}=l(e);for(const s of r??[])c.add(s),e===t.parent?.initial&&d.add(t.parent),_(s);for(const e of r??[])v(Oe(e,t.parent),void 0)}else{if("compound"===t.type){const e=l(t.initial).targets??[];for(const t of e)Ce(t)||c.add(t);d.add(t);for(const t of e)_(t);for(const s of e)v(Oe(s,t),void 0);return}if("parallel"===t.type)for(const e of ke(t))Xe(c,e)||(c.add(e),d.add(e),_(e))}},m=new Map;for(const t of f){const s=es(t,e,l),{targets:n,reenter:r}=l(t),o=new Set;m.set(t,o);for(const e of n??[])Ce(e)||t.source===e&&t.source===s&&!r||(o.add(e),c.add(e),d.add(e)),_(e);const i=Ze(t,e,l);for(const e of i){const n=Oe(e,s);"parallel"===s?.type&&n.push(s),v(n,!t.source.parent&&r?void 0:s)}r&&s===t.source&&c.add(t.source)}if(r)for(const t of f[0]._statesForDefaultEntry??[e.machine.root])d.add(t);const x={};let w=!1;const b=new Set([...u].map(t=>t.id));for(const[t,s]of Object.entries(e._stateInputs??{}))b.has(t)?x[t]=s:w=!0;for(const t of f){const{targets:e,input:s}=l(t,!0);if(s&&e)for(const n of e.filter(e=>m.get(t)?.has(e)))x[n.id]=s,w=!0}const S=new Set;for(const r of[...c].sort((t,e)=>t.order-e.order)){u.add(r);const c=[];let h=g.children,f=!1;for(const t of"final"===r.type?[]:r.invoke){f=!0;let o=t.logic;"function"==typeof o&&(o=o({actors:e.machine.sources.actors,context:g.context,event:n,self:s.self}));const a="string"==typeof o?e.machine.sources.actors[o]:o;if(!a)throw new Error(i(15,"string"==typeof o?o:"inline"));const u="function"==typeof t.input?t.input({self:s.self,context:g.context,event:n,input:x[r.id],output:qs(n)}):t.input;kt(s,t.id);const d=s.system.createActorRef(a,{...t,src:"string"==typeof o?o:void 0,input:u,parent:s.self,syncSnapshot:!!t.onSnapshot});d._invoked=!0,c.push({action:J["@xstate.spawn"],args:[d]}),t.id&&(h={...h,[t.id]:d})}f&&(g=ks(g,{children:h}));let l=g.context,v=!1;const _=x[r.id];if(r.entry){const[t,e,s,n]=y(r.entry,l,h,_,"entry",r);c.push(...t),s?.length&&o.push(...s),e&&(l=e,v=!0),n&&(g=ks(g,{children:n}))}if(v&&(g=ks(g,{context:l})),d.has(r)){const t=[r.initial,...p.get(r)??[]].filter(Boolean);for(const e of t){const{actions:t,context:r,input:i,internalEvents:a}=rs(e,g,n,s);if(t&&c.push(...t),a?.length&&o.push(...a),void 0!==r&&(g=ks(g,{context:yt(g.context,r)})),i&&e.target)for(const t of e.target)x[t.id]=i,w=!0}}const[m,b]=Vt(g,0,s,c);if(g=m,c.length=0,a.push(...b),"final"!==r.type)continue;const I=r.parent;let A="parallel"===I?.type?I:I?.parent,E=A||r;for("compound"===I?.type&&o.push(j(I.id,void 0!==r.output?Ks(r.output,g.context,n,s.self,_):void 0));"parallel"===A?.type&&!S.has(A)&&Me(u,A);){S.add(A);const t={};for(const e of ke(A)){if("final"===e.type){t[e.key]=void 0!==e.output?Ks(e.output,g.context,n,s.self,x[e.id]):void 0;continue}if("parallel"===e.type){const s=o.find(t=>"xstate.done.state"===t.type&&t.stateId===e.id);t[e.key]=s?.output;continue}const r=ke(e).find(t=>"final"===t.type&&u.has(t));t[e.key]=void 0!==r?.output?Ks(r.output,g.context,n,s.self,x[r.id]):void 0}o.push(j(A.id,t)),E=A,A=A.parent}A||(g=ks(g,{status:"done",output:t(E)}))}w&&(g=ks(g,{_stateInputs:x}))},[I,A]=Vt(g,0,s,x);g=I,a.push(...A),m&&m!==e.context&&(g=ks(g,{context:m}));const E=g.machine._microstepHooks?.transitionContent?.(f,[...u]);if(E){const t=f[0].source,[e,n,r,i]=y(E,g.context,g.children,Ae(g,t.id),"transition",t);r?.length&&o.push(...r),(n||i)&&(g=ks(g,{...n?{context:n}:{},...i?{children:i}:{}}));const[c,u]=Vt(g,0,s,e);g=c,a.push(...u)}(r||p)&&S();const k=[...u];if("done"===g.status){const t=[];k.sort((t,e)=>e.order-t.order).forEach(e=>{if(e.exit){const s=Ae(g,e.id),[n,r,i,c]=y(e.exit,g.context,g.children,s,"exit",e);(r||c)&&(g=ks(g,{...r?{context:r}:{},...c?{children:c}:{}})),t.push(...n),i?.length&&o.push(...i)}});const[e,r]=Vt(g,0,s,t);g=e,a.push(...r);const[i,c]=hs(g,n,s);g=i,a.push(...c);const[u,h]=ds(g,n,s);g=u,a.push(...h)}return h===e.historyValue&&e.nodes.length===u.size&&e.nodes.every(t=>u.has(t))?g.context!==d?[g===e?ks(g):g,a,b]:[g,a,b]:[ks(g,{_nodes:k,historyValue:h}),a,b]}}function rs(t,e,s,n,r){let o;const i=()=>o??=ue(n)?de({context:e.context,event:s,output:qs(s),value:e.value,children:e.children,actions:e.machine.sources.actions,actors:e.machine.sources.actors,guards:e.machine.sources.guards,delays:e.machine.sources.delays,input:Ae(e,t.source.id)},n):{context:e.context,event:s,output:qs(s),value:e.value,children:e.children,system:n.system,parent:n.self._parent,self:n.self,actions:e.machine.sources.actions,actors:e.machine.sources.actors,guards:e.machine.sources.guards,delays:e.machine.sources.delays,input:Ae(e,t.source.id)};if(t.to){const o=[],c=[];let a;if(!r?.selectionResult?.reusable||!1!==r.resolveActions&&0!==r.selectionResult.operations.length){const e=Mt(n,o,c,!0,r?.resolveActions??!0),u=!1===r?.resolveActions?q(he(n),"suppress"):void 0;try{if(r?.selectionResult?.reusable){a=r.selectionResult.result;for(const t of r.selectionResult.operations)t(e)}else a=t.to(i(),e),as(a,s,t.source.id)}finally{u?.close(),Pt(e)}}else a=r.selectionResult.result;const u=a?.target?Le(t.source,$s(a.target)):void 0,h=a?.input??("function"==typeof t.input?t.input({context:e.context,event:s,output:qs(s)}):t.input);return{targets:u,context:a?.context,reenter:a?.reenter,actions:o,internalEvents:c,input:h}}const c="function"==typeof t.input?t.input({context:e.context,event:s,output:qs(s)}):t.input,a="function"==typeof t.context?t.context(i()):t.context;return{targets:t.target,context:a,reenter:t.reenter,actions:void 0,internalEvents:void 0,input:c}}class os extends Error{constructor(t,e,s,n){super(i(16,t,e.type,s)),this.actorId=void 0,this.event=void 0,this.maxIterations=void 0,this.states=void 0,this.name="InfiniteTransitionError",this.actorId=t,this.event=e,this.maxIterations=s,this.states=n.slice()}}function is(t,e,s,n,r=[]){if("active"!==t.status&&!r.length)return{snapshot:t,microsteps:[[t,[],[]]]};let o=t;const i=r.slice();let c=!1;function a(t){if("xstate.done.actor"!==t.type&&"xstate.error.actor"!==t.type)return;const{actorId:e,sessionId:n}=t;if(!e)return;const r=o.children[e];if(!r)return;if(void 0!==r.sessionId&&r.sessionId!==n)return;const c={...o.children};if(delete c[e],s.system._unregister(r),o=ks(o,{children:c}),i.length){const[,t,e]=i.at(-1);i[i.length-1]=[o,t,e]}else i.push([o,[],[]])}function f(){if(c&&o===t&&(o=ks(t,{}),i.at(-1)?.[0]===t)){const[,t,e]=i.at(-1);i[i.length-1]=[o,t,e]}const e=function(t){const e=[],s=[];for(const n of t){if(!Lt(n)||n.type!==h)continue;const{actor:t,logic:r}=n,o=qt(t);r===Z||r===et?e.push(o):s.push(o)}return[...e,...s]}(i.flatMap(([,t])=>t)),n=("active"===t.status||r.length>0)&&("done"===o.status||"error"===o.status);if(e.length||n){const t=n?[...e,Nt(s,o)]:e;if(i.length){const[e,s,n]=i.at(-1);i[i.length-1]=[e,[...s,...t],n]}else i.push([o,t,[]])}return{snapshot:o,microsteps:i}}function p(t,n){if(n=t[2]??n,!le(s)&&(e.type===u||(s.system._hasInspectionObservers?.()??1))){const t=s.self._collectedMicrosteps||[];t.push(...n),s.self._collectedMicrosteps=t}i.push([t[0],t[1],n])}if(e.type===d){const[t,n]=hs(o,e,s),[r,i]=ds(t,e,s);return o=ks(r,{status:"stopped"}),p([o,[...n,...i]],[]),f()}let y=e;if(e.type===l){const t=z(e),r=t?o.timers.findIndex(e=>L(e,t)):-1;if(-1===r)return f();const i=o.timers[r],c=o.timers.filter((t,e)=>e!==r);if(o=ks(o,{timers:c}),"@xstate.raise"===i.type)n.push(i.event),p([o,[]],[]);else{const t="self"===i.target?s.self:i.target;p([o,[Dt(s,t,i.event)]],[])}}if(y.type!==u&&y.type!==l){const e=y,r=function(t){return t.type.startsWith("xstate.error.")}(e),i=new Map,u=o.machine.getTransitionData(o,e,s,i);if(r&&!u.length)return"xstate.error.actor"!==e.type||Pe(e,o,e.actorId)?(o=ks(t,{status:"error",error:e.error}),p([o,[]],[]),a(e),f()):f();c=u.length>0;const h=ns(u,t,s,y,!1,n,i);o=h[0],p(h,u),a(e)}if(!n.length&&!1===t.machine._hasEventlessTransitions)return f();let g=!0;const v=t.machine.options?.maxIterations??1/0;let _=0;const m=[];for(;"active"===o.status;){if(_++,_>v)throw new os(s.self?.id,e,v,m);let t;m.push(o.value),m.length>5&&m.shift();let r=g?fs(o,y,s):[];const i=r.length?o:void 0,c=o.machine._microstepHooks?.drain(s.self);if(c?.length&&n.push(...c),!r.length){if(!n.length)break;y=n.shift(),t=new Map,r=o.machine.getTransitionData(o,y,s,t)}const u=ns(r,o,s,y,!1,n,t);o=u[0],g=o!==i,p(u,r),a(y)}return f()}function cs(t,e,s,n,r){return!!t.to&&us(t.to,e,s,n,r,n.machine.sources,t.source.id).enabled}function as(t,e,s,n){if(t&&"function"==typeof t.then)throw Promise.resolve(t).catch(()=>{}),new Error(i(17,n??"transition",s,e.type))}function us(t,e,s,n,r,o,i){let c;const a=function(t){const e=q(t),{frame:s}=e,n=[];let r=!1;const o=t=>{r||n.push(t)},i=()=>{if(!r)throw s.requiresReevaluation=!0,K},c=Ct({cancel:(...t)=>o(e=>e.cancel(...t)),emit:(...t)=>o(e=>e.emit(...t)),log:(...t)=>o(e=>e.log(...t)),raise:(t,e)=>{r||(Tt(t),o(s=>s.raise(t,e)))},sendTo:(...t)=>o(e=>e.sendTo(...t)),stop:(...t)=>o(e=>e.stop(...t)),spawn:i,listen:i,subscribeTo:i},(...t)=>o(e=>e(...t)));return{frame:s,enqueue:c,operations:n,close(){r=!0,e.close()}}}(he(r));try{c=t(de({context:e,event:s,output:qs(s),value:n.value,children:n.children,actions:o.actions,actors:o.actors,guards:o.guards,delays:o.delays,input:Ae(n,i)},r),a.enqueue)}catch(t){if(t===K)return{enabled:!0,result:void 0,reusable:!1};throw t}finally{a.close()}return as(c,s,i),a.frame.requiresReevaluation?{enabled:!0,result:void 0,reusable:!1}:{enabled:void 0!==c||a.operations.length>0,result:c,reusable:!0,operations:a.operations}}function hs(t,e,s){let n;if(!t.children||0===(n=Object.values(t.children).filter(Boolean)).length)return[t,[]];const r=[],o=Mt(s,r,[]);for(const t of n)o.stop(t);return Vt(t,0,s,r)}function ds(t,e,s){if(!t.timers.length)return[t,[]];const n=[],r=Mt(s,n,[]);for(const e of t.timers)r.cancel(N(e));return Vt(t,0,s,n)}function fs(t,e,s){t.machine._microstepHooks?.begin(s.self);const n=new Set,r=t.nodes.filter(Ee);for(const o of r)t:for(let r=o;r;r=r.parent)if(r.always)for(const o of r.always)if(ls(o,e,t,r,s)){n.add(o);break t}return Qe(Array.from(n),new Set(t.nodes),t,Ye(t,e,s,!1))}function ls(t,e,s,n,r,o){if(t.matches&&!function(t,e){return Object.entries(e).every(([e,s])=>Object.is(t[e],s))}(e,t.matches))return!1;if(t._eventMatcher&&!t._eventMatcher(e,s))return!1;if(t.guard){const o=function(t,e){if(!ue(e)){const s=e.self;return Object.assign(t,{self:s,parent:s._parent})}return Object.defineProperties(t,{self:{enumerable:!0,get:()=>e.self},parent:{enumerable:!0,get:()=>he(e)}}),t}({context:s.context,event:e,output:qs(e),children:s.children,actions:n.machine.sources.actions,actors:n.machine.sources.actors,guards:n.machine.sources.guards,delays:n.machine.sources.delays,_snapshot:s},r);if(!t.guard(o))return!1}if(t.to){const i=us(t.to,s.context,e,s,r,n.machine.sources,t.source.id);return o?.set(t,i),i.enabled}return!0}const ps=Object.freeze({}),ys=Object.freeze([]);function gs(t){return t?.length?t:ys}function vs(t){return t===ps||t&&Object.keys(t).length?t:ps}function _s(t){return!!t&&"object"==typeof t&&"machine"in t&&"value"in t}const ms=function(t){if("string"==typeof t&&Re(t)){const e=this.machine.getStateNodeById(t);return this.nodes.includes(e)}return Ts(t,this.value)},xs=function(t){return this.tags.has(t)},ws=function(t){return this.machine?._canTransition(this,t)??!1},bs=function(){const{nodes:t,_stateInputs:e,_nextTimerId:s,_nextActorIds:n,tags:r,machine:o,getMeta:i,getInputs:c,toJSON:a,can:u,hasTag:h,matches:d,...f}=this;return{...f,tags:Array.from(r)}},Ss=function(){const t={};for(const e of this.nodes)void 0!==e.meta&&(t[e.id]=e.meta);return t},Is=function(){return this._stateInputs};function As(t){const e=new Set;for(const s of t)for(const t of s.tags)e.add(t);return e}function Es(t,e,s){const n={status:t.status,output:t.output,error:t.error,machine:e,context:t.context,nodes:t._nodes,value:t.value??Te(e.root,t._nodes),tags:As(t._nodes),children:vs(t.children),timers:gs(t.timers),historyValue:vs(t.historyValue),_stateInputs:vs(t._stateInputs),_nextTimerId:t._nextTimerId??0,_nextActorIds:t._nextActorIds,matches:ms,hasTag:xs,can:ws,getMeta:Ss,getInputs:Is,toJSON:bs};return s&&ce(n,s),n}function ks(t,e={}){const s={...t,...e};if((e._nodes??t.nodes)===t.nodes){const e={status:s.status,output:s.output,error:s.error,machine:t.machine,context:s.context,nodes:t.nodes,value:t.value,tags:t.tags,children:vs(s.children),timers:gs(s.timers),historyValue:vs(s.historyValue),_stateInputs:vs(s._stateInputs),_nextTimerId:s._nextTimerId??0,_nextActorIds:s._nextActorIds,matches:ms,hasTag:xs,can:ws,getMeta:Ss,getInputs:Is,toJSON:bs};return ie(t,e),e}const n=Es({...s,value:void 0},t.machine);return ie(t,n),n}function Os(t){const e={};for(const s in t){const n=t[s];Array.isArray(n)&&(e[s]=n.map(t=>({id:t.id})))}return e}function js(t,e,s="context",n=[]){let r;n.push(t);for(const o in t){const c=t[o];if(c&&"object"==typeof c)if(Vs(c))r??=Array.isArray(t)?t.slice():{...t},r[o]={"@actor":c.address};else{const a=Array.isArray(t)?`${s}[${o}]`:`${s}.${o}`;if(n.includes(c))throw new Error(i(44,e(),a));const u=js(c,e,a,n);u!==c&&(r??=Array.isArray(t)?t.slice():{...t},r[o]=u)}}return n.pop(),r??t}function Ts(t,e){const s=Rs(t),n=Rs(e);return"string"==typeof n?"string"==typeof s&&n===s:"string"==typeof s?s in n:Object.keys(s).every(t=>t in n&&Ts(s[t],n[t]))}function Ms(t){if(Ds(t))return t;const e=[];let s="";for(let n=0;n<t.length;n++){switch(t.charCodeAt(n)){case 92:s+="."===t[n+1]?"\\.":t[n+1],n++;continue;case 46:e.push(s),s="";continue}s+=t[n]}return e.push(s),e}function Rs(t){if(_s(t))return t.value;if("string"!=typeof t)return t;return function(t){if(1===t.length)return t[0];const e={};let s=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)s[t[e]]=t[e+1];else{const n=s;s={},n[t[e]]=s}return e}(Ms(t))}function Ps(t){return Ds(t)?t:[t]}function $s(t){return void 0===t?[]:Ps(t)}function Ks(t,e,s,n,r){if("function"==typeof t){return t({context:e,event:s,output:qs(s),self:n,input:r})}return t}function qs(t){if(function(t){return"xstate.done.actor"===t.type||"xstate.done.state"===t.type}(t)){return t.output}}function Ds(t){return Array.isArray(t)}function Ns(t,e,s){return Ps(t).map(t=>{if("string"==typeof t)throw new Error(i(38,e,s,t));return void 0===t?{target:void 0}:"function"==typeof t?{to:t}:t})}function zs(t){if(void 0!==t&&""!==t)return $s(t)}function Ls(t,e,s){const n="object"==typeof t,r=n?t:void 0;return{next:(n?t.next:t)?.bind(r),error:(n?t.error:e)?.bind(r),complete:(n?t.complete:s)?.bind(r),...n&&t.passive?{passive:!0}:{}}}function Vs(t){return"sessionId"in t&&"send"in t&&"ref"in t}function Cs(t,e,s){const n=[];for(const t of e)for(const e of t.invoke)e.id===s&&n.push(e);if(1!==n.length)throw new Error(i(39,s));const r=n[0].logic;if("string"==typeof r){const e=t.sources.actors[r];if(!e)throw new Error(i(29,s,r));return e}if("function"==typeof r)throw new Error(i(40,s));return r}function Ws(t,e){if(e===t)return!0;if("*"===e)return!0;if(!e.endsWith(".*"))return!1;const s=e.split("."),n=t.split(".");for(let t=0;t<s.length;t++){const e=s[t],r=n[t];if("*"===e){return t===s.length-1}if(e!==r)return!1}return!0}let Us,Js=0;function Hs(){return Us??=function(){let t;try{t=globalThis.crypto}catch{}if(t?.getRandomValues)try{const e=new Uint32Array(4);return t.getRandomValues(e),Array.from(e,t=>t.toString(36).padStart(7,"0")).join("")}catch{}if(t?.randomUUID)try{return t.randomUUID().replaceAll("-","")}catch{}return`xstate-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`}(),`${Us}:${(Js++).toString(36)}`}function Bs(t){if("string"==typeof t)return t;const e=t?.id;return"string"==typeof e&&e.length?e:"x"}function Fs(t){return t.includes("/")||t.includes("%")?t.replaceAll("%","%25").replaceAll("/","%2F"):t}function Xs(t){const e=t.lastIndexOf(":");if(e<=0||e===t.length-1)return;const s=Number(t.slice(e+1));return Number.isSafeInteger(s)&&s>=0?{prefix:t.slice(0,e),index:s}:void 0}function Gs(t,e){return`${t?t.address:""}|${e}`}function Qs(t,e,s){const n=t._snapshot._nextActorIds;(n[e]??0)>=s||(t._snapshot._nextActorIds={...n,[e]:s},ee(t))}const Ys=Object.freeze({});function Zs(t,e){return V(t,e)}function tn(t,e){return t.getSnapshot()?.timers?.find(t=>L(t,e))}class en{get children(){const t=this._children??=new Map;return this._getRootActor()&&t.set(this._rootActor.sessionId,this._rootActor),t}set children(t){this._children=t}_getRootActor(){return this._rootActor._isRunning()?this._rootActor:void 0}_peekChildren(){return this._children}get keyedActors(){return this._keyedActors??=new Map}set keyedActors(t){this._keyedActors=t}get reverseKeyedActors(){return this._reverseKeyedActors??=new WeakMap}set reverseKeyedActors(t){this._reverseKeyedActors=t}_peekKeyedActors(){return this._keyedActors}constructor(t,e){this._rootActor=t,this._identity={executionId:Hs(),nextSessionId:0},this._snapshot=void 0,this._snapshotVersion=0,this.scheduler=this,this._clock=void 0,this._logger=void 0,this.createActorRef=void 0,this._clock=e.clock,this._logger=e.logger,this.createActorRef=e.createActorRef;const s=Ht();s&&this.inspect(s),this._snapshot={_scheduledTimers:Ys,_nextActorIds:{}}}_recordSent(t,e,s,n,r){if(!this._inspectionObservers?.size||!t)return;const o=t;if(!o._inTransition)return;(o._collectedSent??=[]).push({targetRef:e,targetId:e.id,event:s,delay:n,id:r})}schedule(t,e,s){const n=Zs(t,e);void 0!==this._timerMap?.[n]&&this.cancel(t,e);const r=tn(t,e);if(r){const e="self"===r.target?t:r.target;this._recordSent(t,e,r.event,s,r.id)}const o=this._clock.now?.()??Date.now(),i={source:t,delay:s,key:e,scheduledAt:o,dueAt:o+s};this._snapshot._scheduledTimers===Ys&&(this._snapshot._scheduledTimers={}),this._snapshot._scheduledTimers[n]=i,ee(this);const c=this._clock.setTimeout(()=>{this._timerMap&&delete this._timerMap[n],delete this._snapshot._scheduledTimers[n],ee(this),this._deliver(t,t,{type:l,...e})},s);(this._timerMap??={})[n]=c}cancel(t,e){const s=Zs(t,e),n=this._timerMap?.[s];this._timerMap&&delete this._timerMap[s],this._snapshot._scheduledTimers!==Ys&&delete this._snapshot._scheduledTimers[s],ee(this),void 0!==n&&this._clock.clearTimeout(n)}cancelAll(t){for(const e in this._snapshot._scheduledTimers){const s=this._snapshot._scheduledTimers[e];s.source===t&&this.cancel(t,s.key)}}_deliver(t,e,s){!function(t,e,s){if(g(e)){const n=e,r=t?.system,o=r&&m(r,n);if(!o){const e=_(n);if(!r)throw e;return void r.deadLetter(t,void 0,s,"missingTarget",{targetId:v(n["@actor"]),error:e})}e=o}if(A(t,e,s))return;e._send(s,t)}(t,e,s)}_register(t,e){return e===this._rootActor?this._children?.set(t,e):(this._children??=new Map).set(t,e),ee(this),t}_getByAddress(t){const e=this._getRootActor();if(e?.address===t)return e;for(const e of this._children?.values()??[])if(e.address===t&&e._isRunning())return e}_unregister(t){let e;t===this._rootActor?(e=void 0!==this._getRootActor(),e=void 0!==t.sessionId&&(this._children?.delete(t.sessionId)??!1)||e):e=void 0!==t.sessionId&&(this._children?.delete(t.sessionId)??!1);const s=this._reverseKeyedActors?.get(t);void 0!==s&&(this._keyedActors?.get(s)===t&&(this._keyedActors.delete(s),e=!0),this._reverseKeyedActors?.delete(t)),e&&ee(this)}get(t){return this._keyedActors?.get(t)}getAll(){return Object.fromEntries(this._keyedActors?.entries()??[])}_set(t,e){const s=this._keyedActors?.get(t);if(s&&s!==e)throw new Error(i(45,String(t)));(this._keyedActors??=new Map).set(t,e),(this._reverseKeyedActors??=new WeakMap).set(e,t),s!==e&&ee(this)}inspect(t){const e=Ls(t);return(this._inspectionObservers??=new Set).add(e),{unsubscribe:()=>{this._inspectionObservers?.delete(e)}}}onRejectedEvent(t){const e=e=>t(e);return(this._rejectionListeners??=new Set).add(e),{unsubscribe:()=>{this._rejectionListeners?.delete(e)}}}onEmitted(t){const e=e=>t(e);return(this._emissionListeners??=new Set).add(e),{unsubscribe:()=>{this._emissionListeners?.delete(e)}}}_notifyEmitted(t,e){const s=this._emissionListeners;if(!s?.size)return;const n={actorRef:t,event:e};for(const t of Array.from(s))try{t(n)}catch(t){R(t)}}_hasInspectionObservers(){return!!this._inspectionObservers?.size}_sendInspectionEvent(t){if(!this._inspectionObservers?.size)return;const e={...t,rootId:this._rootActor.sessionId};this._inspectionObservers.forEach(t=>{try{t.next?.(e)}catch(t){R(t)}})}spawnActor(t,e){return this.runtime?.spawnActor?.(t,e)}startActor(t){const e=this.runtime?.startActor;if(e)return e(t);t.start()}stopActor(t){const e=this.runtime?.stopActor;if(e)return e(t);!function(t){t._stop()}(t)}terminateActor(t,e){const s=this.runtime?.terminateActor;if(s)return s(t,e);!function(t,e){t._terminate(e)}(t,e)}sendEvent(t,e,s){if(g(e)){const n=m(this,e);if(n)e=n;else if(this._getByAddress(e["@actor"])||!this.runtime?.sendEvent)return this.deadLetter(t,void 0,s,"missingTarget",{targetId:v(e["@actor"]),error:_(e)})}if(A(t,e,s))return;this._recordSent(t,e,s);const n=this.runtime?.sendEvent;if(n)return n(t,e,s);this._deliver(t,e,s)}emitEvent(t,e){const s=this.runtime?.emitEvent;if(s)return s(t,e);t._emit(e)}deadLetter(t,e,s,n,r){const o=this._rejectionListeners;if(o?.size){const i={event:s,targetRef:e,targetId:e?.id??r?.targetId,sourceRef:t,eventOrigin:t?"actor":"external",reason:n,issues:r?.issues,error:r?.error};for(const t of[...o])try{t(i)}catch(t){R(t)}}const i=this.runtime?.deadLetter;if(i)return i(t,e,s,n,r)}runStep(t,e,s){const n=this.runtime?.runStep;return n?n(t,e,s):I(t,e,s)}runLogic(t,e){const s=this.runtime?.runLogic;return s?s(t,e):e()}scheduleTimer(t,e,s){const n=this.runtime?.scheduleTimer;if(n){const r=tn(t,e);if(r){const e="self"===r.target?t:r.target;this._recordSent(t,e,r.event,s,r.id)}return n(t,e,s)}this.schedule(t,e,s)}cancelTimer(t,e){const s=this.runtime?.cancelTimer;if(s)return s(t,e);this.cancel(t,e)}cancelAllTimers(t){const e=this.runtime?.cancelAllTimers;if(e)return e(t);this.cancelAll(t)}_relay(t,e,s){if(!function(t){let e=!1,s=!1;for(const n of $)n.parent===t&&(n.requiresReevaluation=!0,e=!0,s||="abort"===n.mode);if(s)throw K;return e}(e))return this.sendEvent(t,e,s)}getSnapshot(){return{_scheduledTimers:{...this._snapshot._scheduledTimers}}}start(){const t=this._snapshot._scheduledTimers;let e=!0;for(const s in t){e&&(this._snapshot._scheduledTimers={},e=!1);const{source:n,dueAt:r,key:o}=t[s];this.scheduleTimer(n,o,Math.max(0,r-(this._clock.now?.()??Date.now())))}}}let sn=!1;const nn=Object.freeze([]),rn={"xstate.done.actor":!0,"xstate.error.actor":!0,"xstate.error.execution":!0,"xstate.snapshot.actor":!0,"xstate.done.state":!0,"xstate.after":!0,"xstate.timeout.state":!0,"xstate.timeout.actor":!0,[l]:!0};let on=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const cn=Object.freeze({clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console)});function an(t,e){try{t?.(e)}catch(t){R(t)}}function un(t,e){if(t?.length)for(const s of t)e.actionExecutor(s)}function hn(t,e){return new dn(t,e)}class dn{get address(){return this._address??=this._parent?`${this._parent.address}/${Fs(this.id)}`:Fs(this.id)}get trigger(){return this._trigger??=new Proxy({},{get:(t,e)=>t=>{this.send({...t,type:e})}})}get send(){return this._boundSend??=this._sendPublic.bind(this)}constructor(t,e){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this._boundProcess=void 0,this.mailbox=void 0,this._mailboxStarted=!1,this.observers=void 0,this._errorObserved=!1,this.eventListeners=void 0,this.logger=void 0,this._processingStatus=on.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=void 0,this._collectedActions=void 0,this._collectedSent=void 0,this._inTransition=!1,this._initialEffects=void 0,this.registryKey=void 0,this.sessionId=void 0,this._address=void 0,this.system=void 0,this._trigger=void 0,this._boundSend=void 0,this.src=void 0,this._deferred=void 0,this._restored=!1,this._warnedUnhandledTypes=void 0;const s=e?{...cn,...e}:cn,{clock:n,logger:r,parent:o,syncSnapshot:i,id:c,registryKey:a,inspect:u}=s;var h;this.system=o?o.system:s._systemRef?.current??function(t,e){return new en(t,e)}(this,{clock:n,logger:r,createActorRef:hn}),o||!s._systemRef||s._systemRef.current||(s._systemRef.current=this.system,s._systemRef.initialize?.()),u&&!o&&this.system.inspect(Ls(u)),s.onRejectedEvent&&!o&&this.system.onRejectedEvent(s.onRejectedEvent),this.sessionId=s._sessionId??`${(h=this.system)._identity.executionId}:${h._identity.nextSessionId++}`,this.id=function(t,e,s){if(void 0!==e){const n=Xs(e);return n?Qs(t,Gs(s?.parent,n.prefix),n.index+1):s?.parent||Qs(t,Gs(void 0,e),1),e}const n=Bs(s?.src),r=Gs(s?.parent,n),o=t._snapshot._nextActorIds[r]??0;return Qs(t,r,o+1),s?.parent||"x"===n||0!==o?`${n}:${o}`:n}(this.system,c,{parent:o,src:s.src??t}),this.logger=e?.logger??this.system._logger,this.clock=e?.clock??this.system._clock,this._parent=o,this._syncSnapshot=i,this.options=s,this.src=s.src??t,this.ref=this,this._actorScope=this,a&&(this.registryKey=a,this.system._set(a,this)),this._collectedMicrosteps=void 0;const d=s._restore;this._restored=void 0!==d;try{if(d)this._setSnapshot(d.restore(this._actorScope));else if(e?._inert);else{this._inTransition=!0;try{const[t,e]=zt(this._actorScope,void 0,this.logic.initialTransition(this.options?.input,this._actorScope));this._setSnapshot(t),this._initialEffects=e.length?e:void 0}finally{this._inTransition=!1}}}catch(t){this._setSnapshot(d?.error?.(t)??{status:"error",output:void 0,error:t}),this._deferred&&(this._deferred.length=0)}a&&"active"!==this._snapshot.status&&this.system._unregister(this),(this.system._hasInspectionObservers?.()??1)&&this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this,parentRef:this._parent,id:this.id,src:this.src,snapshot:this._snapshot})}_getLogger(){return this.logger}get self(){return this}get defer(){const t=t=>this._defer(t);return Object.defineProperty(this,"defer",{value:t}),t}get stopChild(){const t=t=>this._stopChild(t);return Object.defineProperty(this,"stopChild",{value:t}),t}get emit(){const t=t=>b(this,t,this.system);return Object.defineProperty(this,"emit",{value:t}),t}get actionExecutor(){const t=t=>this._executeAction(t);return Object.defineProperty(this,"actionExecutor",{value:t}),t}_isRunning(){return this._processingStatus===on.Running}_defer(t){(this._deferred??=[]).push(t)}_stopChild(t){if(t._parent!==this)throw new Error(i(47,t.id,this.id));t._stop()}_executeAction(t){const e=()=>{(this.system._hasInspectionObservers?.()??1)&&(this._collectedActions??=[]).push({type:t.type,params:t.params});const e=sn;try{sn=!0;const e=t.exec();e&&"function"==typeof e.then&&Promise.resolve(e).catch(t=>{this._processingStatus!==on.Stopped&&this._recoverOrError(t)})}finally{sn=e}};this._processingStatus!==on.Running||this._forceDeferredActions?(this._deferred??=[]).push(e):e()}_setSnapshot(t){const e=this._snapshot;this._snapshot=t,ce(t,this,this.system,e)}_setErrorSnapshot(t,e=this._snapshot){this._setSnapshot({...e,status:"error",error:t})}_recoverOrError(t,e,s){return!!this._tryHandleExecutionError(t,e)||(this._setErrorSnapshot(t),this._error(t,s),!1)}_tryHandleExecutionError(t,e=this._snapshot){const s=this.logic.getExecutionErrorEvent?.(e,t);if(!s)return!1;const n=this._inTransition;this._inTransition=!0;try{const[t,n]=zt(this._actorScope,e,this.logic.transition(e,s,this._actorScope));return this._setSnapshot(t),un(n,this._actorScope),this.update(t,s),!0}catch{return!1}finally{this._inTransition=n}}_next(t){for(const e of this.observers??nn)an(e.next,t)}update(t,e){this._setSnapshot(t);const s=this._deferred;for(let e=0;e<(s?.length??0);e++){const n=s[e];try{n()}catch(e){if(s.length=0,this._tryHandleExecutionError(e,t))return;this._setErrorSnapshot(e,t),this._error(e);break}}if(s&&(s.length=0),"active"===this._snapshot.status)this._next(t);this._inspectTransition(this._snapshot,e)}_inspectTransition(t,e){(this.system._hasInspectionObservers?.()??1)&&this.system._sendInspectionEvent({type:"@xstate.transition",actorRef:this,event:e,sourceRef:this._lastSourceRef,targetRef:this,snapshot:t,microsteps:this._collectedMicrosteps??nn,actions:this._collectedActions??nn,sent:this._collectedSent??nn,eventType:e.type}),this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0}_flushInitialEffects(){if(!this._initialEffects?.length)return!0;this._forceDeferredActions=!0;try{return un(this._initialEffects,this._actorScope),this._initialEffects=void 0,!0}catch(t){return this._initialEffects=void 0,this._deferred&&(this._deferred.length=0),this._recoverOrError(t),"active"===this._snapshot.status}finally{this._forceDeferredActions=!1}}subscribe(t,e,s){const n=Ls(t,e,s);if(this._processingStatus!==on.Stopped)(this.observers??=new Set).add(n);else switch(this._snapshot.status){case"done":an(n.complete);break;case"error":{const t=this._snapshot.error;n.error?(n.passive||(this._errorObserved=!0),an(n.error,t)):R(t);break}}return{unsubscribe:()=>{this.observers?.delete(n)}}}on(t,e){let s=this.eventListeners?.get(t);return s||(s=new Set,(this.eventListeners??=new Map).set(t,s)),s.add(e),{unsubscribe:()=>{s.delete(e)}}}select(t,e=Object.is){return{subscribe:(s,n,r)=>{const o=Ls(s,n,r);let i=t(this.getSnapshot());return this.subscribe({next:s=>{const n=t(s);e(i,n)||(i=n,o.next?.(n))},error:o.error,complete:o.complete})},get:()=>t(this.getSnapshot())}}start(){const t=this._inTransition;this._inTransition=!0;try{return this._start()}finally{this._inTransition=t}}_start(){if(this._processingStatus===on.Running)return this;if(this._processingStatus===on.Stopped){const t=this._snapshot.status;if("done"===t||"error"===t)return this;throw new Error(i(48,this.id))}this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:"xstate.snapshot.actor",actorId:this.id,sessionId:this.sessionId,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this.registryKey&&this.system._set(this.registryKey,this),this._processingStatus=on.Running,this._lastSourceRef=this._parent;switch(this._snapshot.status){case"done":return this._restored?(this._next(this._snapshot),this._stopProcedure(),this._complete(),this):this._flushInitialEffects()?(this.update(this._snapshot,M(this.options.input)),this):this;case"error":return this._error(this._snapshot.error),this;case"stopped":return this._stopProcedure(),this._complete(),this}if(this._parent||this.system.start(),!this._flushInitialEffects())return this;if(this.logic.start)try{this.logic.start(this._snapshot,this._actorScope,{restored:this._restored})}catch(t){return this._setErrorSnapshot(t),this._error(t),this}if(this._restored||this._deferred?.length||this.observers?.size||(this.system._hasInspectionObservers?.()??1)?this.update(this._snapshot,M(this.options.input)):(function(t,e,s){const n=Qt.get(t),r=se(s);return n?.actor===e&&n.systemState.sourceSystem===s&&n.systemState.sourceVersion+1===r&&s._getRootActor?.()===e&&!s._peekChildren?.()&&(n.systemState.root=e,n.systemState.sourceVersion=r,!0)}(this._snapshot,this,this.system)||this._setSnapshot(this._snapshot),this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0),this._restored){const t=this._snapshot.timers??[],e=!this.system._clock.now,s=Date.now();for(const n of t){const t=e&&void 0!==n.scheduledAt?Math.min(n.delay,Math.max(0,n.scheduledAt+n.delay-s)):n.delay;this.system.scheduleTimer(this,N(n),t)}this._restored=!1}return this._mailboxStarted=!0,this.mailbox?.start(),this}_process(t){const e=this._inTransition;this._inTransition=!0;try{this._processEvent(t)}finally{this._inTransition=e}}_processEvent({event:t,sourceRef:e}){this._lastSourceRef=e;const s=this._snapshot;let n,r;try{n=zt(this._actorScope,this._snapshot,this.logic.transition(this._snapshot,t,this._actorScope))}catch(t){r={err:t}}if(r)return this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0,void(this._recoverOrError(r.err)||this._inspectTransition(this._snapshot,t));if(!n)return;let o=this._snapshot;try{const[r,i]=n;r!==s||i.length||!("machine"in s)||t.type===d||e&&Object.hasOwn(rn,t.type)||this._reportUnhandledEvent(t),o=r,this._setSnapshot(o),un(i,this._actorScope),this.update(o,t)}catch(e){return void(this._recoverOrError(e,o,s)||this._inspectTransition(this._snapshot,t))}t.type===d&&(this._stopProcedure(),this._complete())}_reportUnhandledEvent(t){an(()=>this.options.onUnhandledEvent?.(t,this._snapshot))}_emit(t){for(const e of[this.eventListeners?.get(t.type),this.eventListeners?.get("*")])if(e)for(const s of e)an(s,t)}_terminate(t){if("done"===t.status)return this._next(this._snapshot),this._stopProcedure(),this._complete(),void(this._parent&&this.system._relay(this,this._parent,(e=this.id,s=t.output,n=this.sessionId,{type:"xstate.done.actor",output:s,actorId:e,sessionId:n})));var e,s,n;"error"===t.status&&this._error(t.error)}_stop(){return this._processingStatus===on.Stopped?this:(this.mailbox?.clear(),this._processingStatus===on.NotStarted?(this._processingStatus=on.Stopped,this.system._unregister(this),this):(this._send({type:d}),this.system._unregister(this),this))}stop(){if(this._parent)throw new Error(i(49,this.id));return this._stop()}_complete(){for(const t of this.observers??nn)an(t.complete);this.observers?.clear(),this.eventListeners?.clear()}_reportUnlessObserved(t){setTimeout(()=>{this._errorObserved||R(t)})}_error(t,e){this._stopProcedure();const s=new Set;for(const t of[this._snapshot,e]){const e=t?.children;for(const t of Object.values(e??{}))t?._parent===this&&"function"==typeof t.getSnapshot&&s.add(t)}for(const t of this.system._peekChildren?.()?.values()??[])t._parent===this&&s.add(t);for(const t of s)try{if("active"!==t.getSnapshot().status)continue;const e=this.system.stopActor(t);e&&Promise.resolve(e).catch(R)}catch(r){R(r)}if(this.observers?.size){let e=!1,s=!1;for(const n of this.observers){const r=n.error;n.passive||(e||=!r,s||=!!r),an(r,t)}e||=!s,this.observers.clear(),e&&this._reportUnlessObserved(t)}else this._parent||this._reportUnlessObserved(t);var n,r,o;this.eventListeners?.clear(),this._parent&&this.system._relay(this,this._parent,(n=this.id,r=t,o=this.sessionId,{type:"xstate.error.actor",error:r,actorId:n,sessionId:o}))}_stopProcedure(){this._processingStatus===on.Running&&(this.system.cancelAllTimers(this),this.mailbox?.clear(),this.mailbox=void 0,this._boundProcess=void 0,this._mailboxStarted=!1,this._processingStatus=on.Stopped,this.system._unregister(this))}_send(t,e){if(this._processingStatus===on.Stopped)return void this.system.deadLetter(e,this,t,"stopped");let s=this.mailbox;s||(this._boundProcess??=this._process.bind(this),s=this.mailbox=new E(this._boundProcess),this._mailboxStarted&&s.start()),s.enqueue({event:t,sourceRef:e})}_sendPublic(t){this.system._relay(void 0,this,t)}toJSON(){return{"@actor":this.address}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[P](){return this}getSnapshot(){return this._snapshot}}function fn(t,...[e]){return a(e),new dn(t,e)}class ln extends Error{constructor(t){super(t),this.name="InvalidSnapshotError"}}function pn(t){return null!==t&&"object"==typeof t&&!Array.isArray(t)}function yn(t){return pn(t)&&"function"==typeof t.transition&&"function"==typeof t.getInitialSnapshot}function gn(t,e,s,n){if("string"==typeof e){if(!s.states[e])throw new ln(i(21,n.concat(e).join(".")));return}const r=t=>new ln(i(22===t?22:23,n.join(".")));if(!e||"object"!=typeof e||Array.isArray(e))throw r(22);const o=Object.keys(e);if(o.length>1&&"parallel"!==s.type)throw r(23);for(const r of o){const o=s.states[r];if(!o)throw new ln(i(21,n.concat(r).join(".")));gn(t,e[r],o,n.concat(r))}}function vn(t,e){if(!pn(e))throw new ln(i(24,t.id));if(void 0===e.value)throw new ln(i(25,t.id));gn(t,e.value,t.root,[]);const s=Array.from(je(He(t.root,e.value)));if(!pn(e.children))throw new ln(i(26));const n=new Map;for(const[r,o]of Object.entries(e.children)){if(!pn(o)||void 0!==o.remote&&"boolean"!=typeof o.remote||void 0!==o.address&&"string"!=typeof o.address||void 0!==o.syncSnapshot&&"boolean"!=typeof o.syncSnapshot||void 0!==o.registryKey&&"string"!=typeof o.registryKey||void 0!==o.incarnation&&"string"!=typeof o.incarnation||void 0!==o.snapshot&&!pn(o.snapshot))throw new ln(i(27,r));if(!0!==o.remote){if(void 0!==o.src&&"string"!=typeof o.src&&!yn(o.src))throw new ln(i(27,r));try{const e=void 0===o.src?Cs(t,s,r):"string"==typeof o.src?Object.hasOwn(t.sources.actors,o.src)?t.sources.actors[o.src]:void 0:o.src;if(!e)throw new ln(i(29,r,"string"==typeof o.src?o.src:"<inline>"));n.set(r,e)}catch(t){if(t instanceof ln)throw t;throw new ln(t instanceof Error?t.message:i(30,r))}}else if("string"!=typeof o.address||"string"!=typeof o.src)throw new ln(i(28,r))}if(void 0!==e.timers&&!Array.isArray(e.timers))throw new ln(i(31));for(const t of e.timers??[]){if(!pn(t)||"number"!=typeof t.delay||"@xstate.raise"!==t.type&&"@xstate.sendTo"!==t.type||!pn(t.event)||"string"!=typeof t.event.type||void 0!==t.id&&"string"!=typeof t.id||void 0!==t.seq&&"number"!=typeof t.seq||void 0!==t.scheduledAt&&"number"!=typeof t.scheduledAt||!("string"==typeof t.target||pn(t.target)&&"parent"===t.target.type||g(t.target)))throw new ln(i(32));if("string"==typeof t.target&&"self"!==t.target&&!Object.hasOwn(e.children,t.target))throw new ln(i(33,t.target))}return{nodes:s,childLogics:n}}const _n=new WeakMap;const mn={};class xn{constructor(t,e){if(this.config=t,this.key=void 0,this.id=void 0,this.type=void 0,this.path=void 0,this.states=void 0,this.history=void 0,this.entry=void 0,this.exit=void 0,this.parent=void 0,this.machine=void 0,this.meta=void 0,this.output=void 0,this.order=-1,this.description=void 0,this.schemas=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._candidateCache=void 0,this.parent=e._parent,this.key=e._key,this.machine=e._machine,this.parent&&function(t){for(let e=0;e<t.length;e++)if("\\"===t[e])e++;else if("."===t[e])return!0;return!1}(this.key))throw new Error(i(57,this.key));this.path=this.parent?this.parent.path.concat(this.key):[];const s=this.config.states?Object.keys(this.config.states)[0]:void 0;this.id=this.config.id||(this.machine.id?[this.machine.id,...this.path]:this.path).join("."),this.type=this.config.type||(void 0!==s?"compound":this.config.history?"history":"atomic"),this.description=this.config.description,this.schemas=this.config.schemas,function(t){const e=t.config;if("history"===t.type&&!("string"==typeof e.target&&e.target.trim().length>0||Array.isArray(e.target)&&e.target.length>0&&e.target.every(t=>"string"==typeof t&&t.trim().length>0)))throw new Error(i(60,t.id));if("choice"!==t.type)return;if("function"!=typeof e.choice)throw new Error(i(61,t.id))}(this);if(this.machine.idMap.get(this.id))throw new Error(i(58,this.id));if(this.order=this.machine.idMap.size,this.machine.idMap.set(this.id,this),this.states=this.config.states?function(t,e){const s={},n=Object.keys(t);for(let r=0;r<n.length;r++){const o=n[r],i=e(t[o],o,t,r);"__proto__"===o?Object.defineProperty(s,o,{value:i,enumerable:!0,configurable:!0,writable:!0}):s[o]=i}return s}(this.config.states,(t,e)=>new xn(t,{_parent:this,_key:e,_machine:this.machine})):mn,"compound"===this.type&&!this.config.initial)throw new Error(i(59,this.id));this.history=!0===this.config.history?"shallow":this.config.history||!1,this.entry=this.config.entry,this.exit=this.config.exit,this.meta=this.config.meta,this.output="final"!==this.type&&this.parent?void 0:this.config.output,this.tags=$s(t.tags).slice(),this.invoke=$s(this.config.invoke).map((t,e)=>{const{src:s,registryKey:n}=t,r=`${this.id}:${e}`;const o=t.id??r;return{...t,src:"string"==typeof s?s:void 0,logic:s,id:o,registryKey:n}})}_initialize(){this.after=function(t){const e=t.config.after,s=t.config.timeout,n=t.config.onTimeout,r=t.invoke.filter(t=>void 0!==t.timeout);if(!e&&void 0===s&&0===r.length)return[];const o=[];if(e)for(const s of Object.keys(e)){const n=Number.isNaN(+s)?s:+s;o.push({event:k(n,t.id),delay:n,transitions:e[s],fromDelaysMap:!0,name:`after.${s}`})}var i;void 0!==s&&n&&o.push({event:(i=t.id,{type:"xstate.timeout.state",stateId:i}),delay:s,transitions:n,fromDelaysMap:!0,name:"onTimeout"});for(const t of r)o.push({event:O(t.id),resolveEvent:({children:e})=>O(t.id,e[t.id]?.sessionId),eventMatcher:(e,s)=>Pe(e,s,t.id),delay:t.timeout,transitions:t.onTimeout,fromDelaysMap:!1,name:"invoke.onTimeout"});const c=[];for(const{event:e,resolveEvent:s,eventMatcher:n,delay:r,transitions:i,fromDelaysMap:a,name:u}of o){$e(t,e,s??(()=>e),e=>Ie(r,a?e.delays:{},{context:e.context,event:e.event,stateNode:t,input:e.input}));const{type:o,...h}=e;for(const e of Ns(i,u,t.id))c.push({...e,matches:{...e.matches,...Object.fromEntries(Object.entries(h).filter(([,t])=>void 0!==t))},event:o,delay:r,_eventMatcher:n})}return c.map(e=>({...Ke(t,e.event,e),delay:e.delay}))}(this),this.transitions=function(t){const e=new Map,s=(t,s)=>{e.set(t,[...e.get(t)??[],...s])};if(t.config.on)for(const s of Object.keys(t.config.on)){if(""===s)throw new Error(i(63,t.id));const n=t.config.on[s];e.set(s,wn(t,s,n,e=>Ke(t,s,e)))}if(t.config.onDone){const e="xstate.done.state";s(e,wn(t,"onDone",t.config.onDone,s=>Ke(t,e,{...s,matches:{...s.matches,stateId:t.id}})))}if(t.config.onError){const s="xstate.error.*";e.set(s,wn(t,"onError",t.config.onError,e=>Ke(t,s,e)))}const n=(e,s,n)=>Ke(t,e,{matches:{actorId:n},_eventMatcher:(t,e)=>Pe(t,e,n),to:(t,e)=>(e.cancel(s),{})}),r=(e,s,n)=>{const{target:r,to:o,reenter:i,...c}=s;return Ke(t,e,{...c,reenter:i,to:(t,e)=>{if(o){let s=!1;const r=new Proxy(e,{apply:(t,e,n)=>(s=!0,Reflect.apply(t,e,n)),get(t,e,n){const r=Reflect.get(t,e,n);return"function"!=typeof r?r:(...e)=>(s=!0,r.apply(t,e))}}),i=o(t,r);return(void 0!==i||s)&&e.cancel(n),i}return e.cancel(n),{target:r,reenter:i}}})};for(const e of t.invoke){const o=t=>({...t,matches:{...t.matches,actorId:e.id},_eventMatcher:(t,s)=>Pe(t,s,e.id)}),i=void 0!==e.timeout?{event:{type:"xstate.timeout.actor",actorId:e.id}}:void 0;if(e.onDone){const c="xstate.done.actor",a=wn(t,"invoke.onDone",e.onDone,e=>{const s=o(e);return i?r(c,s,i):Ke(t,c,s)});i&&a.push(n(c,i,e.id)),s(c,a)}else if(i){const t="xstate.done.actor";s(t,[n(t,i,e.id)])}if(e.onError){const n="xstate.error.actor";s(n,wn(t,"invoke.onError",e.onError,e=>{const s=o(e);return i?r(n,s,i):Ke(t,n,s)}))}if(e.onSnapshot){const n="xstate.snapshot.actor";s(n,wn(t,"invoke.onSnapshot",e.onSnapshot,e=>Ke(t,n,o(e))))}}for(const s of t.after){let t=e.get(s.eventType);t||(t=[],e.set(s.eventType,t)),t.push(s)}return e}(this),"choice"===this.type?this.always=function(t){const e=t.config.choice,s=e=>{if(!e||void 0===e.target)throw new Error(i(62,t.id));return e};return[Ke(t,"",{to:t=>s(e(t))})]}(this):this.config.always&&(this.always=wn(this,"always",this.config.always,t=>Ke(this,"",t)));for(const t of Object.keys(this.states))this.states[t]._initialize();this._refreshEventMetadata()}_refreshEventMetadata(){const t={},e=[];for(const[s,n]of this.transitions)t[s]=n.slice(),n.some(t=>t.target||t.reenter||t.to)&&e.push(s);this.on=t,this.ownEvents=e;const s=new Set(e);for(const t of Object.values(this.states))for(const e of t.events)s.add(e);this.events=Array.from(s)}get initial(){return function(t,e,s){let n=_n.get(t);return n?e in n||(n[e]=s()):(n={[e]:s()},_n.set(t,n)),n[e]}(this,"initial",()=>function(t,e){const s="object"==typeof e&&null!==e?e.target:e,n="object"==typeof e&&null!==e?e.input:void 0,r="object"==typeof e&&null!==e?e.to:void 0,o="object"==typeof e&&null!==e?e.meta:void 0,c="object"==typeof e&&null!==e?e.description:void 0,a=Array.isArray(s)?s:s?[s]:[],u=a.map(e=>e.startsWith("#")?t.machine.getStateNodeById(e.slice(1)):t.states[e]);if(u.some(t=>!t))throw new Error(i(64,a.find((t,e)=>!u[e]),t.id));return{source:t,target:u.length?u:void 0,input:n,to:r,meta:o,description:c}}(this,this.config.initial))}next(t,e,s,n){if("final"===this.type)return;let r=this._candidateCache?.get(e.type);r||(r=function(t,e){const s=t.transitions.get(e.type)??[],n=[...t.transitions.keys()].filter(t=>t!==e.type&&Ws(e.type,t)).sort((t,e)=>e.length-t.length).flatMap(e=>t.transitions.get(e));return s.length?[...s,...n]:n}(this,e),(this._candidateCache??=new Map).set(e.type,r));for(const o of r){if(ls(o,e,t,this,s,n))return[o]}}}function wn(t,e,s,n){const r=Ns(s,e,t.id),o=new Array(r.length);for(let t=0;t<r.length;t++)o[t]=n(r[t]);return o}function bn(t,e){const s=t.check(e);if(s)throw s}let Sn,In;function An(){if(In)return In;const t=Sn??=fn({transition:t=>[t,[]],initialTransition:()=>[{status:"active",output:void 0,error:void 0},[]],getInitialSnapshot:()=>({status:"active",output:void 0,error:void 0}),getPersistedSnapshot:t=>t});return In={self:t,logger:()=>{},id:"",sessionId:"",defer:()=>{},system:t.system,stopChild:()=>{},emit:()=>{},actionExecutor:()=>{}},jt.add(In),In}class En{constructor(t,e,s){if(this.config=t,this.validator=s,this._internalEventType=void 0,this._transitionMetaType=void 0,this._inputType=void 0,this._actionMap=void 0,this._actorMap=void 0,this._stateSchema=void 0,this.version=void 0,this.schemas=void 0,this.sources=void 0,this.options=void 0,this.idMap=new Map,this.root=void 0,this._raw=()=>this,this.id=void 0,this.states=void 0,this.events=void 0,this.internalEventDescriptors=void 0,this._hasEventlessTransitions=void 0,this._microstepHooks=void 0,this._json=void 0,void 0!==t.version&&!t.id)throw new Error(i(18,t.version));this.id=t.id??"",this.sources={actors:t.actors??{},actions:t.actions??{},delays:t.delays??{},guards:t.guards??{},...e},this.version=this.config.version,this.schemas=this.config.schemas,this.internalEventDescriptors=Object.keys(this.schemas?.internalEvents??{}),this.options={...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.start=this.start.bind(this),this.root=new xn(t,{_key:this.id,_machine:this}),this.root._initialize(),function(t){const e=[],s=n=>{Object.values(n).forEach(n=>{if(n.config.route&&n.config.id){const r=n.config.id,o=n.config.route,i=({event:t})=>t.to===`#${r}`;if("function"==typeof o)return e.push(Ke(t,"xstate.route",{guard:i,to:t=>{const e=o(t);if(e)return{...!0===e?{}:e,target:`#${r}`}}})),void(n.states&&s(n.states));const{guard:c,...a}=o,u={...a,guard:i,target:`#${r}`};e.push(Ke(t,"xstate.route",u))}n.states&&s(n.states)})};s(t.states),e.length>0&&t.transitions.set("xstate.route",e)}(this.root),this.root._refreshEventMetadata(),this._hasEventlessTransitions=Array.from(this.idMap.values()).some(t=>!!t.always?.length),this.states=this.root.states,this.events=this.root.events}provide(t){const{actions:e,guards:s,actors:n,delays:r}=this.sources,o=new En(this.config,{actions:{...e,...t.actions},guards:{...s,...t.guards},actors:{...n,...t.actors},delays:{...r,...t.delays}},this.validator);return o._json=this._json,o.internalEventDescriptors=this.internalEventDescriptors,o._microstepHooks=this._microstepHooks,o}resolveState(t){const e=(s=this.root,n=t.value,Te(s,je(He(s,n))));var s,n;const r=je(He(this.root,e));return Es({_nodes:[...r],value:e,context:t.context||{},children:{},status:Me(r,this.root)?"done":t.status||"active",output:t.output,error:t.error,historyValue:t.historyValue},this)}transition(t,e,s){const n=!s,r=s??be(this,t);if(n&&fe(r,t,!1),this.validator){const n=s&&s.self._lastSourceRef,o=n?"actor":"external",i=this.validator.check({kind:"event",logic:this,event:e,eventOrigin:o});if(i)return[t,[ot(r,n,e,"invalidEvent",{issues:i.issues,error:i})]]}bt(r);const o=this._transitionFast(t,e,r);if(o){n&&fe(r,o,!1);const e=n&&o!==t?pe(r,o):this._attachPureActorRef(o,r);return this.validator&&bn(this.validator,{kind:"result",logic:this,snapshot:e,effects:[]}),[e,[]]}const{snapshot:i,microsteps:c}=is(t,e,r,[]);n&&fe(r,i,!1);const a=n?i===t?i:pe(r,i):this._attachPureActorRef(i,r),u=this._collectEffects(c);return this.validator&&bn(this.validator,{kind:"result",logic:this,snapshot:a,effects:u}),[a,u]}_collectEffects(t){return t.flatMap(([,t])=>t)}_attachPureActorRef(t,e,s=!1){return le(e)||s&&e.self._actorScope===e&&void 0===e.self._snapshot||ce(t,e.self,e.system),t}_transitionFast(t,e,s){if("active"!==t.status||"string"!=typeof t.value||this.root.always?.length)return;const n=this.root.states[t.value];if(!n||"atomic"!==n.type||n.exit||n.invoke.length||n.always?.length||n.after?.length)return;const r=n.transitions.get(e.type);if(1!==r?.length)return;const o=r[0];if(o.guard||o.actions||o.to||o.reenter||o.input||"function"==typeof o.context||o.target&&1!==o.target.length)return;const i=o.target?.[0]??n,c=i!==n;if(i.parent!==this.root||"atomic"!==i.type||c&&(i.entry||i.invoke.length||i.always?.length||i.after?.length))return;const a=void 0!==o.context?{...t.context,...o.context}:t.context;if(!le(s)&&(s.system._hasInspectionObservers?.()??1)){const t=s.self._collectedMicrosteps||[];t.push(o),s.self._collectedMicrosteps=t}const u=t._stateInputs[this.root.id];return ks(t,{...a!==t.context?{context:a}:{},...c?{_nodes:[this.root,i],_stateInputs:u?{[this.root.id]:u}:{}}:{}})}microstep(t,e,s){const{microsteps:n}=is(t,e,s,[]),r=new Array(n.length);for(let t=0;t<n.length;t++)r[t]=n[t][0];return r}getTransitionData(t,e,s,n){return this._microstepHooks?.begin(s.self),Be(this.root,t.value,t,e,s,n)||[]}isInternalEventType(t){if(t===l)return!0;for(const e of this.internalEventDescriptors)if(Ws(t,e))return!0;return!1}_canTransition(t,e){if("active"!==t.status)return!1;const s=An(),n=this.getTransitionData(t,e,s);if(!n?.length)return!1;for(const r of n){if(void 0!==r.target)return!0;const n=rs(r,t,e,s,{resolveActions:!1});if(n.targets?.length||n.context||cs(r,t.context,e,t,s))return!0}return!1}getExecutionErrorEvent(t,e){if("active"===t?.status&&t.nodes?.some(t=>t.config.onError))return T("execution",e)}_getPreInitialState(t,e){const{context:s}=this.config,n=Es({context:"function"!=typeof s&&s?s:{},_nodes:[this.root],children:{},status:"active"},this);if("function"==typeof s){const r={},o=function(t,e,s){return(n,r)=>{const o=B(e,n),i=r?.id??Et(t,o??n).id;void 0!==r?.id&&(kt(t,r.id),Ot(t,r.id));const c=t.system.createActorRef(n,{id:i,parent:t.self,syncSnapshot:r?.syncSnapshot,input:r?.input,src:o??n,registryKey:r?.registryKey});return s[c.id]=c,c}}(t,this.sources.actors,r),i=s(function(t,e){return ue(e)?(Object.defineProperty(t,"self",{enumerable:!0,get:()=>e.self}),t):Object.assign(t,{self:e.self})}({spawn:o,input:e.input,actors:this.sources.actors},t)),[c]=Vt(n,0,t,[]);if(i&&(c.context=i),Object.keys(r).length>0){c.children={...c.children,...r};const e=function(t){const e=xt.get(t)?.counters;if(e?.size)return Object.fromEntries(e)}(t);e&&(c._nextActorIds=vt(c._nextActorIds,e))}return c}return n}getInitialSnapshot(t,e){return this.initialTransition(e,t)[0]}initialTransition(t,e){this.validator&&bn(this.validator,{kind:"input",logic:this,input:t});const s=!e,n=e??be(this);bt(n);const r=M(t),o=[],i=(t,e)=>{s&&fe(n,t,!1);const r=s?pe(n,t):this._attachPureActorRef(t,n,!0),o=this._collectEffects(e);return this.validator&&"error"!==t.status&&bn(this.validator,{kind:"result",logic:this,snapshot:r,effects:o}),[r,o]};let c;try{c=this._getPreInitialState(n,r)}catch(t){return i(ks(Es({context:"function"!=typeof this.config.context&&this.config.context?this.config.context:{},_nodes:[this.root],children:{},status:"active"},this),{status:"error",error:t}),[])}const a=Object.values(c.children).filter(Boolean).map(t=>function(t){const e=[t];return{kind:"builtin",exec:ct,type:h,source:t._parent,params:void 0,args:e,actor:t,id:t.id,logic:t.logic,src:t.src,input:t.options?.input}}(t));try{const[t,e]=function(t,e,s,n,r){t.machine._microstepHooks?.begin(s.self);const o=[...We(t)],i=new Set,c=t=>{if("compound"===t.type){i.add(t);for(const e of t.initial.target??[])c(e)}else if("parallel"===t.type)for(const e of ke(t))c(e)};return c(t),ns([{target:o.filter(t=>!o.some(e=>e!==t&&Fe(e,t))),source:t,reenter:!0,eventType:null,toJSON:null,_statesForDefaultEntry:[...i]}],e,s,n,!0,r)}(this.root,c,n,r,o),{snapshot:s,microsteps:u}=is(t,r,n,o,[[t,[...a,...e]]]);return i(s,u)}catch(t){if(!this.root.config.onError)throw t;const e=is(c,T("execution",t),n,[],[[c,a]]);return i(e.snapshot,e.microsteps)}}start(t,e){if(!t?.children)return;const s=t.children;for(const t in s){if(!Object.hasOwn(s,t))continue;const n=s[t];n._rehydrated&&"active"===n.getSnapshot?.().status&&(e?e.actionExecutor(qt(n)):n.system.startActor(n))}}getStateNodeById(t){const e=Re(t),s=e?t.slice(1):t;if(""===s&&e)throw new Error(i(19,this.id));const n=this.idMap.get(s);if(n)return n;throw new Error(i(20,s))}getPersistedSnapshot(t,e){return function(t,e){const{nodes:s,_stateInputs:n,_nextTimerId:r,_nextActorIds:o,tags:c,machine:a,children:u,timers:h,context:d,can:f,hasTag:l,matches:p,getMeta:v,getInputs:_,toJSON:m,...x}=t,w={},b=[],S=!1!==e?.embedChildren;for(const t in u){const n=u[t],r="string"==typeof n.src?n.src:void 0,o=void 0===r&&!0===n._invoked&&s.some(e=>e.invoke.some(e=>e.id===t));if(!(void 0!==r||o||e&&"__unsafeAllowInlineActors"in e))throw new Error(i(41,t));const c=S&&!y(n);if(!c&&void 0===r)throw new Error(i(42,t));w[t]={address:n.address,...c?{snapshot:n.getPersistedSnapshot(e)}:{remote:!0,incarnation:y(n)?n.sessionId:void 0},...o?{}:{src:n.src},registryKey:n.registryKey,syncSnapshot:n._syncSnapshot}}const I=oe(t)?.actor,A=I?.system?._clock?.now?void 0:I?.system?._snapshot?._scheduledTimers;for(const e of h){let s,n=e.event;if("xstate.timeout.actor"===n.type&&(n={...n},delete n.sessionId),"self"===e.target)s="self";else if(g(e.target))s={...e.target};else{const n=Object.entries(u).find(([,t])=>t===e.target)?.[0];if(n)s=n;else{if(e.target!==oe(t)?.actor._parent)throw new Error(i(43,e.target.id));s={type:"parent"}}}const r=A?.[V(I,N(e))],o=r?r.dueAt-e.delay:W(e);b.push({...void 0!==e.id&&{id:e.id},...void 0!==e.seq&&{seq:e.seq},delay:e.delay,type:e.type,event:n,target:s,...void 0!==o&&{scheduledAt:o}})}const E={...x,context:js(d,()=>oe(t)?.actor.id??a.id),children:w,timers:b,historyValue:Os(x.historyValue)};return n&&Object.keys(n).length>0&&(E.stateInputs=n),E.nextTimerId=r,o&&Object.keys(o).length>0&&(E.nextActorIds=o),void 0!==a.version&&(E.machine={id:a.id,version:a.version}),E}(t,e)}}const kn=new WeakMap;function On(t,e,s){if(t instanceof En)return Tn(t,e,s);if(!t.restoreSnapshot&&null===e)throw new Error(i(46));return t.restoreSnapshot?t.restoreSnapshot(e,s):e}function jn(t,e){return{restore:s=>On(t,e,s),error:s=>t instanceof En?function(t,e,s){const n=e?.context;return ks(Es({context:n&&"object"==typeof n?n:{},_nodes:[t.root],children:{},status:"active"},t),{status:"error",error:s})}(t,e,s):void 0}}function Tn(t,e,s){t instanceof En&&(t=t._raw()),e=o(e);const n=!s,r=s??be(t);if(!e||"object"!=typeof e||Array.isArray(e))throw new ln(i(24,t.id));const c=e.machine,a=c&&"object"==typeof c&&"function"==typeof c.transition&&"root"in c?void 0:c,u=e.version,h="string"==typeof a?.version?a.version:u;if(void 0!==u&&void 0!==a?.version&&a.version!==u)throw new Error(i(34,u,a.version));if(a&&"string"==typeof a.id&&a.id!==t.id)throw new Error(i(35,a.id,t.id));if(h!==t.version){const s=t.config.migrate;if("function"!=typeof s)throw new Error(i(36,h,t.version));e=s(e,h)}const d=e,{nodes:f,childLogics:l}=vn(t,d),p={},y=d.children;for(const t of Object.keys(y)){const e=y[t];if(!0===e.remote&&void 0!==e.address){if("string"!=typeof e.src)throw new Error(i(28,t));const s=w(r.system,{id:t,address:e.address,src:e.src,parent:r.self,registryKey:e.registryKey,syncSnapshot:e.syncSnapshot,incarnation:e.incarnation});e.registryKey&&r.system._set(e.registryKey,s),p[t]=s;continue}const s=e.snapshot,n=e.src,o=l.get(t),c=r.system.createActorRef(o,{id:t,parent:r.self,syncSnapshot:e.syncSnapshot,...void 0!==s?{_restore:jn(o,s)}:{},src:n,registryKey:e.registryKey});c._rehydrated=!0,void 0===n&&(c._invoked=!0),p[t]=c}const v=[],_=d.timers??[];for(const t of _){let e=t.event;if("xstate.timeout.actor"===e.type){const t=e.actorId,s=p[t];s&&(e=O(t,s.sessionId))}const s="string"==typeof t.target?"self"===t.target?"self":p[t.target]:g(t.target)?t.target:r.self._parent;if(!s){const e="string"==typeof t.target?t.target:t.target.type;throw new ln(i(33,e))}v.push({...t,event:e,target:s})}const m=(e=>{if(!e||"object"!=typeof e)return{};const s={};for(const n of Object.keys(e)){const r=e[n];for(const e of r){let r;if(e instanceof xn)r=e;else try{r=t.getStateNodeById(e.id)}catch{}r&&(s[n]??=[],s[n].push(r))}}return s})(d.historyValue),{version:x,_nextActorId:b,nextTimerId:S,nextActorIds:I,stateInputs:A,...E}=e;let k=I;for(const t of Object.keys(y)){const e=Xs(t);e&&(k?.[e.prefix]??0)<=e.index&&(k={...k,[e.prefix]:e.index+1})}const j=new Map;const T=Es({...E,context:function t(e,s=!1){if(!e||"object"!=typeof e)return e;if(!s&&g(e))return void 0!==e.incarnation?e:Object.values(p).find(t=>t.address===e["@actor"])??e;const n=Object.getPrototypeOf(e);if(!Array.isArray(e)&&n!==Object.prototype&&null!==n)return e;let r=j.get(e);if(!r){j.set(e,r=Array.isArray(e)?[]:Object.create(n));for(const s of Object.keys(e))Object.defineProperty(r,s,{value:t(e[s]),writable:!0,enumerable:!0,configurable:!0})}return r}(E.context,!0),children:p,timers:v,_nextTimerId:S,_nextActorIds:k,_nodes:f,value:void 0,historyValue:m,_stateInputs:Object.fromEntries(Object.entries(d.stateInputs??{}).filter(([t])=>f.some(e=>e.id===t)))},t,r.self);return n?(fe(r,T,!1),pe(r,T)):(le(r)||ce(T,r.self,r.system),T)}t.restoreActor=function(t,e,s){const{system:n,...r}=s??{};a(r);const o={...r,_restore:jn(t,e)};return n?n.createActor(t,o):new dn(t,o)},t.restoreLogicSnapshot=On,t.restoreMachineSnapshot=Tn,t.snapshotSchema=function(t){return function(t,e,s){let n=t.get(e);return n||t.set(e,n=s()),n}(kn,t,()=>({"~standard":{version:1,vendor:"xstate",validate:async e=>{if(null===e||"object"!=typeof e)return{issues:[{message:"Expected a persisted snapshot."}]};const s={historyValue:{},timers:[],...e},n=t.schemas?.context;let r=s.context;if(n){const e=await n["~standard"].validate(r);if(e.issues)return{issues:[{message:`Invalid context for machine '${t.id}' version '${t.version}': ${e.issues[0]?.message}`}]};r=e.value}for(const t of["value","children"])if(!(t in s))return{issues:[{message:`Persisted snapshot is missing '${t}'.`}]};if(!["active","done","error","stopped"].includes(s.status))return{issues:[{message:"Persisted snapshot has invalid status."}]};if(!pn(s.historyValue))return{issues:[{message:"Persisted snapshot has invalid 'historyValue'."}]};try{vn(t,s)}catch(t){return{issues:[{message:t instanceof Error?t.message:"Persisted snapshot has invalid state value."}]}}return{value:{...s,context:r}}}}}))},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).XStatePersist={})}(this,function(t){"use strict";const e={$actor:["@actor",["incarnation"]],$error:["@error",["message"]],$stateNode:["@stateNode",[]],$systemMapper:["@systemMapper",[]]},s={_stateInputs:"stateInputs",_nextTimerId:"nextTimerId",_nextActorIds:"nextActorIds"};function n(t){if(!t||"object"!=typeof t)return!1;const e=Object.getPrototypeOf(t);return e===Object.prototype||null===e||Array.isArray(t)}function r(t,s){if(!n(t))return t;if(Array.isArray(t)){const e=t.map(t=>r(t,s));return e.some((e,s)=>e!==t[s])?e:t}const o=Object.keys(t),i=o.find(t=>s.includes(t));if(i&&"string"==typeof t[i]&&o.every(t=>t===i||e[i][1].includes(t))){const{[i]:s,...n}=t;return{[e[i][0]]:s,...n}}let c;for(const e of o){const n=r(t[e],s);n!==t[e]&&((c??={...t})[e]=n)}return c??t}function o(t){return t?.machine&&"function"==typeof t.matches?t:n(e=t)&&"_nextTimerId"in e&&!("nextTimerId"in e)?r(function(t){if(!n(t)||"function"==typeof t.matches)return t;let e;for(const[n,r]of Object.entries(s))if(n in t&&!(r in t)){const{[n]:s,...o}=e??t;e={...o,[r]:s}}return e??t}(t),["$actor"]):t;var e}function i(t,...e){return`XState error E${t}${e.length?` (${e.map(t=>"string"==typeof t?JSON.stringify(t):String(t)).join(", ")})`:""}. See https://github.com/statelyai/xstate/blob/next/docs/errors.md#e${t}`}const c=new Set(["clock","logger","syncSnapshot","id","registryKey","input","src","inspect","onRejectedEvent","onUnhandledEvent","_systemRef","_sessionId","_inert","_restore"]);function a(t){if(t&&"object"==typeof t)for(const e of Object.keys(t))if(!c.has(e))throw new Error(i(8,e))}const u="xstate.init",h="@xstate.spawn",d="xstate.stop",f="@xstate.terminate",l="xstate.timer",p={unsubscribe(){}};function y(t){return!0===t._remote}function g(t){if("object"!=typeof t||null===t||Array.isArray(t)||"string"!=typeof t["@actor"])return!1;for(const e in t)if("@actor"!==e&&"incarnation"!==e)return!1;const e=t.incarnation;return void 0===e||"string"==typeof e}function v(t){const e=t.slice(t.lastIndexOf("/")+1);return e.includes("%")?e.replaceAll("%2F","/").replaceAll("%25","%"):e}function _(t){return new Error(i(50,...void 0===t.incarnation?[t["@actor"]]:[t["@actor"],t.incarnation]))}function m(t,e){const s=t._getByAddress?.(e["@actor"]);return!s||void 0!==e.incarnation&&s.sessionId!==e.incarnation?void 0:s}const x=Object.freeze({status:"active",output:void 0,error:void 0});function w(t,e){const s={_remote:!0,ref:void 0,id:e.id,address:e.address,src:e.src,registryKey:e.registryKey,sessionId:e.incarnation,system:t,_parent:e.parent,_syncSnapshot:e.syncSnapshot,send(e){t.sendEvent(void 0,n,e)},_send(t){throw new Error(i(52,e.address,t.type))},getSnapshot:()=>x,getPersistedSnapshot(){throw new Error(i(53,e.address))},start(){},_stop(){},stop(){throw new Error(i(54,e.address))},select(){throw new Error(i(55,e.address))},get trigger(){throw new Error(i(56,e.address))},subscribe:()=>p,on:()=>p,_isRunning:()=>!1,toJSON:()=>({"@actor":e.address})},n=s;return s.ref=n,n}function b(t,e,s){return t.system._notifyEmitted(t,e),s.emitEvent(t,e)}const S=new WeakMap;async function I(t,e,s,n=e=>{t.system.sendEvent(t,t,e)}){const r=function(t,e){const s=t.getSnapshot().effects;return s&&Object.prototype.hasOwnProperty.call(s,e)?s[e]:void 0}(t,e);if("done"===r?.status)return r.output;if("error"===r?.status)throw r.error;const o=S.get(t)?.get(e);if(o)return o;if("active"!==t.getSnapshot().status)throw new Error(i(72,e));let c,a;const u=new Promise((t,e)=>{c=t,a=e});let h,d=S.get(t);d||(d=new Map,S.set(t,d)),d.set(e,u);const f=()=>{h?.unsubscribe(),d.delete(e),d.size||S.delete(t)};return u.then(f,f),h=t.subscribe({error:a,complete:()=>a(new Error(i(72,e)))}),(async()=>{try{n({type:"xstate.logic.effect.start",key:e});const r=await s();"active"===t.getSnapshot().status&&(n({type:"xstate.logic.effect.resolve",key:e,output:r}),c(r))}catch(s){"active"===t.getSnapshot().status&&n({type:"xstate.logic.effect.reject",key:e,error:s}),a(s)}})().catch(a),u}function A(t,e,s){const n=function(t,e,s){const n=e;if(t!==e&&n.logic?.isInternalEventType?.(s.type))return new Error(i(73,s.type,e.id))}(t,e,s);return!!n&&(e.system.deadLetter(t,e,s,"internalEvent",{error:n}),!0)}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}}function k(t,e){return{type:"xstate.after",delay:t,stateId:e}}function O(t,e){return{type:"xstate.timeout.actor",actorId:t,sessionId:e}}function j(t,e){return{type:"xstate.done.state",stateId:t,output:e}}function T(t,e){return{type:`xstate.error.${t}`,error:e}}function M(t){return{type:u,input:t}}function R(t){setTimeout(()=>{throw t})}const P="function"==typeof Symbol&&Symbol.observable||"@@observable",$=[],K=new Error("Transition effect");function q(t,e="abort"){const s={mode:e,parent:t,requiresReevaluation:!1};return $.push(s),{frame:s,close(){$.pop()}}}function D(t){const{stateId:e,delay:s,actorId:n}=t;switch(t.type){case"xstate.after":return{type:t.type,delay:s,stateId:e};case"xstate.timeout.state":return{type:t.type,stateId:e};case"xstate.timeout.actor":return{type:t.type,actorId:n}}}function N(t){return void 0!==t.id?{id:t.id}:void 0!==t.seq?{seq:t.seq}:{event:D(t.event)??t.event}}function z(t){const e=t;return"string"==typeof e.id?{id:e.id}:"number"==typeof e.seq?{seq:e.seq}:e.event&&"object"==typeof e.event&&"string"==typeof e.event.type?{event:e.event}:void 0}function L(t,e){if("id"in e)return t.id===e.id;if(void 0!==t.id)return!1;if("seq"in e)return t.seq===e.seq;if(void 0!==t.seq)return!1;const s=t.event,n=e.event;for(const t in n)if(s[t]!==n[t])return!1;return!0}function V(t,e){return`${t.sessionId}|${function(t){return"id"in t?JSON.stringify(["id",t.id]):"seq"in t?JSON.stringify(["seq",t.seq]):JSON.stringify(["event",D(t.event)??t.event])}(e)}`}const C=new WeakMap;function W(t){return C.get(t)??t.scheduledAt}function U(t){if("string"==typeof t)throw new Error(i(70,t))}const J={"@xstate.spawn":t=>t.system.spawnActor(t._parent,t),"@xstate.start":t=>t.system.startActor(t),"@xstate.raise":(t,e,s)=>t.system.scheduleTimer(t.self,N({id:s.id,seq:s.seq,event:e}),s?.delay??0),"@xstate.sendTo":(t,e,s,n)=>(U(s),void 0!==n?.delay?t.system.scheduleTimer(t.self,N({id:n.id,seq:n.seq,event:s}),n?.delay??0):t.system._relay(t.self,e,s)),"@xstate.cancel":(t,e)=>t.system.cancelTimer(t.self,e),"@xstate.stop":(t,e)=>t.system.stopActor(e),"@xstate.terminate":(t,e)=>t.system.terminateActor(t,e)},H=new WeakMap;function B(t,e){let s=H.get(t);if(!s){s=new Map;for(const e of Object.keys(t)){const n=t[e];s.has(n)||s.set(n,e)}H.set(t,s)}return s.get(e)}const F=Symbol("xstate.systemLogicMetadata"),X=new WeakMap,G="xstate.attached.forward";function Q(t,e){const s=(t,e)=>[{status:"active",output:void 0,error:void 0,input:t},[]],n={id:e,start:(e,{self:s,system:n})=>{const r=e.input.actor;if(!r||"stopped"===r.getSnapshot().status)return;const o=t(e.input,{self:s,system:n});if(!o)return;let i,c=!1;const a=()=>{c||(c=!0,o.unsubscribe(),i?.unsubscribe())},u=s._parent;u&&(i=u.subscribe({complete:a,error:a})),X.set(s,{unsubscribe:a})},transition:(t,e,s)=>{const n=s.self;return e.type===G?[t,"active"===t.status&&n._parent?[Nt(s,n._parent,e.event)]:[]]:e.type===d?(X.get(n)?.unsubscribe(),X.delete(n),[{...t,status:"stopped",error:void 0},[]]):[t,[]]},initialTransition:s,getInitialSnapshot:(t,e)=>s(e)[0],getPersistedSnapshot:t=>t,restoreSnapshot:t=>t,isInternalEventType:t=>t===G};return Object.defineProperty(n,F,{value:{kind:e}}),n}function Y(t,e,s){"stopped"!==t.getSnapshot().status&&t._parent&&e._relay(t,t,{type:G,event:s()})}function Z(){return Q(({actor:t,eventType:e,mapper:s},{self:n,system:r})=>{const o="*"!==e&&e.endsWith(".*");return t.on(o?"*":e,t=>{o&&!Us(t.type,e)||Y(n,r,()=>s(t))})},"@xstate.listener")}const tt=Z();function et(){return Q(({actor:t,mappers:e},{self:s,system:n})=>{const{done:r,error:o,snapshot:i}=e;return t.subscribe({next:t=>{"done"===t.status&&r?Y(s,n,()=>r(t.output)):"error"===t.status&&o?Y(s,n,()=>o(t.error)):"active"===t.status&&i&&Y(s,n,()=>i(t))},error:t=>{o&&Y(s,n,()=>o(t))},complete:()=>{}})},"@xstate.subscription")}const st=et();function nt(){const t=this.action?.(...this.args);return t}function rt(t=this.source.system){return b(this.source,this.event,t)}function ot(t=(this.target??this.source).system){return t.deadLetter(this.source,this.target,this.event,this.reason,this.detail)}function it(t,e,s,n,r){return{kind:"builtin",type:"@xstate.deadLetter",exec:ot,source:e,target:t.self,event:s,reason:n,detail:r,params:void 0,args:[]}}function ct(t,e,s){return{kind:"builtin",type:"@xstate.deadLetter",exec:ot,source:t.self,target:void 0,event:e,reason:"missingTarget",detail:{targetId:s,error:new Error(i(65,s))},params:void 0,args:[]}}function at(t=this.actor.system){return t.spawnActor(this.source,this.actor)}function ut(t=this.actor.system){if(2!==this.actor._processingStatus)return t.startActor(this.actor)}function ht(t=this.source.system){return t.stopActor(this.actor)}function dt(t=this.actor.system){const e="done"===this.status?{status:"done",output:this.output,error:void 0}:{status:"error",output:void 0,error:this.error};return t.terminateActor(this.actor,e)}function ft(t=this.source.system){return t.scheduleTimer(this.source,N(this),this.delay??0)}function lt(t=this.source.system){return U(this.event),void 0!==this.delay?t.scheduleTimer(this.source,N(this),this.delay):t.sendEvent(this.source,this.target,this.event)}function pt(t=this.source.system){return t.cancelTimer(this.source,z(this))}function yt(t,e,s){if(!Vt(e))return t;if("@xstate.cancel"===e.type){const s=z(e);return t.timers?.some(t=>L(t,s))?{...t,timers:t.timers.filter(t=>!L(t,s))}:t}if("@xstate.raise"!==e.type&&"@xstate.sendTo"!==e.type||void 0===e.delay)return t;let n=t._nextTimerId??0;if(void 0===e.id&&!D(e.event)){e.seq=n++;const t=e.args;t[t.length-1]={...t[t.length-1],seq:e.seq}}const r="@xstate.raise"===e.type||e.target===s.self?"self":e.target,o={...void 0!==e.id&&{id:e.id},...void 0!==e.seq&&{seq:e.seq},delay:e.delay,type:e.type,event:e.event,target:r},i=N(o),c=t.timers??[],a=c.findIndex(t=>L(t,i));return{...t,timers:-1===a?[...c,o]:c.map((t,e)=>e===a?o:t),_nextTimerId:n}}function gt(t,e){return{...t,...e}}function vt(t,e,...s){const n={action:e,args:s};return t.push(n),n}function _t(t,e){const s={...t};for(const t of Object.keys(e))s[t]=Math.max(s[t]??0,e[t]);return s}function mt(t,e,s){if("add"===e.type)return{...t,children:{...t.children,[e.id]:e.actor},...e.counters&&{_nextActorIds:_t(t._nextActorIds,e.counters)}};const n={...t.children};let r=e.actor._parent===s.self;for(const t of Object.keys(n))n[t]===e.actor&&(r=!0,delete n[t]);if(!r)throw new Error(i(47,e.actor.id,s.self.id));return s.system._unregister(e.actor),{...t,children:n}}function xt(t){if("object"==typeof t&&null!==t&&"action"in t&&"function"==typeof t.action)return t}const wt=new WeakMap,bt=()=>({counters:new Map,explicitIds:new Set,stoppedIds:new Set});function St(t){wt.set(t,bt())}function It(t){return t.self._snapshot}function At(t,e,s){return Math.max(e.counters.get(s)??0,It(t)?._nextActorIds?.[s]??0)}function Et(t,e,s){let n=At(t,e,s);const r=It(t)?.children;for(;e.explicitIds.has(`${s}:${n}`)||r?.[`${s}:${n}`];)n++;return n}function kt(t,e,s){const n=wt.get(t)??s??bt(),r=Fs(e),o=Et(t,n,r);return n.counters.set(r,o+1),{id:`${r}:${o}`,counters:{[r]:o+1}}}function Ot(t,e,s){const n=wt.get(t)??s??bt(),r=It(t)?.children?.[e],o=void 0!==r&&!n.stoppedIds.has(e)&&"active"===r.getSnapshot().status;if(n.explicitIds.has(e)||o)throw new Error(i(67,e,t.self.id));n.explicitIds.add(e)}function jt(t,e,s){const n=Gs(e);if(!n)return;n.prefix;const r=wt.get(t)??s??bt(),o=Math.max(At(t,r,n.prefix),n.index+1);return r.counters.set(n.prefix,o),{[n.prefix]:o}}const Tt=new WeakSet;function Mt(t){if("string"==typeof t)throw new Error(i(68,t))}function Rt(t,e,s,n=!1,r=!0){const o=wt.get(t)??bt(),c={cancel:s=>{vt(e,J["@xstate.cancel"],t,"string"==typeof s?{id:s}:s)},emit:t=>{e.push(t)},log:(...s)=>{vt(e,t.logger,...s)},raise:(n,r)=>{Mt(n),void 0!==r?.delay?vt(e,J["@xstate.raise"],t,n,r):s.push(n)},spawn:(s,n)=>{const{logic:c,src:a}=function(t,e){const s=function(t){return t.self.logic?.sources?.actors}(t);if("string"==typeof e){const t=s?.[e];if(!t)throw new Error(i(66,e));return{logic:t,src:e}}return s?{logic:e,src:B(s,e)}:{logic:e,src:void 0}}(t,s);if(!r){const e=Fs(a??c),s=n?.id??`${e}:${Et(t,wt.get(t)??o,e)}`;return function(t){const{id:e,address:s}=t,n={ref:void 0,id:e,address:s,src:t.src,sessionId:void 0,send(){},getSnapshot:()=>x,subscribe:()=>p,on:()=>p,toJSON(){if(void 0===s)throw new Error(i(51,e));return{"@actor":s}}},r=n;return n.ref=r,r}({id:s,address:Tt.has(t)?void 0:`${t.self.address}/${Xs(s)}`,src:a})}let u,h=n?.id;void 0===h?({id:h,counters:u}=kt(t,a??c,o)):(Ot(t,h,o),u=jt(t,h,o));const d=t.system.createActorRef(c,{...n,...void 0!==a&&{src:a},id:h,parent:t.self});return function(t,e,s,n){vt(t,J["@xstate.spawn"],e).childUpdate={type:"add",actor:e,id:s,counters:n}}(e,d,h,u),d},sendTo:(s,n,r)=>{vt(e,J["@xstate.sendTo"],t,s,n,r)},stop:s=>{if(s){const n=s;vt(e,J["@xstate.stop"],t,n).childUpdate={type:"remove",actor:n},function(t,e){const s=wt.get(t);s&&(s.stoppedIds.add(e.id),s.explicitIds.delete(e.id))}(t,n)}}};n&&Object.assign(c,{listen:(s,n,o)=>{if(!r)return{id:void 0};const i={actor:s,eventType:n,mapper:o},c=t.system.createActorRef(tt,{input:i,parent:t.self});return vt(e,J["@xstate.spawn"],c),c},subscribeTo:(s,n)=>{if(!r)return{id:void 0};const o={actor:s,mappers:"function"==typeof n?{snapshot:n}:n},i=t.system.createActorRef(st,{input:o,parent:t.self});return vt(e,J["@xstate.spawn"],i),i}});let a=!1;const u=t=>(...e)=>a?Kt():t(...e);for(const t of Object.keys(c))c[t]=u(c[t]);const h=Wt(c,u((t,...s)=>{"function"==typeof t&&vt(e,t,...s)}));return Pt.set(h,()=>{a=!0}),h}const Pt=new WeakMap;function $t(t){Pt.get(t)?.()}function Kt(){}function qt(t,e){switch(t){case J["@xstate.spawn"]:{const[t]=e;return{type:"@xstate.spawn",kind:"builtin",exec:at,source:t._parent,actor:t,id:t.id,logic:t.logic,src:t.src,input:t.options?.input}}case J["@xstate.raise"]:{const[,t,s]=e;return{type:"@xstate.raise",kind:"builtin",exec:ft,source:e[0].self,event:t,id:s?.id,delay:s?.delay}}case J["@xstate.sendTo"]:{const[,t,s,n]=e;return{type:"@xstate.sendTo",kind:"builtin",exec:lt,source:e[0].self,target:t,event:s,id:n?.id,delay:n?.delay}}case J["@xstate.cancel"]:{const[,t]=e;return{type:"@xstate.cancel",kind:"builtin",exec:pt,source:e[0].self,...t}}case J["@xstate.stop"]:{const[,t]=e;return{type:"@xstate.stop",kind:"builtin",exec:ht,source:e[0].self,actor:t,id:t.id}}default:return}}function Dt(t){const e=[t];return{kind:"builtin",exec:ut,type:"@xstate.start",source:t._parent,params:void 0,args:e,actor:t,id:t.id}}function Nt(t,e,s){const n=[t,e,s,{}];return{kind:"builtin",exec:lt,type:"@xstate.sendTo",source:t.self,target:e,event:s,id:void 0,delay:void 0,params:void 0,args:n}}function zt(t,e){if("done"!==e.status&&"error"!==e.status)throw new Error(i(69,t.self.id,e.status));const s="done"===e.status?{status:"done",output:e.output,error:void 0}:{status:"error",output:void 0,error:e.error},n=[t.self,s];return{kind:"builtin",exec:dt,type:f,source:t.self,actor:t.self,id:t.self.id,...s,params:void 0,args:n}}function Lt(t,e,[s,n]){const r="done"===s.status||"error"===s.status,o="done"===e?.status||"error"===e?.status,i=n.some(t=>"object"==typeof t&&null!==t&&"type"in t&&t.type===f);return!r||o||i?[s,n]:[s,[...n,zt(t,s)]]}function Vt(t){return"builtin"===t.kind}function Ct(t,e,s,n){let r=t;const o=[];for(const t of n){const e="function"==typeof t,n=xt(t);let i=n?.args;if(n?.action===J["@xstate.sendTo"]&&(void 0===n.args[1]||"string"==typeof n.args[1])){const t=n.args[1],e=void 0!==t&&Object.hasOwn(r.children,t)?r.children[t]:void 0;if(!e){o.push(ct(s,n.args[2],t));continue}i=[n.args[0],e,...n.args.slice(2)]}const c=e?t:!!n&&n.action.bind(null,...i);let a;if("object"==typeof t&&null!==t){const{type:e,childUpdate:s,...n}=t;a=n}if(n?.childUpdate&&(r=mt(r,n.childUpdate,s)),!c||!("resolve"in c)){const c="object"==typeof t&&null!==t&&"action"in t&&"function"==typeof t.action?qt(t.action,i):void 0,u="object"==typeof t&&null!==t&&!n,h={kind:c?"builtin":u?"emit":"action",type:"object"==typeof t?"action"in t&&"function"==typeof t.action?t.action.name??"(anonymous)":t.type:t.name||"(anonymous)",params:c?void 0:a,args:"object"==typeof t&&"action"in t?i:[],...c?{}:u?{source:s.self,event:t}:{action:n?.action??(e?t:void 0)},...c?{}:{exec:u?rt:nt},...c};r=yt(r,h,s),o.push(h);continue}}return[r,o]}function Wt(t,e){const s=(t,...s)=>{e(t,...s)};return Object.assign(s,{cancel:Ut,emit:Ut,log:Ut,raise:Ut,spawn:Jt,sendTo:Ut,stop:Ut,listen:Jt,subscribeTo:Jt,...t}),s}const Ut=()=>{},Jt=()=>({});let Ht;function Bt(){return Ht}const Ft=new WeakMap,Xt=["runtime","_timerMap","_rejectionListeners","_inspectionObservers","_emissionListeners"],Gt=new WeakMap;let Qt;const Yt=new WeakMap,Zt=new WeakMap,te=new Map;function ee(t){const e=t._peekKeyedActors;return e?e.call(t)??te:t.keyedActors}function se(t){t._snapshotVersion++}function ne(t){return t._snapshotVersion}function re(t,e,s,n){const r=Ft.get(t),o=r?.executionSystem??t,c=r?.rootSystem??t,a=void 0!==n&&Gt.get(o)!==n,u=!!n&&!!n.inspect,h=function(t,e){if(e){const t=new Map(e.children);return e.root?.sessionId&&t.set(e.root.sessionId,e.root),t}return new Map(t.children)}(t,s),d=new Map(s?.keyedActors??ee(t)),f=new WeakMap,l=()=>Ft.get(y).executionSystem,p=()=>Ft.get(y).forwardInspection,y=Object.assign(Object.create(o),{children:h,keyedActors:d,reverseKeyedActors:f,_snapshot:{...s?.snapshot??t._snapshot},_snapshotVersion:s?.sourceVersion??t._snapshotVersion,_register:(t,e)=>(h.set(t,e),se(y),t),_unregister:t=>{t.sessionId&&h.delete(t.sessionId);const e=f.get(t);void 0!==e&&d.get(e)===t&&d.delete(e),f.delete(t),se(y)},_set:(t,e)=>{const s=d.get(t);if(s&&s!==e)throw new Error(i(45,String(t)));d.set(t,e),f.set(e,t),se(y)},get:t=>d.get(t),getAll:()=>Object.fromEntries(d),onEmitted:t=>c.onEmitted.call(l(),t),_notifyEmitted:(t,e)=>c._notifyEmitted.call(l(),t,e),_hasInspectionObservers:()=>p()&&(c._hasInspectionObservers?.call(l())??!1),_sendInspectionEvent:t=>{p()&&c._sendInspectionEvent.call(l(),t)}}),g=a?y:o;if(a)!function(t,e,s){const n=e._timerMap;for(const e of Xt)Object.defineProperty(t,e,{configurable:!0,writable:!0,value:"runtime"===e?s.runtime:"_timerMap"===e&&n?{...n}:void 0});Gt.set(t,s),s.inspect&&t.inspect(s.inspect)}(y,o,n);else for(const t of Xt)Object.defineProperty(y,t,{configurable:!0,get:()=>l()[t],set:e=>{l()[t]=e}});Ft.set(y,{executionSystem:g,rootSystem:c,forwardInspection:u});for(const[t,e]of d)e&&f.set(e,t);for(const t of Object.values(e)){if(!t)continue;t.sessionId&&h.set(t.sessionId,t);const e=t.registryKey;e&&(d.set(e,t),f.set(t,e))}return y}function oe(){const t=Qt;return Qt=void 0,t}function ie(t){const e=Yt.get(t);if(e)return e;const s=Zt.get(t);if(!s)return;const n=s();return Yt.set(t,n),Zt.delete(t),n}function ce(t,e){const s=Yt.get(t);if(s)return Yt.set(e,s),!0;const n=Zt.get(t);return!!n&&(Zt.set(e,n),!0)}function ae(t,e,s=e.system,n){Zt.delete(t);const r=ne(s),o=n?ie(n):void 0,i=o?.actor===e?o:ie(t);if(i?.actor===e&&i.systemState.sourceSystem===s&&i.systemState.sourceVersion===r)return void Yt.set(t,i);const c=function(t){const e=t._getRootActor?.(),s=t._peekChildren?.();if(!e&&!s){if(t._getRootActor&&t._peekChildren)return{};const e=new Map(t.children);return e.size?{children:e}:{}}return s?.size?{children:new Map(t.children)}:e?{root:e}:{}}(s);let a=c.children;const u=ee(s);let h=u.size?new Map(u):te;for(const e of Object.values(function(t){return"children"in t&&t.children?t.children:{}}(t))){if(!e)continue;e.sessionId&&(a??=new Map).set(e.sessionId,e);const t=e.registryKey;t&&(h===te&&(h=new Map),h.set(t,e))}Yt.set(t,{actor:e,systemState:{...c,children:a,keyedActors:h,snapshot:{...s._snapshot},sourceSystem:s,sourceVersion:r}})}const ue=Symbol();function he(t){return!!t[ue]}function de(t){return he(t)?t._parent:t.self._parent}function fe(t,e){if(!he(e)){const s=t,n=e.self;return s.self=n,s.system=e.system,s.parent=n._parent,s}return Object.defineProperties(t,{self:{enumerable:!0,get:()=>e.self},system:{enumerable:!0,get:()=>e.system},parent:{enumerable:!0,get:()=>de(e)}}),t}function le(t,e,s=!0){const n=me(t);if(n){n.snapshot=e;const t=n.materialized??n.identityScope;t&&(t.self._snapshot=e)}else t.self._snapshot=e;return s&&e&&"object"==typeof e&&ae(e,t.self),e}function pe(t){return!!me(t)||!!t.self.options?._inert}function ye(t,e){le(t,e,!1);const s=me(t);_e.set(e,t);const n=()=>(ae(e,t.self,t.system),ie(e));return s&&(s.materialized?s.identityProvider=n:s.identityProvider??=n),function(t,e){Yt.delete(t),Zt.set(t,e)}(e,n),e}function ge(t,e){return t.sourceRef=void 0,t.sourceChildren={},t.materialized=e,t.materialize=()=>e,e}const ve=Symbol(),_e=new WeakMap;function me(t){return t[ve]}function xe(t){return me(t).materialize()}const we={[ue]:!0,get _parent(){const t=me(this);return t.parentKnown?t.parent:xe(this).self._parent},get self(){return xe(this).self},get defer(){return xe(this).defer},get id(){return xe(this).id},get logger(){return xe(this).logger},get sessionId(){return xe(this).sessionId},get stopChild(){return xe(this).stopChild},get system(){return xe(this).system},get emit(){return xe(this).emit},get actionExecutor(){return xe(this).actionExecutor}};function be(t,e,s,n,r,o){const i=e?.(),c=r??i?.actor,a=c?.system,u=c&&a?re(a,s,r?void 0:i?.systemState,o):void 0,h=ln(t,{_inert:!0,...c?{id:c.id,_sessionId:c.sessionId}:{},...u?{_systemRef:{current:u}}:{}});c?._parent&&(h._parent=c._parent,h._address=void 0);const d=Object.create(h._actorScope,{defer:{value:()=>{}},stopChild:{value:t=>t._stop()},actionExecutor:{value:()=>{}},_snapshot:{enumerable:!0,configurable:!0,get:()=>h._snapshot,set:t=>{h._snapshot=t}}});return n&&(d._snapshot=n),d}function Se(t,e,s,n){const r=n??(e&&"object"==typeof e?_e.get(e):void 0),o=r?me(r):void 0,i=e&&"object"==typeof e?function(t){return Yt.get(t)}(e):void 0,c=o?.identityProvider??(e&&"object"==typeof e?function(t){const e=Yt.get(t);return e?()=>e:Zt.get(t)}(e):void 0),a={snapshot:e,sourceRef:c,sourceChildren:ms(e)?e.children:{},identityProvider:o?.identityProvider??c,parent:s?._parent??o?.parent??i?.actor._parent,owner:n?o?.owner:oe(),parentKnown:!(!s&&!o?.parentKnown&&!i&&e),materialize:()=>{const e=be(t,a.sourceRef,a.sourceChildren,a.snapshot,s,a.owner);return ge(a,e)}};if(!a.identityProvider&&!e){const e={};let n;const r=()=>(a.identityScope||(a.identityScope=be(t,a.sourceRef,a.sourceChildren,void 0,s,a.owner),void 0!==a.snapshot&&(a.identityScope.self._snapshot=a.snapshot)),a.identityScope);a.materialize=()=>{const t=r();return void 0!==a.snapshot&&(t.self._snapshot=a.snapshot),ge(a,t)},a.identityProvider=()=>{if(n)return n;const t=r();return ae(e,t.self,t.system),n=ie(e)}}const u=Object.create(we);return u[ve]=a,u}function Ie(t,e){if("string"!=typeof t)return t;const s=e[t];if(void 0!==s)return s;const n=function(t){const e=t.trim(),s=e.match(/^(\d+)ms$/i);if(s)return parseInt(s[1],10);const n=e.match(/^(\d*)(\.?)(\d*)s$/i);if(n){const t=n[1]?parseInt(n[1],10):0,e=!!n[2],s=n[3]?parseInt(n[3].padEnd(3,"0").slice(0,3),10):0;return 1e3*t+(e?s:0)}const r=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(!r?.groups)return;const{weeks:o,days:i,hours:c,minutes:a,seconds:u}=r.groups;if(!(o||i||c||a||u))return;const h=t=>t?Number(t.replace(",",".")):0;return 7*h(o)*24*60*60*1e3+24*h(i)*60*60*1e3+60*h(c)*60*1e3+60*h(a)*1e3+1e3*h(u)}(t);if(void 0===n)throw new Error(i(9,t));return n}function Ae(t,e,s){if("function"==typeof t)return t(s);const n=Ie(t,e);return"function"==typeof n?n(s):n}function Ee(t,e){return t._stateInputs?.[e]}function ke(t){return"atomic"===t.type||"final"===t.type||"choice"===t.type}function Oe(t){return Object.values(t.states).filter(t=>"history"!==t.type)}function je(t,e){const s=[];if(e===t)return s;let n=t.parent;for(;n&&n!==e;)s.push(n),n=n.parent;return s}function Te(t){const e=new Set(t),s=new Set;for(const t of e)t.parent&&s.add(t.parent);for(const t of e)if("compound"!==t.type||s.has(t)){if("parallel"===t.type)for(const s of Oe(t))if(!e.has(s)){const t=Ue(s);for(const s of t)e.add(s)}}else for(const s of Ue(t))e.add(s);for(const t of e){let s=t.parent;for(;s&&!e.has(s);)e.add(s),s=s.parent}return e}function Me(t,e){const s=Te(e),n=new Map;for(const t of s)n.has(t)||n.set(t,[]),t.parent&&(n.has(t.parent)||n.set(t.parent,[]),n.get(t.parent).push(t));const r=t=>{const e=n.get(t);if(!e)return{};if("compound"===t.type){const t=e[0];if(!t)return{};if(ke(t))return t.key}const s={};for(const t of e)s[t.key]=r(t);return s};return r(t)}function Re(t,e){return"compound"===e.type?Oe(e).some(e=>"final"===e.type&&t.has(e)):"parallel"===e.type?Oe(e).every(e=>Re(t,e)):"final"===e.type}const Pe=t=>"#"===t[0];function $e(t,e,s){const n=e.children[s];return!n||!("sessionId"in t)||(void 0===n.sessionId||n.sessionId===t.sessionId)}function Ke(t,e,s,n){const r=t.entry;t.entry=(t,e)=>{const o="function"==typeof r?r(t,e):void 0,i=void 0!==o?.context?gt(t.context,o.context):t.context;return e.raise(s(t),{delay:n({context:i,event:t.event,delays:t.delays,input:t.input})}),o};const o=t.exit;t.exit=(t,s)=>(s.cancel({event:D(e)}),"function"==typeof o?o(t,s):void 0)}function qe(t,e,s){const n=function(t){const{cond:e,actions:s,guard:n}=t;if(void 0!==e)return'"cond"';if(void 0!==s)return'"actions"';if(void 0!==n&&"function"!=typeof n)return'an object-form "guard"';return}(s);if(n)throw new Error(i(10,e||"always",t.id,n));const r=Ls(s.target),o=s.reenter??!1,c=Ve(t,r);Le(t,c);const a={...s,target:c,source:t,reenter:o,eventType:e,toJSON:()=>De(a)};return a}function De(t){const{definition:e,...s}=t;return{...s,source:`#${t.source.id}`,target:t.target?.map(t=>`#${t.id}`)}}function Ne(t,e){for(let s=t.parent;s;s=s.parent)if(s===e)return!0;return!1}function ze(t,e){const s=new Set;for(let e=t;e;e=e.parent)s.add(e);for(let t=e;t;t=t.parent)if(s.has(t))return t}function Le(t,e){if(e&&!(e.length<2))for(let s=0;s<e.length;s++)for(let n=s+1;n<e.length;n++){const r=e[s],o=e[n],c=ze(r,o);if(r===o||Ne(r,o)||Ne(o,r)||"parallel"!==c?.type)throw new Error(i(11,t.id))}}function Ve(t,e){if(void 0!==e)return e.map(e=>{if("string"!=typeof e)return e;if(Pe(e))return t.machine.getStateNodeById(e);const s="."===e[0];if(s&&!t.parent)return He(t,e.slice(1));const n=s?t.key+e:e;if(!t.parent)throw new Error(i(13,e));try{return He(t.parent,n)}catch(e){throw new Error(i(12,t.id,e.message))}})}function Ce(t){const e=Ls(t.config.target);if(!e)return"parallel"===t.parent.type?{target:[t.parent],source:t,reenter:!1,eventType:""}:t.parent.initial;const s=e.map(e=>"string"==typeof e?He(t.parent,e):e);return Le(t,s),{target:s,source:t,reenter:!1,eventType:"",to:t.config._historyDefaultTransition}}function We(t){return"history"===t.type}function Ue(t){const e=new Set;!function t(s){if(!e.has(s))if(e.add(s),"compound"===s.type)for(const e of s.initial.target??[])t(e);else if("parallel"===s.type)for(const e of Oe(s))t(e)}(t);for(const s of e)for(const n of je(s,t))e.add(n);return e}function Je(t,e){const s=t.states?.[e];if(!s)throw new Error(i(14,e,t.id));return s}function He(t,e){if("string"==typeof e&&Pe(e))return t.machine.getStateNodeById(e);const s=Rs(e).slice();let n=t;for(;s.length;){const t=s.shift();if(!t.length)break;n=Je(n,t)}return n}function Be(t,e){if("string"==typeof e){const s=t.states[e];if(!s)throw new Error(i(14,e,t.id));return[t,s]}const s=Object.keys(e),n=new Array(s.length),r=[t.machine.root,t];for(let e=0;e<s.length;e++){const o=Je(t,s[e]);n[e]=o,r.push(o)}for(let t=0;t<s.length;t++)r.push(...Be(n[t],e[s[t]]));return r}function Fe(t,e,s,n,r,o){if("string"==typeof e){const i=Je(t,e).next(s,n,r,o);return i&&i.length?i:t.next(s,n,r,o)}const i=Object.keys(e),c=i[0];if(1===i.length){const i=Fe(Je(t,c),e[c],s,n,r,o);return i&&i.length?i:t.next(s,n,r,o)}const a=[];for(const c of i){const i=e[c];if(!i)continue;const u=Fe(Je(t,c),i,s,n,r,o);u&&a.push(...u)}return a.length?a:t.next(s,n,r,o)}function Xe(t,e){let s=t;for(;s.parent&&s.parent!==e;)s=s.parent;return s.parent===e}function Ge(t,e){for(const s of t)if(Xe(s,e))return!0;return!1}function Qe(t,e){const s=t instanceof Set?t.size:Array.isArray(t)?t.length:void 0,n=e instanceof Set?e.size:Array.isArray(e)?e.length:void 0;void 0!==s&&void 0!==n&&n<s&&([t,e]=[e,t]);const r=t instanceof Set?t:new Set(t);for(const t of e)if(r.has(t))return!0;return!1}function Ye(t,e,s,n){const r=new Set,o=new Map,i=t=>{let r=o.get(t);return r||(r=ns([t],e,s,n),o.set(t,r)),r};for(const e of t){let t=!1;const s=new Set;for(const n of r)if(Qe(i(e),i(n))){if(!Xe(e.source,n.source)){t=!0;break}s.add(n)}if(!t){for(const t of s)r.delete(t);r.add(e)}}return Array.from(r)}function Ze(t,e,s,n,r){let o;return(i,c)=>{if(!i.to&&!c)return{targets:i.target,context:void 0,reenter:i.reenter,actions:void 0,internalEvents:void 0,input:void 0};let a=o?.get(i);return a||(a=os(i,t,e,s,{resolveActions:n,selectionResult:r?.get(i)}),(o??=new Map).set(i,a)),a}}function ts(t,e,s){const n=e.historyValue,{targets:r}=s(t);if(!r)return[];const o=new Set;for(const t of r)if(We(t))if(n[t.id])for(const e of n[t.id])o.add(e);else for(const n of ts(Ce(t),e,s))o.add(n);else o.add(t);return[...o]}function es(t,e){let s=t;for(;"parallel"===s.type;){const t=Oe(s).find(t=>e.every(e=>e===t||Xe(e,t)));if(!t)break;s=t}return s}function ss(t,e,s){const n=ts(t,e,s),{targets:r,reenter:o}=s(t);if(t._transitionDomain){if("internal"===t._transitionDomain&&"compound"===t.source.type&&n.every(e=>Xe(e,t.source)))return t.source;const[e,...s]=n.concat(t.source);for(const t of je(e,void 0))if("compound"===t.type&&s.every(e=>Xe(e,t)))return t;return}if(!(r?.some(We)&&n.some(e=>e===t.source))&&n.every(e=>e===t.source||Xe(e,t.source)))return o?t.source:es(t.source,n);const[i,...c]=n.concat(t.source);for(const t of je(i,void 0))if(c.every(e=>Xe(e,t)))return o?t:es(t,n);return o?void 0:es(t.source.machine.root,n)}function ns(t,e,s,n){const r=new Set;for(const o of t){const{targets:t,reenter:i}=n(o);if(t?.length){const t=ss(o,s,n);i&&o.source===t&&r.add(t);for(const s of e)Xe(s,t)&&r.add(s)}}return[...r]}function rs(t,e,s,n,r,o,c){const a=[],u=e.machine._microstepHooks?.drain(s.self);if(u?.length&&o.push(...u),!t.length)return[e,a];{const u=new Set(e.nodes);let h=e.historyValue;const d=e.context,f=1===t.length?t:Ye(t,u,e,Ze(e,n,s,!1,c)),l=Ze(e,n,s,!0,c),p=f.some(t=>{const{targets:e,reenter:s}=l(t);return!!e?.length||!!s}),y=(t,r,o,i,c,a)=>{const u=[],h=[];let d;const f=Rt(s,u,h,!0),l=he(s)?fe({context:r,event:n,output:Ds(n),children:o,actions:e.machine.sources.actions,actors:e.machine.sources.actors,guards:e.machine.sources.guards,delays:e.machine.sources.delays,input:i,stateNode:a},s):{context:r,event:n,output:Ds(n),parent:s.self._parent,self:s.self,children:o,system:s.system,actions:e.machine.sources.actions,actors:e.machine.sources.actors,guards:e.machine.sources.guards,delays:e.machine.sources.delays,input:i,stateNode:a};let p;try{p=t(l,f)}finally{$t(f)}return us(p,n,a.id,c),void 0!==p?.context&&(d=gt(r,p.context)),[u,d,h,p?.children]};let g=e;const v=(t,e)=>{const n=[],r=Rt(s,n,[]);for(const s of t.invoke){const t=e[s.id];t&&r.stop(t)}return n},_=()=>{const t=ns(f,u,e,l);let n;t.sort((t,e)=>e.order-t.order);const r=[...u];for(const e of t)for(const t of Object.values(e.states)){if("history"!==t.type)continue;const s="deep"===t.history?t=>ke(t)&&Xe(t,e):t=>t.parent===e;n??={...h},n[t.id]=r.filter(s)}for(const n of t){const t=Ee(e,n.id),[r,i,c,h]=n.exit?y(n.exit,g.context,g.children,t,"exit",n):[[],void 0,void 0,void 0];c?.length&&o.push(...c),(i||h)&&(g=Os(g,{...i?{context:i}:{},...h?{children:h}:{}}));const[d,f]=Ct(g,0,s,r);g=d,a.push(...f);const l=v(n,g.children);if(l.length){const[t,e]=Ct(g,0,s,l);g=t,a.push(...e)}u.delete(n)}h=n||h};!r&&p&&_();let m=g.context;const x=[],w=[],b=[];for(const t of f){const e=l(t,!0);if(t.to){const s={...t,target:e.targets,definition:t};s.toJSON=()=>De(s),b.push(s)}else b.push(t);void 0!==e.context&&(m=gt(m,e.context)),e.actions&&x.push(...e.actions),e.internalEvents&&w.push(...e.internalEvents)}w.length&&o.push(...w);const S=()=>{const t=t=>{const e=g.machine.root;let r;if(void 0!==t.output&&t.parent){const i=o.find(t=>"xstate.done.state"===t.type&&t.stateId===e.id);r=i&&t.parent===e?i.output:qs(t.output,g.context,n,s.self,x[t.id])}else if("parallel"===t.type){const e=o.find(e=>"xstate.done.state"===e.type&&e.stateId===t.id);r=e?.output}return void 0===e.output?t.parent===e?r:void 0:qs(e.output,g.context,j(t.id,r),s.self)},c=new Set,d=new Set,p=new Map,v=(t,e)=>{for(const s of t)if(e&&!Xe(s,e)||c.add(s),"parallel"===s.type)for(const t of Oe(s))Ge(c,t)||(c.add(t),_(t))},_=t=>{if(We(t))if(h[t.id]){const e=h[t.id];for(const t of e)c.add(t),_(t);for(const s of e)v(je(s,t.parent),void 0)}else{const e=Ce(t),s=t.parent;d.add(s);const n=p.get(s)??[];n.push(e),p.set(s,n);const{targets:r}=l(e);for(const s of r??[])c.add(s),e===t.parent?.initial&&d.add(t.parent),_(s);for(const e of r??[])v(je(e,t.parent),void 0)}else{if("compound"===t.type){const e=l(t.initial).targets??[];for(const t of e)We(t)||c.add(t);d.add(t);for(const t of e)_(t);for(const s of e)v(je(s,t),void 0);return}if("parallel"===t.type)for(const e of Oe(t))Ge(c,e)||(c.add(e),d.add(e),_(e))}},m=new Map;for(const t of f){const s=ss(t,e,l),{targets:n,reenter:r}=l(t),o=new Set;m.set(t,o);for(const e of n??[])We(e)||t.source===e&&t.source===s&&!r||(o.add(e),c.add(e),d.add(e)),_(e);const i=ts(t,e,l);for(const e of i){const n=je(e,s);"parallel"===s?.type&&n.push(s),v(n,!t.source.parent&&r?void 0:s)}n?.length&&r&&s===t.source&&c.add(t.source)}if(r)for(const t of f[0]._statesForDefaultEntry??[e.machine.root])d.add(t);const x={};let w=!1;const b=new Set([...u].map(t=>t.id));for(const[t,s]of Object.entries(e._stateInputs??{}))b.has(t)?x[t]=s:w=!0;for(const t of f){const{targets:e,input:s}=l(t,!0);if(s&&e)for(const n of e.filter(e=>m.get(t)?.has(e)))x[n.id]=s,w=!0}const S=new Set;for(const r of[...c].sort((t,e)=>t.order-e.order)){u.add(r);const c=[];let h=g.children,f=!1;for(const t of"final"===r.type?[]:r.invoke){f=!0;let o=t.logic;"function"==typeof o&&(o=o({actors:e.machine.sources.actors,context:g.context,event:n,self:s.self}));const a="string"==typeof o?e.machine.sources.actors[o]:o;if(!a)throw new Error(i(15,"string"==typeof o?o:"inline"));const u="function"==typeof t.input?t.input({self:s.self,context:g.context,event:n,input:x[r.id],output:Ds(n)}):t.input;Ot(s,t.id);const d=s.system.createActorRef(a,{...t,src:"string"==typeof o?o:void 0,input:u,parent:s.self,syncSnapshot:!!t.onSnapshot});d._invoked=!0,c.push({action:J["@xstate.spawn"],args:[d]}),t.id&&(h={...h,[t.id]:d})}f&&(g=Os(g,{children:h}));let l=g.context,v=!1;const _=x[r.id];if(r.entry){const[t,e,s,n]=y(r.entry,l,h,_,"entry",r);c.push(...t),s?.length&&o.push(...s),e&&(l=e,v=!0),n&&(g=Os(g,{children:n}))}if(v&&(g=Os(g,{context:l})),d.has(r)){const t=[r.initial,...p.get(r)??[]].filter(Boolean);for(const e of t){const{actions:t,context:r,input:i,internalEvents:a}=os(e,g,n,s);if(t&&c.push(...t),a?.length&&o.push(...a),void 0!==r&&(g=Os(g,{context:gt(g.context,r)})),i&&e.target)for(const t of e.target)x[t.id]=i,w=!0}}const[m,b]=Ct(g,0,s,c);if(g=m,c.length=0,a.push(...b),"final"!==r.type)continue;const I=r.parent;let A="parallel"===I?.type?I:I?.parent,E=A||r;for("compound"===I?.type&&o.push(j(I.id,void 0!==r.output?qs(r.output,g.context,n,s.self,_):void 0));"parallel"===A?.type&&!S.has(A)&&Re(u,A);){S.add(A);const t={};for(const e of Oe(A)){if("final"===e.type){t[e.key]=void 0!==e.output?qs(e.output,g.context,n,s.self,x[e.id]):void 0;continue}if("parallel"===e.type){const s=o.find(t=>"xstate.done.state"===t.type&&t.stateId===e.id);t[e.key]=s?.output;continue}const r=Oe(e).find(t=>"final"===t.type&&u.has(t));t[e.key]=void 0!==r?.output?qs(r.output,g.context,n,s.self,x[r.id]):void 0}o.push(j(A.id,t)),E=A,A=A.parent}A||(g=Os(g,{status:"done",output:t(E)}))}w&&(g=Os(g,{_stateInputs:x}))},[I,A]=Ct(g,0,s,x);g=I,a.push(...A),m&&m!==e.context&&(g=Os(g,{context:m}));const E=g.machine._microstepHooks?.transitionContent?.(f,[...u]);if(E){const t=f[0].source,[e,n,r,i]=y(E,g.context,g.children,Ee(g,t.id),"transition",t);r?.length&&o.push(...r),(n||i)&&(g=Os(g,{...n?{context:n}:{},...i?{children:i}:{}}));const[c,u]=Ct(g,0,s,e);g=c,a.push(...u)}(r||p)&&S();const k=[...u];if("done"===g.status){const t=[];k.sort((t,e)=>e.order-t.order).forEach(e=>{if(e.exit){const s=Ee(g,e.id),[n,r,i,c]=y(e.exit,g.context,g.children,s,"exit",e);(r||c)&&(g=Os(g,{...r?{context:r}:{},...c?{children:c}:{}})),t.push(...n),i?.length&&o.push(...i)}});const[e,r]=Ct(g,0,s,t);g=e,a.push(...r);const[i,c]=ds(g,n,s);g=i,a.push(...c);const[u,h]=fs(g,n,s);g=u,a.push(...h)}return h===e.historyValue&&e.nodes.length===u.size&&e.nodes.every(t=>u.has(t))?g.context!==d?[g===e?Os(g):g,a,b]:[g,a,b]:[Os(g,{_nodes:k,historyValue:h}),a,b]}}function os(t,e,s,n,r){let o;const i=()=>o??=he(n)?fe({context:e.context,event:s,output:Ds(s),value:e.value,children:e.children,actions:e.machine.sources.actions,actors:e.machine.sources.actors,guards:e.machine.sources.guards,delays:e.machine.sources.delays,input:Ee(e,t.source.id)},n):{context:e.context,event:s,output:Ds(s),value:e.value,children:e.children,system:n.system,parent:n.self._parent,self:n.self,actions:e.machine.sources.actions,actors:e.machine.sources.actors,guards:e.machine.sources.guards,delays:e.machine.sources.delays,input:Ee(e,t.source.id)};if(t.to){const o=[],c=[];let a;if(!r?.selectionResult?.reusable||!1!==r.resolveActions&&0!==r.selectionResult.operations.length){const e=Rt(n,o,c,!0,r?.resolveActions??!0),u=!1===r?.resolveActions?q(de(n),"suppress"):void 0;try{if(r?.selectionResult?.reusable){a=r.selectionResult.result;for(const t of r.selectionResult.operations)t(e)}else a=t.to(i(),e),us(a,s,t.source.id)}finally{u?.close(),$t(e)}}else a=r.selectionResult.result;const u=a?.target?Ve(t.source,Ks(a.target)):void 0,h=a?.input??("function"==typeof t.input?t.input({context:e.context,event:s,output:Ds(s)}):t.input);return{targets:u,context:a?.context,reenter:a?.reenter,actions:o,internalEvents:c,input:h}}const c="function"==typeof t.input?t.input({context:e.context,event:s,output:Ds(s)}):t.input,a="function"==typeof t.context?t.context(i()):t.context;return{targets:t.target,context:a,reenter:t.reenter,actions:void 0,internalEvents:void 0,input:c}}class is extends Error{constructor(t,e,s,n){super(i(16,t,e.type,s)),this.actorId=void 0,this.event=void 0,this.maxIterations=void 0,this.states=void 0,this.name="InfiniteTransitionError",this.actorId=t,this.event=e,this.maxIterations=s,this.states=n.slice()}}function cs(t,e,s,n,r=[]){if("active"!==t.status&&!r.length)return{snapshot:t,microsteps:[[t,[],[]]]};let o=t;const i=r.slice();let c=!1;function a(t){if("xstate.done.actor"!==t.type&&"xstate.error.actor"!==t.type)return;const{actorId:e,sessionId:n}=t;if(!e)return;const r=o.children[e];if(!r)return;if(void 0!==r.sessionId&&r.sessionId!==n)return;const c={...o.children};if(delete c[e],s.system._unregister(r),o=Os(o,{children:c}),i.length){const[,t,e]=i.at(-1);i[i.length-1]=[o,t,e]}else i.push([o,[],[]])}function f(){if(c&&o===t&&(o=Os(t,{}),i.at(-1)?.[0]===t)){const[,t,e]=i.at(-1);i[i.length-1]=[o,t,e]}const e=function(t){const e=[],s=[];for(const n of t){if(!Vt(n)||n.type!==h)continue;const{actor:t,logic:r}=n,o=Dt(t);r===tt||r===st?e.push(o):s.push(o)}return[...e,...s]}(i.flatMap(([,t])=>t)),n=("active"===t.status||r.length>0)&&("done"===o.status||"error"===o.status);if(e.length||n){const t=n?[...e,zt(s,o)]:e;if(i.length){const[e,s,n]=i.at(-1);i[i.length-1]=[e,[...s,...t],n]}else i.push([o,t,[]])}return{snapshot:o,microsteps:i}}function p(t,n){if(n=t[2]??n,!pe(s)&&(e.type===u||(s.system._hasInspectionObservers?.()??1))){const t=s.self._collectedMicrosteps||[];t.push(...n),s.self._collectedMicrosteps=t}i.push([t[0],t[1],n])}if(e.type===d){const[t,n]=ds(o,e,s),[r,i]=fs(t,e,s);return o=Os(r,{status:"stopped"}),p([o,[...n,...i]],[]),f()}let y=e;if(e.type===l){const t=z(e),r=t?o.timers.findIndex(e=>L(e,t)):-1;if(-1===r)return f();const i=o.timers[r],c=o.timers.filter((t,e)=>e!==r);if(o=Os(o,{timers:c}),"@xstate.raise"===i.type)n.push(i.event),p([o,[]],[]);else{const t="self"===i.target?s.self:i.target;p([o,[Nt(s,t,i.event)]],[])}}if(y.type!==u&&y.type!==l){const e=y,r=function(t){return t.type.startsWith("xstate.error.")}(e),i=new Map,u=o.machine.getTransitionData(o,e,s,i);if(r&&!u.length)return"xstate.error.actor"!==e.type||$e(e,o,e.actorId)?(o=Os(t,{status:"error",error:e.error}),p([o,[]],[]),a(e),f()):f();c=u.length>0;const h=rs(u,t,s,y,!1,n,i);o=h[0],p(h,u),a(e)}if(!n.length&&!1===t.machine._hasEventlessTransitions)return f();let g=e.type!==l;const v=t.machine.options?.maxIterations??1/0;let _=0;const m=[];for(;"active"===o.status;){if(_++,_>v)throw new is(s.self?.id,e,v,m);let t;m.push(o.value),m.length>5&&m.shift();let r=g?ls(o,y,s):[];const i=r.length?o:void 0,c=o.machine._microstepHooks?.drain(s.self);if(c?.length&&n.push(...c),!r.length){if(!n.length)break;y=n.shift(),t=new Map,r=o.machine.getTransitionData(o,y,s,t)}const u=rs(r,o,s,y,!1,n,t);o=u[0],g=o!==i,p(u,r),a(y)}return f()}function as(t,e,s,n,r){return!!t.to&&hs(t.to,e,s,n,r,n.machine.sources,t.source.id).enabled}function us(t,e,s,n){if(t&&"function"==typeof t.then)throw Promise.resolve(t).catch(()=>{}),new Error(i(17,n??"transition",s,e.type))}function hs(t,e,s,n,r,o,i){let c;const a=function(t){const e=q(t),{frame:s}=e,n=[];let r=!1;const o=t=>{r||n.push(t)},i=()=>{if(!r)throw s.requiresReevaluation=!0,K},c=Wt({cancel:(...t)=>o(e=>e.cancel(...t)),emit:(...t)=>o(e=>e.emit(...t)),log:(...t)=>o(e=>e.log(...t)),raise:(t,e)=>{r||(Mt(t),o(s=>s.raise(t,e)))},sendTo:(...t)=>o(e=>e.sendTo(...t)),stop:(...t)=>o(e=>e.stop(...t)),spawn:i,listen:i,subscribeTo:i},(...t)=>o(e=>e(...t)));return{frame:s,enqueue:c,operations:n,close(){r=!0,e.close()}}}(de(r));try{c=t(fe({context:e,event:s,output:Ds(s),value:n.value,children:n.children,actions:o.actions,actors:o.actors,guards:o.guards,delays:o.delays,input:Ee(n,i)},r),a.enqueue)}catch(t){if(t===K)return{enabled:!0,result:void 0,reusable:!1};throw t}finally{a.close()}return us(c,s,i),a.frame.requiresReevaluation?{enabled:!0,result:void 0,reusable:!1}:{enabled:void 0!==c||a.operations.length>0,result:c,reusable:!0,operations:a.operations}}function ds(t,e,s){let n;if(!t.children||0===(n=Object.values(t.children).filter(Boolean)).length)return[t,[]];const r=[],o=Rt(s,r,[]);for(const t of n)o.stop(t);return Ct(t,0,s,r)}function fs(t,e,s){if(!t.timers.length)return[t,[]];const n=[],r=Rt(s,n,[]);for(const e of t.timers)r.cancel(N(e));return Ct(t,0,s,n)}function ls(t,e,s){t.machine._microstepHooks?.begin(s.self);const n=new Set,r=t.nodes.filter(ke);for(const o of r)t:for(let r=o;r;r=r.parent)if(r.always)for(const o of r.always)if(ps(o,e,t,r,s)){n.add(o);break t}return Ye(Array.from(n),new Set(t.nodes),t,Ze(t,e,s,!1))}function ps(t,e,s,n,r,o){if(t.matches&&!function(t,e){return Object.entries(e).every(([e,s])=>Object.is(t[e],s))}(e,t.matches))return!1;if(t._eventMatcher&&!t._eventMatcher(e,s))return!1;if(t.guard){const o=function(t,e){if(!he(e)){const s=e.self;return Object.assign(t,{self:s,parent:s._parent})}return Object.defineProperties(t,{self:{enumerable:!0,get:()=>e.self},parent:{enumerable:!0,get:()=>de(e)}}),t}({context:s.context,event:e,output:Ds(e),children:s.children,actions:n.machine.sources.actions,actors:n.machine.sources.actors,guards:n.machine.sources.guards,delays:n.machine.sources.delays,_snapshot:s},r);if(!t.guard(o))return!1}if(t.to){const i=hs(t.to,s.context,e,s,r,n.machine.sources,t.source.id);return o?.set(t,i),i.enabled}return!0}const ys=Object.freeze({}),gs=Object.freeze([]);function vs(t){return t?.length?t:gs}function _s(t){return t===ys||t&&Object.keys(t).length?t:ys}function ms(t){return!!t&&"object"==typeof t&&"machine"in t&&"value"in t}const xs=function(t){if("string"==typeof t&&Pe(t)){const e=this.machine.getStateNodeById(t);return this.nodes.includes(e)}return Ms(t,this.value)},ws=function(t){return this.tags.has(t)},bs=function(t){return this.machine?._canTransition(this,t)??!1},Ss=function(){const{nodes:t,_stateInputs:e,_nextTimerId:s,_nextActorIds:n,tags:r,machine:o,getMeta:i,getInputs:c,toJSON:a,can:u,hasTag:h,matches:d,...f}=this;return{...f,tags:Array.from(r)}},Is=function(){const t={};for(const e of this.nodes)void 0!==e.meta&&(t[e.id]=e.meta);return t},As=function(){return this._stateInputs};function Es(t){const e=new Set;for(const s of t)for(const t of s.tags)e.add(t);return e}function ks(t,e,s){const n={status:t.status,output:t.output,error:t.error,machine:e,context:t.context,nodes:t._nodes,value:t.value??Me(e.root,t._nodes),tags:Es(t._nodes),children:_s(t.children),timers:vs(t.timers),historyValue:_s(t.historyValue),_stateInputs:_s(t._stateInputs),_nextTimerId:t._nextTimerId??0,_nextActorIds:t._nextActorIds,matches:xs,hasTag:ws,can:bs,getMeta:Is,getInputs:As,toJSON:Ss};return s&&ae(n,s),n}function Os(t,e={}){const s={...t,...e};if((e._nodes??t.nodes)===t.nodes){const e={status:s.status,output:s.output,error:s.error,machine:t.machine,context:s.context,nodes:t.nodes,value:t.value,tags:t.tags,children:_s(s.children),timers:vs(s.timers),historyValue:_s(s.historyValue),_stateInputs:_s(s._stateInputs),_nextTimerId:s._nextTimerId??0,_nextActorIds:s._nextActorIds,matches:xs,hasTag:ws,can:bs,getMeta:Is,getInputs:As,toJSON:Ss};return ce(t,e),e}const n=ks({...s,value:void 0},t.machine);return ce(t,n),n}function js(t){const e={};for(const s in t){const n=t[s];Array.isArray(n)&&(e[s]=n.map(t=>({id:t.id})))}return e}function Ts(t,e,s="context",n=[]){let r;n.push(t);for(const o in t){const c=t[o];if(c&&"object"==typeof c)if(Cs(c))r??=Array.isArray(t)?t.slice():{...t},r[o]={"@actor":c.address};else{const a=Array.isArray(t)?`${s}[${o}]`:`${s}.${o}`;if(n.includes(c))throw new Error(i(44,e(),a));const u=Ts(c,e,a,n);u!==c&&(r??=Array.isArray(t)?t.slice():{...t},r[o]=u)}}return n.pop(),r??t}function Ms(t,e){const s=Ps(t),n=Ps(e);return"string"==typeof n?"string"==typeof s&&n===s:"string"==typeof s?s in n:Object.keys(s).every(t=>t in n&&Ms(s[t],n[t]))}function Rs(t){if(Ns(t))return t;const e=[];let s="";for(let n=0;n<t.length;n++){switch(t.charCodeAt(n)){case 92:s+="."===t[n+1]?"\\.":t[n+1],n++;continue;case 46:e.push(s),s="";continue}s+=t[n]}return e.push(s),e}function Ps(t){if(ms(t))return t.value;if("string"!=typeof t)return t;return function(t){if(1===t.length)return t[0];const e={};let s=e;for(let e=0;e<t.length-1;e++)if(e===t.length-2)s[t[e]]=t[e+1];else{const n=s;s={},n[t[e]]=s}return e}(Rs(t))}function $s(t){return Ns(t)?t:[t]}function Ks(t){return void 0===t?[]:$s(t)}function qs(t,e,s,n,r){if("function"==typeof t){return t({context:e,event:s,output:Ds(s),self:n,input:r})}return t}function Ds(t){if(function(t){return"xstate.done.actor"===t.type||"xstate.done.state"===t.type}(t)){return t.output}}function Ns(t){return Array.isArray(t)}function zs(t,e,s){return $s(t).map(t=>{if("string"==typeof t)throw new Error(i(38,e,s,t));return void 0===t?{target:void 0}:"function"==typeof t?{to:t}:t})}function Ls(t){if(void 0!==t&&""!==t)return Ks(t)}function Vs(t,e,s){const n="object"==typeof t,r=n?t:void 0;return{next:(n?t.next:t)?.bind(r),error:(n?t.error:e)?.bind(r),complete:(n?t.complete:s)?.bind(r),...n&&t.passive?{passive:!0}:{}}}function Cs(t){return"sessionId"in t&&"send"in t&&"ref"in t}function Ws(t,e,s){const n=[];for(const t of e)for(const e of t.invoke)e.id===s&&n.push(e);if(1!==n.length)throw new Error(i(39,s));const r=n[0].logic;if("string"==typeof r){const e=t.sources.actors[r];if(!e)throw new Error(i(29,s,r));return e}if("function"==typeof r)throw new Error(i(40,s));return r}function Us(t,e){if(e===t)return!0;if("*"===e)return!0;if(!e.endsWith(".*"))return!1;const s=e.split("."),n=t.split(".");for(let t=0;t<s.length;t++){const e=s[t],r=n[t];if("*"===e){return t===s.length-1}if(e!==r)return!1}return!0}let Js,Hs=0;function Bs(){return Js??=function(){let t;try{t=globalThis.crypto}catch{}if(t?.getRandomValues)try{const e=new Uint32Array(4);return t.getRandomValues(e),Array.from(e,t=>t.toString(36).padStart(7,"0")).join("")}catch{}if(t?.randomUUID)try{return t.randomUUID().replaceAll("-","")}catch{}return`xstate-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`}(),`${Js}:${(Hs++).toString(36)}`}function Fs(t){if("string"==typeof t)return t;const e=t?.id;return"string"==typeof e&&e.length?e:"x"}function Xs(t){return t.includes("/")||t.includes("%")?t.replaceAll("%","%25").replaceAll("/","%2F"):t}function Gs(t){const e=t.lastIndexOf(":");if(e<=0||e===t.length-1)return;const s=Number(t.slice(e+1));return Number.isSafeInteger(s)&&s>=0?{prefix:t.slice(0,e),index:s}:void 0}function Qs(t,e){return`${t?t.address:""}|${e}`}function Ys(t,e,s){const n=t._snapshot._nextActorIds;(n[e]??0)>=s||(t._snapshot._nextActorIds={...n,[e]:s},se(t))}const Zs=Object.freeze({});function tn(t,e){return V(t,e)}function en(t,e){return t.getSnapshot()?.timers?.find(t=>L(t,e))}class sn{get children(){const t=this._children??=new Map;return this._getRootActor()&&t.set(this._rootActor.sessionId,this._rootActor),t}set children(t){this._children=t}_getRootActor(){return this._rootActor._isRunning()?this._rootActor:void 0}_peekChildren(){return this._children}get keyedActors(){return this._keyedActors??=new Map}set keyedActors(t){this._keyedActors=t}get reverseKeyedActors(){return this._reverseKeyedActors??=new WeakMap}set reverseKeyedActors(t){this._reverseKeyedActors=t}_peekKeyedActors(){return this._keyedActors}constructor(t,e){this._rootActor=t,this._identity={executionId:Bs(),nextSessionId:0},this._snapshot=void 0,this._snapshotVersion=0,this.scheduler=this,this._clock=void 0,this._logger=void 0,this.createActorRef=void 0,this._clock=e.clock,this._logger=e.logger,this.createActorRef=e.createActorRef;const s=Bt();s&&this.inspect(s),this._snapshot={_scheduledTimers:Zs,_nextActorIds:{}}}_recordSent(t,e,s,n,r){if(!this._inspectionObservers?.size||!t)return;const o=t;if(!o._inTransition)return;(o._collectedSent??=[]).push({targetRef:e,targetId:e.id,event:s,delay:n,id:r})}schedule(t,e,s){const n=tn(t,e);void 0!==this._timerMap?.[n]&&this.cancel(t,e);const r=en(t,e);if(r){const e="self"===r.target?t:r.target;this._recordSent(t,e,r.event,s,r.id)}const o=this._clock.now?.()??Date.now(),i={source:t,delay:s,key:e,scheduledAt:o,dueAt:o+s};this._snapshot._scheduledTimers===Zs&&(this._snapshot._scheduledTimers={}),this._snapshot._scheduledTimers[n]=i,se(this);const c=this._clock.setTimeout(()=>{this._timerMap&&delete this._timerMap[n],delete this._snapshot._scheduledTimers[n],se(this),this._deliver(t,t,{type:l,...e})},s);(this._timerMap??={})[n]=c}cancel(t,e){const s=tn(t,e),n=this._timerMap?.[s];this._timerMap&&delete this._timerMap[s],this._snapshot._scheduledTimers!==Zs&&delete this._snapshot._scheduledTimers[s],se(this),void 0!==n&&this._clock.clearTimeout(n)}cancelAll(t){for(const e in this._snapshot._scheduledTimers){const s=this._snapshot._scheduledTimers[e];s.source===t&&this.cancel(t,s.key)}}_deliver(t,e,s){!function(t,e,s){if(g(e)){const n=e,r=t?.system,o=r&&m(r,n);if(!o){const e=_(n);if(!r)throw e;return void r.deadLetter(t,void 0,s,"missingTarget",{targetId:v(n["@actor"]),error:e})}e=o}if(A(t,e,s))return;e._send(s,t)}(t,e,s)}_register(t,e){return e===this._rootActor?this._children?.set(t,e):(this._children??=new Map).set(t,e),se(this),t}_getByAddress(t){const e=this._getRootActor();if(e?.address===t)return e;for(const e of this._children?.values()??[])if(e.address===t&&e._isRunning())return e}_unregister(t){let e;t===this._rootActor?(e=void 0!==this._getRootActor(),e=void 0!==t.sessionId&&(this._children?.delete(t.sessionId)??!1)||e):e=void 0!==t.sessionId&&(this._children?.delete(t.sessionId)??!1);const s=this._reverseKeyedActors?.get(t);void 0!==s&&(this._keyedActors?.get(s)===t&&(this._keyedActors.delete(s),e=!0),this._reverseKeyedActors?.delete(t)),e&&se(this)}get(t){return this._keyedActors?.get(t)}getAll(){return Object.fromEntries(this._keyedActors?.entries()??[])}_set(t,e){const s=this._keyedActors?.get(t);if(s&&s!==e)throw new Error(i(45,String(t)));(this._keyedActors??=new Map).set(t,e),(this._reverseKeyedActors??=new WeakMap).set(e,t),s!==e&&se(this)}inspect(t){const e=Vs(t);return(this._inspectionObservers??=new Set).add(e),{unsubscribe:()=>{this._inspectionObservers?.delete(e)}}}onRejectedEvent(t){const e=e=>t(e);return(this._rejectionListeners??=new Set).add(e),{unsubscribe:()=>{this._rejectionListeners?.delete(e)}}}onEmitted(t){const e=e=>t(e);return(this._emissionListeners??=new Set).add(e),{unsubscribe:()=>{this._emissionListeners?.delete(e)}}}_notifyEmitted(t,e){const s=this._emissionListeners;if(!s?.size)return;const n={actorRef:t,event:e};for(const t of Array.from(s))try{t(n)}catch(t){R(t)}}_hasInspectionObservers(){return!!this._inspectionObservers?.size}_sendInspectionEvent(t){if(!this._inspectionObservers?.size)return;const e={...t,rootId:this._rootActor.sessionId};this._inspectionObservers.forEach(t=>{try{t.next?.(e)}catch(t){R(t)}})}spawnActor(t,e){return this.runtime?.spawnActor?.(t,e)}startActor(t){const e=this.runtime?.startActor;if(e)return e(t);t.start()}stopActor(t){const e=this.runtime?.stopActor;if(e)return e(t);!function(t){t._stop()}(t)}terminateActor(t,e){const s=this.runtime?.terminateActor;if(s)return s(t,e);!function(t,e){t._terminate(e)}(t,e)}sendEvent(t,e,s){if(g(e)){const n=m(this,e);if(n)e=n;else if(this._getByAddress(e["@actor"])||!this.runtime?.sendEvent)return this.deadLetter(t,void 0,s,"missingTarget",{targetId:v(e["@actor"]),error:_(e)})}if(A(t,e,s))return;this._recordSent(t,e,s);const n=this.runtime?.sendEvent;if(n)return n(t,e,s);this._deliver(t,e,s)}emitEvent(t,e){const s=this.runtime?.emitEvent;if(s)return s(t,e);t._emit(e)}deadLetter(t,e,s,n,r){const o=this._rejectionListeners;if(o?.size){const i={event:s,targetRef:e,targetId:e?.id??r?.targetId,sourceRef:t,eventOrigin:t?"actor":"external",reason:n,issues:r?.issues,error:r?.error};for(const t of[...o])try{t(i)}catch(t){R(t)}}const i=this.runtime?.deadLetter;if(i)return i(t,e,s,n,r)}runStep(t,e,s){const n=this.runtime?.runStep;return n?n(t,e,s):I(t,e,s)}runLogic(t,e){const s=this.runtime?.runLogic;return s?s(t,e):e()}scheduleTimer(t,e,s){const n=this.runtime?.scheduleTimer;if(n){const r=en(t,e);if(r){const e="self"===r.target?t:r.target;this._recordSent(t,e,r.event,s,r.id)}return n(t,e,s)}this.schedule(t,e,s)}cancelTimer(t,e){const s=this.runtime?.cancelTimer;if(s)return s(t,e);this.cancel(t,e)}cancelAllTimers(t){const e=this.runtime?.cancelAllTimers;if(e)return e(t);this.cancelAll(t)}_relay(t,e,s){if(!function(t){let e=!1,s=!1;for(const n of $)n.parent===t&&(n.requiresReevaluation=!0,e=!0,s||="abort"===n.mode);if(s)throw K;return e}(e))return this.sendEvent(t,e,s)}getSnapshot(){return{_scheduledTimers:{...this._snapshot._scheduledTimers}}}start(){const t=this._snapshot._scheduledTimers;let e=!0;for(const s in t){e&&(this._snapshot._scheduledTimers={},e=!1);const{source:n,dueAt:r,key:o}=t[s];this.scheduleTimer(n,o,Math.max(0,r-(this._clock.now?.()??Date.now())))}}}let nn=!1;const rn=Object.freeze([]),on={"xstate.done.actor":!0,"xstate.error.actor":!0,"xstate.error.execution":!0,"xstate.snapshot.actor":!0,"xstate.done.state":!0,"xstate.after":!0,"xstate.timeout.state":!0,"xstate.timeout.actor":!0,[l]:!0};let cn=function(t){return t[t.NotStarted=0]="NotStarted",t[t.Running=1]="Running",t[t.Stopped=2]="Stopped",t}({});const an=Object.freeze({clock:{setTimeout:(t,e)=>setTimeout(t,e),clearTimeout:t=>clearTimeout(t)},logger:console.log.bind(console)});function un(t,e){try{t?.(e)}catch(t){R(t)}}function hn(t,e){if(t?.length)for(const s of t)e.actionExecutor(s)}function dn(t,e){return new fn(t,e)}class fn{get address(){return this._address??=this._parent?`${this._parent.address}/${Xs(this.id)}`:Xs(this.id)}get trigger(){return this._trigger??=new Proxy({},{get:(t,e)=>t=>{this.send({...t,type:e})}})}get send(){return this._boundSend??=this._sendPublic.bind(this)}constructor(t,e){this.logic=t,this._snapshot=void 0,this.clock=void 0,this.options=void 0,this.id=void 0,this._boundProcess=void 0,this.mailbox=void 0,this._mailboxStarted=!1,this.observers=void 0,this._errorObserved=!1,this.eventListeners=void 0,this.logger=void 0,this._processingStatus=cn.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=void 0,this._collectedActions=void 0,this._collectedSent=void 0,this._inTransition=!1,this._initialEffects=void 0,this.registryKey=void 0,this.sessionId=void 0,this._address=void 0,this.system=void 0,this._trigger=void 0,this._boundSend=void 0,this.src=void 0,this._deferred=void 0,this._restored=!1,this._warnedUnhandledTypes=void 0;const s=e?{...an,...e}:an,{clock:n,logger:r,parent:o,syncSnapshot:i,id:c,registryKey:a,inspect:u}=s;var h;this.system=o?o.system:s._systemRef?.current??function(t,e){return new sn(t,e)}(this,{clock:n,logger:r,createActorRef:dn}),o||!s._systemRef||s._systemRef.current||(s._systemRef.current=this.system,s._systemRef.initialize?.()),u&&!o&&this.system.inspect(Vs(u)),s.onRejectedEvent&&!o&&this.system.onRejectedEvent(s.onRejectedEvent),this.sessionId=s._sessionId??`${(h=this.system)._identity.executionId}:${h._identity.nextSessionId++}`,this.id=function(t,e,s){if(void 0!==e){const n=Gs(e);return n?Ys(t,Qs(s?.parent,n.prefix),n.index+1):s?.parent||Ys(t,Qs(void 0,e),1),e}const n=Fs(s?.src),r=Qs(s?.parent,n),o=t._snapshot._nextActorIds[r]??0;return Ys(t,r,o+1),s?.parent||"x"===n||0!==o?`${n}:${o}`:n}(this.system,c,{parent:o,src:s.src??t}),this.logger=e?.logger??this.system._logger,this.clock=e?.clock??this.system._clock,this._parent=o,this._syncSnapshot=i,this.options=s,this.src=s.src??t,this.ref=this,this._actorScope=this,a&&(this.registryKey=a,this.system._set(a,this)),this._collectedMicrosteps=void 0;const d=s._restore;this._restored=void 0!==d;try{if(d)this._setSnapshot(d.restore(this._actorScope));else if(e?._inert);else{this._inTransition=!0;try{const[t,e]=Lt(this._actorScope,void 0,this.logic.initialTransition(this.options?.input,this._actorScope));this._setSnapshot(t),this._initialEffects=e.length?e:void 0}finally{this._inTransition=!1}}}catch(t){this._setSnapshot(d?.error?.(t)??{status:"error",output:void 0,error:t}),this._deferred&&(this._deferred.length=0)}a&&"active"!==this._snapshot.status&&this.system._unregister(this),(this.system._hasInspectionObservers?.()??1)&&this.system._sendInspectionEvent({type:"@xstate.actor",actorRef:this,parentRef:this._parent,id:this.id,src:this.src,snapshot:this._snapshot})}_getLogger(){return this.logger}get self(){return this}get defer(){const t=t=>this._defer(t);return Object.defineProperty(this,"defer",{value:t}),t}get stopChild(){const t=t=>this._stopChild(t);return Object.defineProperty(this,"stopChild",{value:t}),t}get emit(){const t=t=>b(this,t,this.system);return Object.defineProperty(this,"emit",{value:t}),t}get actionExecutor(){const t=t=>this._executeAction(t);return Object.defineProperty(this,"actionExecutor",{value:t}),t}_isRunning(){return this._processingStatus===cn.Running}_defer(t){(this._deferred??=[]).push(t)}_stopChild(t){if(t._parent!==this)throw new Error(i(47,t.id,this.id));t._stop()}_executeAction(t){const e=()=>{(this.system._hasInspectionObservers?.()??1)&&(this._collectedActions??=[]).push({type:t.type,params:t.params});const e=nn;try{nn=!0;const e=t.exec();e&&"function"==typeof e.then&&Promise.resolve(e).catch(t=>{this._processingStatus!==cn.Stopped&&this._recoverOrError(t)})}finally{nn=e}};this._processingStatus!==cn.Running||this._forceDeferredActions?(this._deferred??=[]).push(e):e()}_setSnapshot(t){const e=this._snapshot;this._snapshot=t,ae(t,this,this.system,e)}_setErrorSnapshot(t,e=this._snapshot){this._setSnapshot({...e,status:"error",error:t})}_recoverOrError(t,e,s){return!!this._tryHandleExecutionError(t,e)||(this._setErrorSnapshot(t),this._error(t,s),!1)}_tryHandleExecutionError(t,e=this._snapshot){const s=this.logic.getExecutionErrorEvent?.(e,t);if(!s)return!1;const n=this._inTransition;this._inTransition=!0;try{const[t,n]=Lt(this._actorScope,e,this.logic.transition(e,s,this._actorScope));return this._setSnapshot(t),hn(n,this._actorScope),this.update(t,s),!0}catch{return!1}finally{this._inTransition=n}}_next(t){for(const e of this.observers??rn)un(e.next,t)}update(t,e){this._setSnapshot(t);const s=this._deferred;for(let e=0;e<(s?.length??0);e++){const n=s[e];try{n()}catch(e){if(s.length=0,this._tryHandleExecutionError(e,t))return;this._setErrorSnapshot(e,t),this._error(e);break}}if(s&&(s.length=0),"active"===this._snapshot.status)this._next(t);this._inspectTransition(this._snapshot,e)}_inspectTransition(t,e){(this.system._hasInspectionObservers?.()??1)&&this.system._sendInspectionEvent({type:"@xstate.transition",actorRef:this,event:e,sourceRef:this._lastSourceRef,targetRef:this,snapshot:t,microsteps:this._collectedMicrosteps??rn,actions:this._collectedActions??rn,sent:this._collectedSent??rn,eventType:e.type}),this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0}_flushInitialEffects(){if(!this._initialEffects?.length)return!0;this._forceDeferredActions=!0;try{return hn(this._initialEffects,this._actorScope),this._initialEffects=void 0,!0}catch(t){return this._initialEffects=void 0,this._deferred&&(this._deferred.length=0),this._recoverOrError(t),"active"===this._snapshot.status}finally{this._forceDeferredActions=!1}}subscribe(t,e,s){const n=Vs(t,e,s);if(this._processingStatus!==cn.Stopped)(this.observers??=new Set).add(n);else switch(this._snapshot.status){case"done":un(n.complete);break;case"error":{const t=this._snapshot.error;n.error?(n.passive||(this._errorObserved=!0),un(n.error,t)):R(t);break}}return{unsubscribe:()=>{this.observers?.delete(n)}}}on(t,e){let s=this.eventListeners?.get(t);return s||(s=new Set,(this.eventListeners??=new Map).set(t,s)),s.add(e),{unsubscribe:()=>{s.delete(e)}}}select(t,e=Object.is){return{subscribe:(s,n,r)=>{const o=Vs(s,n,r);let i=t(this.getSnapshot());return this.subscribe({next:s=>{const n=t(s);e(i,n)||(i=n,o.next?.(n))},error:o.error,complete:o.complete})},get:()=>t(this.getSnapshot())}}start(){const t=this._inTransition;this._inTransition=!0;try{return this._start()}finally{this._inTransition=t}}_start(){if(this._processingStatus===cn.Running)return this;if(this._processingStatus===cn.Stopped){const t=this._snapshot.status;if("done"===t||"error"===t)return this;throw new Error(i(48,this.id))}this._syncSnapshot&&this.subscribe({next:t=>{"active"===t.status&&this.system._relay(this,this._parent,{type:"xstate.snapshot.actor",actorId:this.id,sessionId:this.sessionId,snapshot:t})},error:()=>{}}),this.system._register(this.sessionId,this),this.registryKey&&this.system._set(this.registryKey,this),this._processingStatus=cn.Running,this._lastSourceRef=this._parent;switch(this._snapshot.status){case"done":return this._restored?(this._next(this._snapshot),this._stopProcedure(),this._complete(),this):this._flushInitialEffects()?(this.update(this._snapshot,M(this.options.input)),this):this;case"error":return this._error(this._snapshot.error),this;case"stopped":return this._stopProcedure(),this._complete(),this}if(this._parent||this.system.start(),!this._flushInitialEffects())return this;if(this.logic.start)try{this.logic.start(this._snapshot,this._actorScope,{restored:this._restored})}catch(t){return this._setErrorSnapshot(t),this._error(t),this}if(this._restored||this._deferred?.length||this.observers?.size||(this.system._hasInspectionObservers?.()??1)?this.update(this._snapshot,M(this.options.input)):(function(t,e,s){const n=Yt.get(t),r=ne(s);return n?.actor===e&&n.systemState.sourceSystem===s&&n.systemState.sourceVersion+1===r&&s._getRootActor?.()===e&&!s._peekChildren?.()&&(n.systemState.root=e,n.systemState.sourceVersion=r,!0)}(this._snapshot,this,this.system)||this._setSnapshot(this._snapshot),this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0),this._restored){const t=this._snapshot.timers??[],e=!this.system._clock.now,s=Date.now();for(const n of t){const t=e&&void 0!==n.scheduledAt?Math.min(n.delay,Math.max(0,n.scheduledAt+n.delay-s)):n.delay;this.system.scheduleTimer(this,N(n),t)}this._restored=!1}return this._mailboxStarted=!0,this.mailbox?.start(),this}_process(t){const e=this._inTransition;this._inTransition=!0;try{this._processEvent(t)}finally{this._inTransition=e}}_processEvent({event:t,sourceRef:e}){this._lastSourceRef=e;const s=this._snapshot;let n,r;try{n=Lt(this._actorScope,this._snapshot,this.logic.transition(this._snapshot,t,this._actorScope))}catch(t){r={err:t}}if(r)return this._collectedMicrosteps=void 0,this._collectedActions=void 0,this._collectedSent=void 0,void(this._recoverOrError(r.err)||this._inspectTransition(this._snapshot,t));if(!n)return;let o=this._snapshot;try{const[r,i]=n;r!==s||i.length||!("machine"in s)||t.type===d||e&&Object.hasOwn(on,t.type)||this._reportUnhandledEvent(t),o=r,this._setSnapshot(o),hn(i,this._actorScope),this.update(o,t)}catch(e){return void(this._recoverOrError(e,o,s)||this._inspectTransition(this._snapshot,t))}t.type===d&&(this._stopProcedure(),this._complete())}_reportUnhandledEvent(t){un(()=>this.options.onUnhandledEvent?.(t,this._snapshot))}_emit(t){for(const e of[this.eventListeners?.get(t.type),this.eventListeners?.get("*")])if(e)for(const s of e)un(s,t)}_terminate(t){if("done"===t.status)return this._next(this._snapshot),this._stopProcedure(),this._complete(),void(this._parent&&this.system._relay(this,this._parent,(e=this.id,s=t.output,n=this.sessionId,{type:"xstate.done.actor",output:s,actorId:e,sessionId:n})));var e,s,n;"error"===t.status&&this._error(t.error)}_stop(){return this._processingStatus===cn.Stopped?this:(this.mailbox?.clear(),this._processingStatus===cn.NotStarted?(this._processingStatus=cn.Stopped,this.system._unregister(this),this):(this._send({type:d}),this.system._unregister(this),this))}stop(){if(this._parent)throw new Error(i(49,this.id));return this._stop()}_complete(){for(const t of this.observers??rn)un(t.complete);this.observers?.clear(),this.eventListeners?.clear()}_reportUnlessObserved(t){setTimeout(()=>{this._errorObserved||R(t)})}_error(t,e){this._stopProcedure();const s=new Set;for(const t of[this._snapshot,e]){const e=t?.children;for(const t of Object.values(e??{}))t?._parent===this&&"function"==typeof t.getSnapshot&&s.add(t)}for(const t of this.system._peekChildren?.()?.values()??[])t._parent===this&&s.add(t);for(const t of s)try{if("active"!==t.getSnapshot().status)continue;const e=this.system.stopActor(t);e&&Promise.resolve(e).catch(R)}catch(r){R(r)}if(this.observers?.size){let e=!1,s=!1;for(const n of this.observers){const r=n.error;n.passive||(e||=!r,s||=!!r),un(r,t)}e||=!s,this.observers.clear(),e&&this._reportUnlessObserved(t)}else this._parent||this._reportUnlessObserved(t);var n,r,o;this.eventListeners?.clear(),this._parent&&this.system._relay(this,this._parent,(n=this.id,r=t,o=this.sessionId,{type:"xstate.error.actor",error:r,actorId:n,sessionId:o}))}_stopProcedure(){this._processingStatus===cn.Running&&(this.system.cancelAllTimers(this),this.mailbox?.clear(),this.mailbox=void 0,this._boundProcess=void 0,this._mailboxStarted=!1,this._processingStatus=cn.Stopped,this.system._unregister(this))}_send(t,e){if(this._processingStatus===cn.Stopped)return void this.system.deadLetter(e,this,t,"stopped");let s=this.mailbox;s||(this._boundProcess??=this._process.bind(this),s=this.mailbox=new E(this._boundProcess),this._mailboxStarted&&s.start()),s.enqueue({event:t,sourceRef:e})}_sendPublic(t){this.system._relay(void 0,this,t)}toJSON(){return{"@actor":this.address}}getPersistedSnapshot(t){return this.logic.getPersistedSnapshot(this._snapshot,t)}[P](){return this}getSnapshot(){return this._snapshot}}function ln(t,...[e]){return a(e),new fn(t,e)}class pn extends Error{constructor(t){super(t),this.name="InvalidSnapshotError"}}function yn(t){return null!==t&&"object"==typeof t&&!Array.isArray(t)}function gn(t){return yn(t)&&"function"==typeof t.transition&&"function"==typeof t.getInitialSnapshot}function vn(t,e,s,n){if("string"==typeof e){if(!s.states[e])throw new pn(i(21,n.concat(e).join(".")));return}const r=t=>new pn(i(22===t?22:23,n.join(".")));if(!e||"object"!=typeof e||Array.isArray(e))throw r(22);const o=Object.keys(e);if(o.length>1&&"parallel"!==s.type)throw r(23);for(const r of o){const o=s.states[r];if(!o)throw new pn(i(21,n.concat(r).join(".")));vn(t,e[r],o,n.concat(r))}}function _n(t,e){if(!yn(e))throw new pn(i(24,t.id));if(void 0===e.value)throw new pn(i(25,t.id));vn(t,e.value,t.root,[]);const s=Array.from(Te(Be(t.root,e.value)));if(!yn(e.children))throw new pn(i(26));const n=new Map;for(const[r,o]of Object.entries(e.children)){if(!yn(o)||void 0!==o.remote&&"boolean"!=typeof o.remote||void 0!==o.address&&"string"!=typeof o.address||void 0!==o.syncSnapshot&&"boolean"!=typeof o.syncSnapshot||void 0!==o.registryKey&&"string"!=typeof o.registryKey||void 0!==o.incarnation&&"string"!=typeof o.incarnation||void 0!==o.snapshot&&!yn(o.snapshot))throw new pn(i(27,r));if(!0!==o.remote){if(void 0!==o.src&&"string"!=typeof o.src&&!gn(o.src))throw new pn(i(27,r));try{const e=void 0===o.src?Ws(t,s,r):"string"==typeof o.src?Object.hasOwn(t.sources.actors,o.src)?t.sources.actors[o.src]:void 0:o.src;if(!e)throw new pn(i(29,r,"string"==typeof o.src?o.src:"<inline>"));n.set(r,e)}catch(t){if(t instanceof pn)throw t;throw new pn(t instanceof Error?t.message:i(30,r))}}else if("string"!=typeof o.address||"string"!=typeof o.src)throw new pn(i(28,r))}if(void 0!==e.timers&&!Array.isArray(e.timers))throw new pn(i(31));for(const t of e.timers??[]){if(!yn(t)||"number"!=typeof t.delay||"@xstate.raise"!==t.type&&"@xstate.sendTo"!==t.type||!yn(t.event)||"string"!=typeof t.event.type||void 0!==t.id&&"string"!=typeof t.id||void 0!==t.seq&&"number"!=typeof t.seq||void 0!==t.scheduledAt&&"number"!=typeof t.scheduledAt||!("string"==typeof t.target||yn(t.target)&&"parent"===t.target.type||g(t.target)))throw new pn(i(32));if("string"==typeof t.target&&"self"!==t.target&&!Object.hasOwn(e.children,t.target))throw new pn(i(33,t.target))}return{nodes:s,childLogics:n}}const mn=new WeakMap;const xn={};class wn{constructor(t,e){if(this.config=t,this.key=void 0,this.id=void 0,this.type=void 0,this.path=void 0,this.states=void 0,this.history=void 0,this.entry=void 0,this.exit=void 0,this.parent=void 0,this.machine=void 0,this.meta=void 0,this.output=void 0,this.order=-1,this.description=void 0,this.schemas=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._candidateCache=void 0,this.parent=e._parent,this.key=e._key,this.machine=e._machine,this.parent&&function(t){for(let e=0;e<t.length;e++)if("\\"===t[e])e++;else if("."===t[e])return!0;return!1}(this.key))throw new Error(i(57,this.key));this.path=this.parent?this.parent.path.concat(this.key):[];const s=this.config.states?Object.keys(this.config.states)[0]:void 0;this.id=this.config.id||(this.machine.id?[this.machine.id,...this.path]:this.path).join("."),this.type=this.config.type||(void 0!==s?"compound":this.config.history?"history":"atomic"),this.description=this.config.description,this.schemas=this.config.schemas,function(t){const e=t.config;if("history"===t.type&&!("string"==typeof e.target&&e.target.trim().length>0||Array.isArray(e.target)&&e.target.length>0&&e.target.every(t=>"string"==typeof t&&t.trim().length>0)))throw new Error(i(60,t.id));if("choice"!==t.type)return;if("function"!=typeof e.choice)throw new Error(i(61,t.id))}(this);if(this.machine.idMap.get(this.id))throw new Error(i(58,this.id));if(this.order=this.machine.idMap.size,this.machine.idMap.set(this.id,this),this.states=this.config.states?function(t,e){const s={},n=Object.keys(t);for(let r=0;r<n.length;r++){const o=n[r],i=e(t[o],o,t,r);"__proto__"===o?Object.defineProperty(s,o,{value:i,enumerable:!0,configurable:!0,writable:!0}):s[o]=i}return s}(this.config.states,(t,e)=>new wn(t,{_parent:this,_key:e,_machine:this.machine})):xn,"compound"===this.type&&!this.config.initial)throw new Error(i(59,this.id));this.history=!0===this.config.history?"shallow":this.config.history||!1,this.entry=this.config.entry,this.exit=this.config.exit,this.meta=this.config.meta,this.output="final"!==this.type&&this.parent?void 0:this.config.output,this.tags=Ks(t.tags).slice(),this.invoke=Ks(this.config.invoke).map((t,e)=>{const{src:s,registryKey:n}=t,r=`${this.id}:${e}`;const o=t.id??r;return{...t,src:"string"==typeof s?s:void 0,logic:s,id:o,registryKey:n}})}_initialize(){this.after=function(t){const e=t.config.after,s=t.config.timeout,n=t.config.onTimeout,r=t.invoke.filter(t=>void 0!==t.timeout);if(!e&&void 0===s&&0===r.length)return[];const o=[];if(e)for(const s of Object.keys(e)){const n=Number.isNaN(+s)?s:+s;o.push({event:k(n,t.id),delay:n,transitions:e[s],fromDelaysMap:!0,name:`after.${s}`})}var i;void 0!==s&&n&&o.push({event:(i=t.id,{type:"xstate.timeout.state",stateId:i}),delay:s,transitions:n,fromDelaysMap:!0,name:"onTimeout"});for(const t of r)o.push({event:O(t.id),resolveEvent:({children:e})=>O(t.id,e[t.id]?.sessionId),eventMatcher:(e,s)=>$e(e,s,t.id),delay:t.timeout,transitions:t.onTimeout,fromDelaysMap:!1,name:"invoke.onTimeout"});const c=[];for(const{event:e,resolveEvent:s,eventMatcher:n,delay:r,transitions:i,fromDelaysMap:a,name:u}of o){Ke(t,e,s??(()=>e),e=>Ae(r,a?e.delays:{},{context:e.context,event:e.event,stateNode:t,input:e.input}));const{type:o,...h}=e;for(const e of zs(i,u,t.id))c.push({...e,matches:{...e.matches,...Object.fromEntries(Object.entries(h).filter(([,t])=>void 0!==t))},event:o,delay:r,_eventMatcher:n})}return c.map(e=>({...qe(t,e.event,e),delay:e.delay}))}(this),this.transitions=function(t){const e=new Map,s=(t,s)=>{e.set(t,[...e.get(t)??[],...s])};if(t.config.on)for(const s of Object.keys(t.config.on)){if(""===s)throw new Error(i(63,t.id));const n=t.config.on[s];e.set(s,bn(t,s,n,e=>qe(t,s,e)))}if(t.config.onDone){const e="xstate.done.state";s(e,bn(t,"onDone",t.config.onDone,s=>qe(t,e,{...s,matches:{...s.matches,stateId:t.id}})))}if(t.config.onError){const s="xstate.error.*";e.set(s,bn(t,"onError",t.config.onError,e=>qe(t,s,e)))}const n=(e,s,n)=>qe(t,e,{matches:{actorId:n},_eventMatcher:(t,e)=>$e(t,e,n),to:(t,e)=>(e.cancel(s),{})}),r=(e,s,n)=>{const{target:r,to:o,reenter:i,...c}=s;return qe(t,e,{...c,reenter:i,to:(t,e)=>{if(o){let s=!1;const r=new Proxy(e,{apply:(t,e,n)=>(s=!0,Reflect.apply(t,e,n)),get(t,e,n){const r=Reflect.get(t,e,n);return"function"!=typeof r?r:(...e)=>(s=!0,r.apply(t,e))}}),i=o(t,r);return(void 0!==i||s)&&e.cancel(n),i}return e.cancel(n),{target:r,reenter:i}}})};for(const e of t.invoke){const o=t=>({...t,matches:{...t.matches,actorId:e.id},_eventMatcher:(t,s)=>$e(t,s,e.id)}),i=void 0!==e.timeout?{event:{type:"xstate.timeout.actor",actorId:e.id}}:void 0;if(e.onDone){const c="xstate.done.actor",a=bn(t,"invoke.onDone",e.onDone,e=>{const s=o(e);return i?r(c,s,i):qe(t,c,s)});i&&a.push(n(c,i,e.id)),s(c,a)}else if(i){const t="xstate.done.actor";s(t,[n(t,i,e.id)])}if(e.onError){const n="xstate.error.actor";s(n,bn(t,"invoke.onError",e.onError,e=>{const s=o(e);return i?r(n,s,i):qe(t,n,s)}))}if(e.onSnapshot){const n="xstate.snapshot.actor";s(n,bn(t,"invoke.onSnapshot",e.onSnapshot,e=>qe(t,n,o(e))))}}for(const s of t.after){let t=e.get(s.eventType);t||(t=[],e.set(s.eventType,t)),t.push(s)}return e}(this),"choice"===this.type?this.always=function(t){const e=t.config.choice,s=e=>{if(!e||void 0===e.target)throw new Error(i(62,t.id));return e};return[qe(t,"",{to:t=>s(e(t))})]}(this):this.config.always&&(this.always=bn(this,"always",this.config.always,t=>qe(this,"",t)));for(const t of Object.keys(this.states))this.states[t]._initialize();this._refreshEventMetadata()}_refreshEventMetadata(){const t={},e=[];for(const[s,n]of this.transitions)t[s]=n.slice(),n.some(t=>t.target||t.reenter||t.to)&&e.push(s);this.on=t,this.ownEvents=e;const s=new Set(e);for(const t of Object.values(this.states))for(const e of t.events)s.add(e);this.events=Array.from(s)}get initial(){return function(t,e,s){let n=mn.get(t);return n?e in n||(n[e]=s()):(n={[e]:s()},mn.set(t,n)),n[e]}(this,"initial",()=>function(t,e){const s="object"==typeof e&&null!==e?e.target:e,n="object"==typeof e&&null!==e?e.input:void 0,r="object"==typeof e&&null!==e?e.to:void 0,o="object"==typeof e&&null!==e?e.meta:void 0,c="object"==typeof e&&null!==e?e.description:void 0,a=Array.isArray(s)?s:s?[s]:[],u=a.map(e=>e.startsWith("#")?t.machine.getStateNodeById(e.slice(1)):t.states[e]);if(u.some(t=>!t))throw new Error(i(64,a.find((t,e)=>!u[e]),t.id));return{source:t,target:u.length?u:void 0,input:n,to:r,meta:o,description:c}}(this,this.config.initial))}next(t,e,s,n){if("final"===this.type)return;let r=this._candidateCache?.get(e.type);r||(r=function(t,e){const s=t.transitions.get(e.type)??[],n=[...t.transitions.keys()].filter(t=>t!==e.type&&Us(e.type,t)).sort((t,e)=>e.length-t.length).flatMap(e=>t.transitions.get(e));return s.length?[...s,...n]:n}(this,e),(this._candidateCache??=new Map).set(e.type,r));for(const o of r){if(ps(o,e,t,this,s,n))return[o]}}}function bn(t,e,s,n){const r=zs(s,e,t.id),o=new Array(r.length);for(let t=0;t<r.length;t++)o[t]=n(r[t]);return o}function Sn(t,e){const s=t.check(e);if(s)throw s}let In,An;function En(){if(An)return An;const t=In??=ln({transition:t=>[t,[]],initialTransition:()=>[{status:"active",output:void 0,error:void 0},[]],getInitialSnapshot:()=>({status:"active",output:void 0,error:void 0}),getPersistedSnapshot:t=>t});return An={self:t,logger:()=>{},id:"",sessionId:"",defer:()=>{},system:t.system,stopChild:()=>{},emit:()=>{},actionExecutor:()=>{}},Tt.add(An),An}class kn{constructor(t,e,s){if(this.config=t,this.validator=s,this._internalEventType=void 0,this._transitionMetaType=void 0,this._inputType=void 0,this._actionMap=void 0,this._actorMap=void 0,this._stateSchema=void 0,this.version=void 0,this.schemas=void 0,this.sources=void 0,this.options=void 0,this.idMap=new Map,this.root=void 0,this._raw=()=>this,this.id=void 0,this.states=void 0,this.events=void 0,this.internalEventDescriptors=void 0,this._hasEventlessTransitions=void 0,this._microstepHooks=void 0,this._json=void 0,void 0!==t.version&&!t.id)throw new Error(i(18,t.version));this.id=t.id??"",this.sources={actors:t.actors??{},actions:t.actions??{},delays:t.delays??{},guards:t.guards??{},...e},this.version=this.config.version,this.schemas=this.config.schemas,this.internalEventDescriptors=Object.keys(this.schemas?.internalEvents??{}),this.options={...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.start=this.start.bind(this),this.root=new wn(t,{_key:this.id,_machine:this}),this.root._initialize(),function(t){const e=[],s=n=>{Object.values(n).forEach(n=>{if(n.config.route&&n.config.id){const r=n.config.id,o=n.config.route,i=({event:t})=>t.to===`#${r}`;if("function"==typeof o)return e.push(qe(t,"xstate.route",{guard:i,to:t=>{const e=o(t);if(e)return{...!0===e?{}:e,target:`#${r}`}}})),void(n.states&&s(n.states));const{guard:c,...a}=o,u={...a,guard:i,target:`#${r}`};e.push(qe(t,"xstate.route",u))}n.states&&s(n.states)})};s(t.states),e.length>0&&t.transitions.set("xstate.route",e)}(this.root),this.root._refreshEventMetadata(),this._hasEventlessTransitions=Array.from(this.idMap.values()).some(t=>!!t.always?.length),this.states=this.root.states,this.events=this.root.events}provide(t){const{actions:e,guards:s,actors:n,delays:r}=this.sources,o=new kn(this.config,{actions:{...e,...t.actions},guards:{...s,...t.guards},actors:{...n,...t.actors},delays:{...r,...t.delays}},this.validator);return o._json=this._json,o.internalEventDescriptors=this.internalEventDescriptors,o._microstepHooks=this._microstepHooks,o}resolveState(t){const e=(s=this.root,n=t.value,Me(s,Te(Be(s,n))));var s,n;const r=Te(Be(this.root,e));return ks({_nodes:[...r],value:e,context:t.context||{},children:{},status:Re(r,this.root)?"done":t.status||"active",output:t.output,error:t.error,historyValue:t.historyValue},this)}transition(t,e,s){const n=!s,r=s??Se(this,t);if(n&&le(r,t,!1),this.validator){const n=s&&s.self._lastSourceRef,o=n?"actor":"external",i=this.validator.check({kind:"event",logic:this,event:e,eventOrigin:o});if(i)return[t,[it(r,n,e,"invalidEvent",{issues:i.issues,error:i})]]}St(r);const o=this._transitionFast(t,e,r);if(o){n&&le(r,o,!1);const e=n&&o!==t?ye(r,o):this._attachPureActorRef(o,r);return this.validator&&Sn(this.validator,{kind:"result",logic:this,snapshot:e,effects:[]}),[e,[]]}const{snapshot:i,microsteps:c}=cs(t,e,r,[]);n&&le(r,i,!1);const a=n?i===t?i:ye(r,i):this._attachPureActorRef(i,r),u=this._collectEffects(c);return this.validator&&Sn(this.validator,{kind:"result",logic:this,snapshot:a,effects:u}),[a,u]}_collectEffects(t){return t.flatMap(([,t])=>t)}_attachPureActorRef(t,e,s=!1){return pe(e)||s&&e.self._actorScope===e&&void 0===e.self._snapshot||ae(t,e.self,e.system),t}_transitionFast(t,e,s){if("active"!==t.status||"string"!=typeof t.value||this.root.always?.length)return;const n=this.root.states[t.value];if(!n||"atomic"!==n.type||n.exit||n.invoke.length||n.always?.length||n.after?.length)return;const r=n.transitions.get(e.type);if(1!==r?.length)return;const o=r[0];if(o.guard||o.actions||o.to||o.reenter||o.input||"function"==typeof o.context||o.target&&1!==o.target.length)return;const i=o.target?.[0]??n,c=i!==n;if(i.parent!==this.root||"atomic"!==i.type||c&&(i.entry||i.invoke.length||i.always?.length||i.after?.length))return;const a=void 0!==o.context?{...t.context,...o.context}:t.context;if(!pe(s)&&(s.system._hasInspectionObservers?.()??1)){const t=s.self._collectedMicrosteps||[];t.push(o),s.self._collectedMicrosteps=t}const u=t._stateInputs[this.root.id];return Os(t,{...a!==t.context?{context:a}:{},...c?{_nodes:[this.root,i],_stateInputs:u?{[this.root.id]:u}:{}}:{}})}microstep(t,e,s){const{microsteps:n}=cs(t,e,s,[]),r=new Array(n.length);for(let t=0;t<n.length;t++)r[t]=n[t][0];return r}getTransitionData(t,e,s,n){return this._microstepHooks?.begin(s.self),Fe(this.root,t.value,t,e,s,n)||[]}isInternalEventType(t){if(t===l)return!0;for(const e of this.internalEventDescriptors)if(Us(t,e))return!0;return!1}_canTransition(t,e){if("active"!==t.status)return!1;const s=En(),n=this.getTransitionData(t,e,s);if(!n?.length)return!1;for(const r of n){if(void 0!==r.target)return!0;const n=os(r,t,e,s,{resolveActions:!1});if(n.targets?.length||n.context||as(r,t.context,e,t,s))return!0}return!1}getExecutionErrorEvent(t,e){if("active"===t?.status&&t.nodes?.some(t=>t.config.onError))return T("execution",e)}_getPreInitialState(t,e){const{context:s}=this.config,n=ks({context:"function"!=typeof s&&s?s:{},_nodes:[this.root],children:{},status:"active"},this);if("function"==typeof s){const r={},o=function(t,e,s){return(n,r)=>{const o=B(e,n),i=r?.id??kt(t,o??n).id;void 0!==r?.id&&(Ot(t,r.id),jt(t,r.id));const c=t.system.createActorRef(n,{id:i,parent:t.self,syncSnapshot:r?.syncSnapshot,input:r?.input,src:o??n,registryKey:r?.registryKey});return s[c.id]=c,c}}(t,this.sources.actors,r),i=s(function(t,e){return he(e)?(Object.defineProperty(t,"self",{enumerable:!0,get:()=>e.self}),t):Object.assign(t,{self:e.self})}({spawn:o,input:e.input,actors:this.sources.actors},t)),[c]=Ct(n,0,t,[]);if(i&&(c.context=i),Object.keys(r).length>0){c.children={...c.children,...r};const e=function(t){const e=wt.get(t)?.counters;if(e?.size)return Object.fromEntries(e)}(t);e&&(c._nextActorIds=_t(c._nextActorIds,e))}return c}return n}getInitialSnapshot(t,e){return this.initialTransition(e,t)[0]}initialTransition(t,e){this.validator&&Sn(this.validator,{kind:"input",logic:this,input:t});const s=!e,n=e??Se(this);St(n);const r=M(t),o=[],i=(t,e)=>{s&&le(n,t,!1);const r=s?ye(n,t):this._attachPureActorRef(t,n,!0),o=this._collectEffects(e);return this.validator&&"error"!==t.status&&Sn(this.validator,{kind:"result",logic:this,snapshot:r,effects:o}),[r,o]};let c;try{c=this._getPreInitialState(n,r)}catch(t){return i(Os(ks({context:"function"!=typeof this.config.context&&this.config.context?this.config.context:{},_nodes:[this.root],children:{},status:"active"},this),{status:"error",error:t}),[])}const a=Object.values(c.children).filter(Boolean).map(t=>function(t){const e=[t];return{kind:"builtin",exec:at,type:h,source:t._parent,params:void 0,args:e,actor:t,id:t.id,logic:t.logic,src:t.src,input:t.options?.input}}(t));try{const[t,e]=function(t,e,s,n,r){t.machine._microstepHooks?.begin(s.self);const o=[...Ue(t)],i=new Set,c=t=>{if("compound"===t.type){i.add(t);for(const e of t.initial.target??[])c(e)}else if("parallel"===t.type)for(const e of Oe(t))c(e)};return c(t),rs([{target:o.filter(t=>!o.some(e=>e!==t&&Xe(e,t))),source:t,reenter:!0,eventType:null,toJSON:null,_statesForDefaultEntry:[...i]}],e,s,n,!0,r)}(this.root,c,n,r,o),{snapshot:s,microsteps:u}=cs(t,r,n,o,[[t,[...a,...e]]]);return i(s,u)}catch(t){if(!this.root.config.onError)throw t;const e=cs(c,T("execution",t),n,[],[[c,a]]);return i(e.snapshot,e.microsteps)}}start(t,e){if(!t?.children)return;const s=t.children;for(const t in s){if(!Object.hasOwn(s,t))continue;const n=s[t];n._rehydrated&&"active"===n.getSnapshot?.().status&&(e?e.actionExecutor(Dt(n)):n.system.startActor(n))}}getStateNodeById(t){const e=Pe(t),s=e?t.slice(1):t;if(""===s&&e)throw new Error(i(19,this.id));const n=this.idMap.get(s);if(n)return n;throw new Error(i(20,s))}getPersistedSnapshot(t,e){return function(t,e){const{nodes:s,_stateInputs:n,_nextTimerId:r,_nextActorIds:o,tags:c,machine:a,children:u,timers:h,context:d,can:f,hasTag:l,matches:p,getMeta:v,getInputs:_,toJSON:m,...x}=t,w={},b=[],S=!1!==e?.embedChildren;for(const t in u){const n=u[t],r="string"==typeof n.src?n.src:void 0,o=void 0===r&&!0===n._invoked&&s.some(e=>e.invoke.some(e=>e.id===t));if(!(void 0!==r||o||e&&"__unsafeAllowInlineActors"in e))throw new Error(i(41,t));const c=S&&!y(n);if(!c&&void 0===r)throw new Error(i(42,t));w[t]={address:n.address,...c?{snapshot:n.getPersistedSnapshot(e)}:{remote:!0,incarnation:y(n)?n.sessionId:void 0},...o?{}:{src:n.src},registryKey:n.registryKey,syncSnapshot:n._syncSnapshot}}const I=ie(t)?.actor,A=I?.system?._clock?.now?void 0:I?.system?._snapshot?._scheduledTimers;for(const e of h){let s,n=e.event;if("xstate.timeout.actor"===n.type&&(n={...n},delete n.sessionId),"self"===e.target)s="self";else if(g(e.target))s={...e.target};else{const n=Object.entries(u).find(([,t])=>t===e.target)?.[0];if(n)s=n;else{if(e.target!==ie(t)?.actor._parent)throw new Error(i(43,e.target.id));s={type:"parent"}}}const r=A?.[V(I,N(e))],o=r?r.dueAt-e.delay:W(e);b.push({...void 0!==e.id&&{id:e.id},...void 0!==e.seq&&{seq:e.seq},delay:e.delay,type:e.type,event:n,target:s,...void 0!==o&&{scheduledAt:o}})}const E={...x,context:Ts(d,()=>ie(t)?.actor.id??a.id),children:w,timers:b,historyValue:js(x.historyValue)};return n&&Object.keys(n).length>0&&(E.stateInputs=n),E.nextTimerId=r,o&&Object.keys(o).length>0&&(E.nextActorIds=o),void 0!==a.version&&(E.machine={id:a.id,version:a.version}),E}(t,e)}}const On=new WeakMap;function jn(t,e,s){if(t instanceof kn)return Mn(t,e,s);if(!t.restoreSnapshot&&null===e)throw new Error(i(46));return t.restoreSnapshot?t.restoreSnapshot(e,s):e}function Tn(t,e){return{restore:s=>jn(t,e,s),error:s=>t instanceof kn?function(t,e,s){const n=e?.context;return Os(ks({context:n&&"object"==typeof n?n:{},_nodes:[t.root],children:{},status:"active"},t),{status:"error",error:s})}(t,e,s):void 0}}function Mn(t,e,s){t instanceof kn&&(t=t._raw()),e=o(e);const n=!s,r=s??Se(t);if(!e||"object"!=typeof e||Array.isArray(e))throw new pn(i(24,t.id));const c=e.machine,a=c&&"object"==typeof c&&"function"==typeof c.transition&&"root"in c?void 0:c,u=e.version,h="string"==typeof a?.version?a.version:u;if(void 0!==u&&void 0!==a?.version&&a.version!==u)throw new Error(i(34,u,a.version));if(a&&"string"==typeof a.id&&a.id!==t.id)throw new Error(i(35,a.id,t.id));if(h!==t.version){const s=t.config.migrate;if("function"!=typeof s)throw new Error(i(36,h,t.version));e=s(e,h)}const d=e,{nodes:f,childLogics:l}=_n(t,d),p={},y=d.children;for(const t of Object.keys(y)){const e=y[t];if(!0===e.remote&&void 0!==e.address){if("string"!=typeof e.src)throw new Error(i(28,t));const s=w(r.system,{id:t,address:e.address,src:e.src,parent:r.self,registryKey:e.registryKey,syncSnapshot:e.syncSnapshot,incarnation:e.incarnation});e.registryKey&&r.system._set(e.registryKey,s),p[t]=s;continue}const s=e.snapshot,n=e.src,o=l.get(t),c=r.system.createActorRef(o,{id:t,parent:r.self,syncSnapshot:e.syncSnapshot,...void 0!==s?{_restore:Tn(o,s)}:{},src:n,registryKey:e.registryKey});c._rehydrated=!0,void 0===n&&(c._invoked=!0),p[t]=c}const v=[],_=d.timers??[];for(const t of _){let e=t.event;if("xstate.timeout.actor"===e.type){const t=e.actorId,s=p[t];s&&(e=O(t,s.sessionId))}const s="string"==typeof t.target?"self"===t.target?"self":p[t.target]:g(t.target)?t.target:r.self._parent;if(!s){const e="string"==typeof t.target?t.target:t.target.type;throw new pn(i(33,e))}v.push({...t,event:e,target:s})}const m=(e=>{if(!e||"object"!=typeof e)return{};const s={};for(const n of Object.keys(e)){const r=e[n];for(const e of r){let r;if(e instanceof wn)r=e;else try{r=t.getStateNodeById(e.id)}catch{}r&&(s[n]??=[],s[n].push(r))}}return s})(d.historyValue),{version:x,_nextActorId:b,nextTimerId:S,nextActorIds:I,stateInputs:A,...E}=e;let k=I;for(const t of Object.keys(y)){const e=Gs(t);e&&(k?.[e.prefix]??0)<=e.index&&(k={...k,[e.prefix]:e.index+1})}const j=new Map;const T=ks({...E,context:function t(e,s=!1){if(!e||"object"!=typeof e)return e;if(!s&&g(e))return void 0!==e.incarnation?e:Object.values(p).find(t=>t.address===e["@actor"])??e;const n=Object.getPrototypeOf(e);if(!Array.isArray(e)&&n!==Object.prototype&&null!==n)return e;let r=j.get(e);if(!r){j.set(e,r=Array.isArray(e)?[]:Object.create(n));for(const s of Object.keys(e))Object.defineProperty(r,s,{value:t(e[s]),writable:!0,enumerable:!0,configurable:!0})}return r}(E.context,!0),children:p,timers:v,_nextTimerId:S,_nextActorIds:k,_nodes:f,value:void 0,historyValue:m,_stateInputs:Object.fromEntries(Object.entries(d.stateInputs??{}).filter(([t])=>f.some(e=>e.id===t)))},t,r.self);return n?(le(r,T,!1),ye(r,T)):(pe(r)||ae(T,r.self,r.system),T)}t.restoreActor=function(t,e,s){const{system:n,...r}=s??{};a(r);const o={...r,_restore:Tn(t,e)};return n?n.createActor(t,o):new fn(t,o)},t.restoreLogicSnapshot=jn,t.restoreMachineSnapshot=Mn,t.snapshotSchema=function(t){return function(t,e,s){let n=t.get(e);return n||t.set(e,n=s()),n}(On,t,()=>({"~standard":{version:1,vendor:"xstate",validate:async e=>{if(null===e||"object"!=typeof e)return{issues:[{message:"Expected a persisted snapshot."}]};const s={historyValue:{},timers:[],...e},n=t.schemas?.context;let r=s.context;if(n){const e=await n["~standard"].validate(r);if(e.issues)return{issues:[{message:`Invalid context for machine '${t.id}' version '${t.version}': ${e.issues[0]?.message}`}]};r=e.value}for(const t of["value","children"])if(!(t in s))return{issues:[{message:`Persisted snapshot is missing '${t}'.`}]};if(!["active","done","error","stopped"].includes(s.status))return{issues:[{message:"Persisted snapshot has invalid status."}]};if(!yn(s.historyValue))return{issues:[{message:"Persisted snapshot has invalid 'historyValue'."}]};try{_n(t,s)}catch(t){return{issues:[{message:t instanceof Error?t.message:"Persisted snapshot has invalid state value."}]}}return{value:{...s,context:r}}}}}))},Object.defineProperty(t,"__esModule",{value:!0})});
2
2
  //# sourceMappingURL=xstate-persist.umd.min.js.map