var FlowService = (function () {
    function FlowService(flow, scope) {
        this.flow = flow;
        this.scope = scope;
    }
    FlowService.prototype.start = function () {
        return this.nextFromCurrentKey("start");
    };
    FlowService.prototype.next = function () {
        return this.nextFromCurrentKey(this.current);
    };
    FlowService.prototype.nextFromCurrentKey = function (currentKey) {
        var _this = this;
        var nextArr = this.flow.transitions
            .filter(function (transition) {
            return (transition.from === currentKey);
        })
            .filter(function (transition) {
            if (!transition.condition) {
                return true;
            }
            else {
                return _this.evaluate(_this.scope, transition.condition);
            }
        });
        if (nextArr.length === 0) {
            throw Error('no destination state found from "' + currentKey + '" state');
        }
        if (nextArr.length > 1) {
            throw Error('more then one destination state found from "' + currentKey + '" state');
        }
        this.current = nextArr[0].to;
        return this.current;
    };
    FlowService.prototype.go = function (state) {
        this.current = state;
        return state;
    };
    FlowService.prototype.evaluate = function (scope, condition) {
        var result = false;
        try {
            result = function (scope) {
                result = eval('(' + condition + ')');
                return result;
            }(this.scope);
        }
        catch (err) {
            console.log('undefined variable when evaluating condition');
        }
        return result;
    };
    return FlowService;
}());
export { FlowService };
//# sourceMappingURL=flow.service.js.map