text: /**
   * machina - A library for creating powerful and flexible finite state machines. Loosely inspired by Erlang/OTP's gen_fsm behavior.
   * Author: Jim Cowart (http://ifandelse.com)
   * Version: v1.1.1
   * Url: http://machina-js.org/
   * License(s): MIT, GPL
   */
  (function(t,e){"function"==typeof define&&define.amd?define(["lodash"],function(n){return e(n,t)}):"object"==typeof module&&module.exports?module.exports=e(require("lodash")):t.machina=e(t._,t)})(this,function(t,e,n){function i(){return{initialState:"uninitialized",eventListeners:{"*":[]},states:{},namespace:S.makeFsmNamespace(),useSafeEmit:!1,hierarchy:{},pendingDelegations:{}}}function s(){return{inputQueue:[],targetReplayState:"",state:n,priorState:n,priorAction:"",currentAction:"",currentActionArgs:n,inExitHandler:!1}}function a(t,e){for(var n=[],i=e||0;i<t.length;i++)n[i]=t[i];return n}function r(t){if(t){var e={};return"object"==typeof t?t.factory?e=t:e.factory=function(){return t}:"function"==typeof t&&(e.factory=t),e.instance=e.factory(),e}}function o(t,e){return e.on("*",function(e,n){switch(e){case"nohandler":n.ticket||n.delegated||n.namespace===t.namespace||(n.args[1].bubbling=!0),"_reset"!==n.inputType&&t.handle.apply(t,n.args);break;case"handling":var i=n.ticket;i&&t.pendingDelegations[i]&&delete t.pendingDelegations[i],t.emit(e,n);break;default:t.emit(e,n)}})}function u(){for(var t=[],e="0123456789abcdef",n=0;36>n;n++)t[n]=e.substr(Math.floor(16*Math.random()),1);return t[14]="4",t[19]=e.substr(3&t[19]|8,1),t[8]=t[13]=t[18]=t[23]="-",t.join("")}function c(e){t.extend(this,e),t.defaults(this,i()),this.initialize.apply(this,arguments),E.emit(v,this)}var l=[].slice,h="transition",p="handling",f="handled",d="nohandler",g="transition",m="invalidstate",y="deferred",v="newfsm",A=["states","initialState"],b=function(e,n){var i,s=this,a={},r=function(){};return i=e&&e.hasOwnProperty("constructor")?e.constructor:function(){var e=l.call(arguments,0);e[0]=e[0]||{};var n,i=e[0].states||{};n=t.merge(t.cloneDeep(a),{states:i}),n.initialState=e[0].initialState||this.initialState,t.extend(e[0],n),s.apply(this,e)},t.merge(i,s),r.prototype=s.prototype,i.prototype=new r,e&&(t.extend(i.prototype,e),t.merge(a,t.transform(e,function(t,e,n){-1!==A.indexOf(n)&&(t[n]=e)}))),n&&t.merge(i,n),i.prototype.constructor=i,i.__super__=s.prototype,i},S={makeFsmNamespace:function(){var t=0;return function(){return"fsm."+t++}}(),listenToChild:o,getLeaklessArgs:a,getDefaultOptions:i,getDefaultClientMeta:s,createUUID:u},_={emit:function(e){var n=a(arguments);this.eventListeners["*"]&&t.each(this.eventListeners["*"],function(t){if(this.useSafeEmit)try{t.apply(this,n)}catch(e){console&&"undefined"!=typeof console.log&&console.log(e.stack)}else t.apply(this,n)},this),this.eventListeners[e]&&t.each(this.eventListeners[e],function(t){if(this.useSafeEmit)try{t.apply(this,n.slice(1))}catch(e){console&&"undefined"!=typeof console.log&&console.log(e.stack)}else t.apply(this,n.slice(1))},this)},on:function(t,e){var n=this;return n.eventListeners=n.eventListeners||{"*":[]},n.eventListeners[t]||(n.eventListeners[t]=[]),n.eventListeners[t].push(e),{eventName:t,callback:e,off:function(){n.off(t,e)}}},off:function(e,n){this.eventListeners=this.eventListeners||{"*":[]},e?this.eventListeners[e]=n?t.without(this.eventListeners[e],n):[]:this.eventListeners={}}},k="__machina__";t.extend(c.prototype,{initialize:function(){},initClient:function(t){var e=this.initialState;if(!e)throw new Error("You must specify an initial state for this FSM");if(!this.states[e])throw new Error("The initial state specified does not exist in the states object.");this.transition(t,e)},ensureClientMeta:function(e){if("object"!=typeof e)throw new Error("An FSM client must be an object.");return e[k]=e[k]||{},e[k][this.namespace]||(e[k][this.namespace]=t.cloneDeep(S.getDefaultClientMeta()),this.initClient(e)),e[k][this.namespace]},buildEventPayload:function(e,n){return t.isPlainObject(n)?t.extend(n,{client:e,namespace:this.namespace}):{client:e,data:n||null,namespace:this.namespace}},getHandlerArgs:function(t,e){var n=t.slice(0),i=n[1];return"object"==typeof i&&n.splice(1,1,i.inputType),e?n:[n[0]].concat(n.slice(2))},handle:function(e,i){var s=i;if("undefined"==typeof i)throw new Error("The input argument passed to the FSM's handle method is undefined. Did you forget to pass the input name?");"string"==typeof i&&(s={inputType:i,delegated:!1,ticket:n});var r=this.ensureClientMeta(e),o=a(arguments);"object"!=typeof i&&o.splice(1,1,s),r.currentActionArgs=o.slice(1);var u,c,l,h,g,m=r.state,y=this.states[m],v=!1;if(!r.inExitHandler)if(l=y._child&&y._child.instance,!l||this.pendingDelegations[s.ticket]||s.bubbling){s.ticket&&this.pendingDelegations[s.ticket]&&delete this.pendingDelegations[s.ticket],u=y[s.inputType]?s.inputType:"*",v="*"===u,c=y[u]||this[u]||this["*"],g=r.state+"."+u,r.currentAction=g;var A=this.buildEventPayload(e,{inputType:s.inputType,delegated:s.delegated,ticket:s.ticket});c?(this.emit(p,A),"function"==typeof c?h=c.apply(this,this.getHandlerArgs(o,v)):(h=c,this.transition(e,c)),this.emit(f,A)):this.emit(d,t.extend({args:o},A)),r.priorAction=r.currentAction,r.currentAction=""}else s.ticket=s.ticket||S.createUUID(),s.delegated=!0,this.pendingDelegations[s.ticket]={delegatedTo:l.namespace},h=l.handle.apply(l,o);return h},transition:function(t,e){var n,i=this.ensureClientMeta(t),s=i.state,a=this.states[s],o=this.states[e];if(!i.inExitHandler&&e!==s){if(o){o._child&&(o._child=r(o._child),n=o._child&&o._child.instance),a&&a._onExit&&(i.inExitHandler=!0,a._onExit.call(this,t),i.inExitHandler=!1),a&&a._child&&a._child.instance&&this.hierarchy[a._child.instance.namespace]&&this.hierarchy[a._child.instance.namespace].off(),i.targetReplayState=e,i.priorState=s,i.state=e,n&&(this.hierarchy[n.namespace]=S.listenToChild(this,n));var u=this.buildEventPayload(t,{fromState:i.priorState,action:i.currentAction,toState:e});return this.emit(g,u),o._onEnter&&o._onEnter.call(this,t),n&&n.handle(t,"_reset"),void(i.targetReplayState===e&&this.processQueue(t,h))}this.emit(m,this.buildEventPayload(t,{state:i.state,attemptedState:e}))}},deferUntilTransition:function(t,e){var n=this.ensureClientMeta(t);if(n.currentActionArgs){var i={type:h,untilState:e,args:n.currentActionArgs};n.inputQueue.push(i);var s=this.buildEventPayload(t,{state:n.state,queuedArgs:i});this.emit(y,s)}},deferAndTransition:function(t,e){this.deferUntilTransition(t,e),this.transition(t,e)},processQueue:function(e){var n=this.ensureClientMeta(e),i=function(t){return!t.untilState||t.untilState===n.state},s=t.filter(n.inputQueue,i);n.inputQueue=t.difference(n.inputQueue,s),t.each(s,function(t){this.handle.apply(this,[e].concat(t.args))},this)},clearQueue:function(e,n){var i=this.ensureClientMeta(e);if(n){var s=function(t){return n?t.untilState!==n:!0};i.inputQueue=t.filter(i.inputQueue,s)}else i.inputQueue=[]},compositeState:function(t){var e=this.ensureClientMeta(t),n=e.state,i=this.states[n]._child&&this.states[n]._child.instance;return i&&(n+="."+i.compositeState(t)),n}},_),c.extend=b;var C=c.extend({constructor:function(){c.apply(this,arguments),this.ensureClientMeta()},initClient:function(){var t=this.initialState;if(!t)throw new Error("You must specify an initial state for this FSM");if(!this.states[t])throw new Error("The initial state specified does not exist in the states object.");this.transition(t)},ensureClientMeta:function(){return this._stamped||(this._stamped=!0,t.defaults(this,t.cloneDeep(s())),this.initClient()),this},ensureClientArg:function(t){var e=t;return"object"!=typeof e[0]||"inputType"in e[0]||e[0]===this?("object"!=typeof e[0]||"object"==typeof e[0]&&"inputType"in e[0])&&e.unshift(this):e.splice(0,1,this),e},getHandlerArgs:function(t,e){var n=t,i=n[1];return"object"==typeof inputType&&n.splice(1,1,i.inputType),n.slice(e?1:2)},buildEventPayload:function(){var e=this.ensureClientArg(S.getLeaklessArgs(arguments)),n=e[1];return t.isPlainObject(n)?t.extend(n,{namespace:this.namespace}):{data:n||null,namespace:this.namespace}},handle:function(t){var e=this.ensureClientArg(S.getLeaklessArgs(arguments));return c.prototype.handle.apply(this,e)},transition:function(t){var e=this.ensureClientArg(S.getLeaklessArgs(arguments));return c.prototype.transition.apply(this,e)},deferUntilTransition:function(t){var e=this.ensureClientArg(S.getLeaklessArgs(arguments));return c.prototype.deferUntilTransition.apply(this,e)},processQueue:function(){var t=this.ensureClientArg(S.getLeaklessArgs(arguments));return c.prototype.processQueue.apply(this,t)},clearQueue:function(t){var e=this.ensureClientArg(S.getLeaklessArgs(arguments));return c.prototype.clearQueue.apply(this,e)},compositeState:function(){var t=this.ensureClientArg(S.getLeaklessArgs(arguments));return c.prototype.compositeState.apply(this,t)}}),E=t.merge(_,{Fsm:C,BehavioralFsm:c,utils:S,eventListeners:{newFsm:[]}});return E});