var t,e;t=this,e=function(t){"use strict";var e;t.InterpreterStatus=void 0,(e=t.InterpreterStatus||(t.InterpreterStatus={}))[e.NotStarted=0]="NotStarted",e[e.Running=1]="Running",e[e.Stopped=2]="Stopped";const n={type:"xstate.init"};function i(t){return void 0===t?[]:[].concat(t)}function o(t,e){return"string"==typeof(t="string"==typeof t&&e&&e[t]?e[t]:t)?{type:t}:"function"==typeof t?{type:t.name,exec:t}:t}function s(t){return e=>t===e}function r(t){return"string"==typeof t?{type:t}:t}function a(t,e){return{value:t,context:e,actions:[],changed:!1,matches:s(t)}}function c(t,e,n){let i=e,o=!1;return[t.filter(t=>{if("xstate.assign"===t.type){o=!0;let e=Object.assign({},i);return"function"==typeof t.assignment?e=t.assignment(i,n):Object.keys(t.assignment).forEach(o=>{e[o]="function"==typeof t.assignment[o]?t.assignment[o](i,n):t.assignment[o]}),i=e,!1}return!0}),i,o]}const u=(t,e)=>t.actions.forEach(({exec:n})=>n&&n(t.context,e));t.assign=function(t){return{type:"xstate.assign",assignment:t}},t.createMachine=function(t,e={}){const[u,f]=c(i(t.states[t.initial].entry).map(t=>o(t,e.actions)),t.context,n),p={config:t,_options:e,initialState:{value:t.initial,actions:u,context:f,matches:s(t.initial)},transition:(e,n)=>{const{value:u,context:f}="string"==typeof e?{value:e,context:t.context}:e,l=r(n),g=t.states[u];if(g.on){const e=i(g.on[l.type]);for(const n of e){if(void 0===n)return a(u,f);const{target:e,actions:r=[],cond:d=(()=>!0)}="string"==typeof n?{target:n}:n,y=void 0===e,x=null!=e?e:u,S=t.states[x];if(d(f,l)){const t=(y?i(r):[].concat(g.exit,r,S.entry).filter(t=>t)).map(t=>o(t,p._options.actions)),[n,a,d]=c(t,f,l),x=null!=e?e:u;return{value:x,context:a,actions:n,changed:e!==u||n.length>0||d,matches:s(x)}}}}return a(u,f)}};return p},t.interpret=function(e){let i=e.initialState,o=t.InterpreterStatus.NotStarted;const a=new Set,c={_machine:e,send:n=>{o===t.InterpreterStatus.Running&&(i=e.transition(i,n),u(i,r(n)),a.forEach(t=>t(i)))},subscribe:t=>(a.add(t),t(i),{unsubscribe:()=>a.delete(t)}),start:r=>{if(r){const t="object"==typeof r?r:{context:e.config.context,value:r};i={value:t.value,actions:[],context:t.context,matches:s(t.value)}}else i=e.initialState;return o=t.InterpreterStatus.Running,u(i,n),c},stop:()=>(o=t.InterpreterStatus.Stopped,a.clear(),c),get state(){return i},get status(){return o}};return c},Object.defineProperty(t,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).XStateFSM={});
