import { __awaiter, __extends, __generator, __spreadArrays } from "tslib";
import { AbstractControl } from './abstract-control';
var FormArray = /** @class */ (function (_super) {
    __extends(FormArray, _super);
    function FormArray(_controls, validators, options) {
        if (_controls === void 0) { _controls = []; }
        if (validators === void 0) { validators = []; }
        var _this = _super.call(this, validators, options) || this;
        _this._controls = _controls;
        _this.validators = validators;
        _this.setupControlsParent();
        return _this;
    }
    FormArray.prototype.setupControlsParent = function () {
        var _this = this;
        this.controls.forEach(function (c) { return c.parent = _this; });
    };
    Object.defineProperty(FormArray.prototype, "value", {
        /**
         * Return an agregate values of all enabled controls.
         */
        get: function () {
            return this._controls.filter(function (c) { return c.enabled; }).map(function (c) { return c.value; });
        },
        set: function (value) {
            throw Error('Assigning a value to a FormArray is not yet implemented');
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormArray.prototype, "valid", {
        get: function () {
            if (!this.enabled || this.readonly) {
                return true;
            }
            else {
                return (this.validators.every(function (v) { return !!v.lastCheck; })
                    &&
                        this._controls.every(function (c) { return c.valid; }));
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormArray.prototype, "enabled", {
        get: function () {
            return this._enabled;
        },
        set: function (isEnabled) {
            this._enabled = isEnabled;
            this._controls.forEach(function (c) { return c.enabled = isEnabled; });
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormArray.prototype, "waiting", {
        get: function () {
            return this._waiting;
        },
        set: function (isWaiting) {
            this._waiting = isWaiting;
            this._controls.forEach(function (c) { return c.waiting = isWaiting; });
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormArray.prototype, "readonly", {
        get: function () {
            return this._readonly;
        },
        set: function (isReadonly) {
            this._readonly = isReadonly;
            this._controls.forEach(function (c) { return c.readonly = isReadonly; });
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormArray.prototype, "touched", {
        get: function () {
            return this.controls.every(function (c) { return c.touched; });
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormArray.prototype, "errors", {
        get: function () {
            return (this.enabled && !this.readonly) ? this._errors : [];
        },
        set: function (errors) {
            this._errors = __spreadArrays(errors);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormArray.prototype, "errorsDeep", {
        get: function () {
            if (!this.enabled || this.readonly) {
                return [];
            }
            var errors = __spreadArrays(this.errors);
            this.controls.forEach(function (control) {
                errors = __spreadArrays(errors, control.errorsDeep);
            });
            return errors;
        },
        enumerable: false,
        configurable: true
    });
    FormArray.prototype.getControl = function (name) {
        throw Error('Getting a control from a FormArray is not yet implemented');
    };
    FormArray.prototype.submit = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _super.prototype.submit.call(this);
                        return [4 /*yield*/, Promise.all(this.controls.map(function (c) { return c.submit(); }))];
                    case 1:
                        _a.sent();
                        if (!!this._hasAnyControlsInError()) return [3 /*break*/, 3];
                        this.validate();
                        return [4 /*yield*/, this.validateAsync()];
                    case 2:
                        _a.sent();
                        _a.label = 3;
                    case 3: return [2 /*return*/];
                }
            });
        });
    };
    FormArray.prototype.reset = function () {
        _super.prototype.reset.call(this);
        this._controls.forEach(function (c) { return c.reset(); });
    };
    FormArray.prototype.initEdition = function () {
        _super.prototype.initEdition.call(this);
    };
    FormArray.prototype.endEdition = function () {
        if (!this.touched) {
            return;
        }
        _super.prototype.endEdition.call(this);
    };
    Object.defineProperty(FormArray.prototype, "controls", {
        get: function () {
            return this._controls;
        },
        enumerable: false,
        configurable: true
    });
    FormArray.prototype.addControl = function (control, indexInsertion) {
        if (indexInsertion === void 0) { indexInsertion = undefined; }
        control.parent = this;
        if (indexInsertion === undefined) {
            this._controls = this._controls.concat(control);
        }
        else {
            this._controls.splice(indexInsertion, 0, control);
        }
    };
    FormArray.prototype.removeControl = function (index) {
        if (this._controls[index] !== undefined) {
            var removedControl = this._controls.splice(index, 1)[0];
            return removedControl;
        }
        else {
            throw Error("There is no control with index= " + index + " in this array");
        }
    };
    FormArray.prototype._hasAnyControlsInError = function () {
        return this.controls.some(function (c) { return c.hasError(); });
    };
    return FormArray;
}(AbstractControl));
export { FormArray };
//# sourceMappingURL=form-array.js.map