import { __assign, __awaiter, __extends, __generator, __spreadArrays } from "tslib";
import { AbstractControl } from './abstract-control';
var FormControl = /** @class */ (function (_super) {
    __extends(FormControl, _super);
    function FormControl(validators, options) {
        if (validators === void 0) { validators = []; }
        var _this = _super.call(this, validators, options) || this;
        _this.validators = validators;
        if (options) {
            if (options.initialValue !== undefined) {
                _this.initialValue = options.initialValue;
            }
        }
        else {
            // ensure reactivity
            _this._value = undefined;
        }
        return _this;
    }
    FormControl.prototype.clone = function (oldObject) {
        var newObject = __assign({}, oldObject);
        Object.setPrototypeOf(newObject, Object.getPrototypeOf(oldObject));
        return newObject;
    };
    Object.defineProperty(FormControl.prototype, "initialValue", {
        set: function (initialValue) {
            if (Array.isArray(initialValue)) {
                this._initialValue = __spreadArrays(initialValue);
                this._value = __spreadArrays(initialValue);
                this._oldValue = __spreadArrays(initialValue);
            }
            else if (initialValue instanceof Object) {
                this._initialValue = this.clone(initialValue);
                this._value = this.clone(initialValue);
                this._oldValue = this.clone(initialValue);
            }
            else {
                this._initialValue = initialValue;
                this._value = initialValue;
                this._oldValue = initialValue;
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormControl.prototype, "touched", {
        get: function () {
            return this._touched;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormControl.prototype, "controls", {
        get: function () {
            return [];
        },
        enumerable: false,
        configurable: true
    });
    FormControl.prototype.getControl = function (name) {
        return undefined;
    };
    Object.defineProperty(FormControl.prototype, "value", {
        get: function () {
            return this._value;
        },
        set: function (value) {
            if (value === this.value && typeof value !== 'object') {
                return;
            }
            this._oldValue = this._value;
            this._value = value;
            this.upwardValueChanged();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormControl.prototype, "valid", {
        get: function () {
            if (!this.enabled || this.readonly) {
                return true;
            }
            else {
                return this.validators.every(function (v) { return !!v.lastCheck; });
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormControl.prototype, "enabled", {
        get: function () {
            return this._enabled;
        },
        set: function (isEnabled) {
            this._enabled = isEnabled;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormControl.prototype, "waiting", {
        get: function () {
            return this._waiting;
        },
        set: function (isWaiting) {
            this._waiting = isWaiting;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormControl.prototype, "readonly", {
        get: function () {
            return this._readonly;
        },
        set: function (isReadonly) {
            this._readonly = isReadonly;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormControl.prototype, "errors", {
        get: function () {
            return (this.enabled && !this.readonly) ? this._errors : [];
        },
        set: function (errors) {
            this._errors = __spreadArrays(errors);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FormControl.prototype, "errorsDeep", {
        get: function () {
            return this.errors;
        },
        enumerable: false,
        configurable: true
    });
    /**
     * This specify the ennd of a edition context.
     */
    FormControl.prototype.endEdition = function () {
        this._touched = true;
        _super.prototype.endEdition.call(this);
    };
    /**
     * Reset the field to it's orginal pristine state.
     *
     * @param {T} [initialValue]  a new initial value for the field
     */
    FormControl.prototype.reset = function (initialValue) {
        _super.prototype.reset.call(this);
        this._touched = false;
        if (typeof initialValue === 'undefined') {
            this._value = this._initialValue;
            this._oldValue = this._initialValue;
        }
        else {
            this.initialValue = initialValue;
        }
    };
    /**
     * Run all validators
     *
     */
    FormControl.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);
                        this.validate();
                        return [4 /*yield*/, this.validateAsync()];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    return FormControl;
}(AbstractControl));
export { FormControl };
//# sourceMappingURL=form-control.js.map