"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
/**
 * Class for choosing the first valid result returned by an array of operations.
 * If the first result is invalid, the returned result will be marked as invalid
 */
var ValidResultOperation = /** @class */ (function () {
    /**
     * Constructor for ValidResultOperation. Sets the field operations.
     */
    function ValidResultOperation(operations) {
        this.operations = operations;
    }
    ValidResultOperation.prototype.executeStart = function (request) {
        var e_1, _a;
        try {
            for (var _b = tslib_1.__values(this.operations), _c = _b.next(); !_c.done; _c = _b.next()) {
                var operation = _c.value;
                operation.executeStart(request);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_1) throw e_1.error; }
        }
    };
    ValidResultOperation.prototype.executeMotion = function (request) {
        var e_2, _a;
        var tResult;
        var tFirstResult;
        try {
            for (var _b = tslib_1.__values(this.operations), _c = _b.next(); !_c.done; _c = _b.next()) {
                var operation = _c.value;
                tResult = operation.executeMotion(request);
                if (!tFirstResult) {
                    tFirstResult = tResult;
                }
                if (!tResult.isInvalid) {
                    break;
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_2) throw e_2.error; }
        }
        if (tResult !== tFirstResult) {
            tResult.isInvalid = true;
        }
        // prefer the first result, if all other are invalid
        if (tResult.isInvalid) {
            return tFirstResult;
        }
        return tResult;
    };
    ValidResultOperation.prototype.executeEnd = function (request) {
        var e_3, _a;
        var tResult;
        var tFirstResult;
        try {
            for (var _b = tslib_1.__values(this.operations), _c = _b.next(); !_c.done; _c = _b.next()) {
                var operation = _c.value;
                tResult = operation.executeEnd(request);
                if (!tFirstResult) {
                    tFirstResult = tResult;
                }
                if (!tResult.isInvalid) {
                    break;
                }
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_3) throw e_3.error; }
        }
        // if all results are invalid we want to give priority to the first operation
        if (tResult.isInvalid) {
            return tFirstResult;
        }
        return tResult;
    };
    return ValidResultOperation;
}());
exports.ValidResultOperation = ValidResultOperation;
