import { __extends } from "tslib";
import { BehaviorSubject } from 'rxjs';
import { FieldSetModel } from './set';
import { ValidateOption } from '../validate';
import uniqueId from '../../../utils/uniqueId';
import { FORM_ID } from './is';
import { createSentinelSubject } from './sentinel-subject';
var FormStrategy;
(function (FormStrategy) {
    FormStrategy[FormStrategy["Model"] = 0] = "Model";
    FormStrategy[FormStrategy["View"] = 1] = "View";
})(FormStrategy || (FormStrategy = {}));
var FormModel = (function (_super) {
    __extends(FormModel, _super);
    function FormModel(children) {
        var _this = _super.call(this, children, uniqueId('form-')) || this;
        _this.children = children;
        _this._displayName = 'FormModel';
        _this.workingValidators = new Set();
        _this.isValidating$ = new BehaviorSubject(false);
        _this.owner = _this;
        var keys = Object.keys(children);
        var keysLength = keys.length;
        for (var index = 0; index < keysLength; index++) {
            var name_1 = keys[index];
            var child = children[name_1];
            _this.registerChild(name_1, child);
        }
        return _this;
    }
    Object.defineProperty(FormModel.prototype, "form", {
        get: function () {
            return this;
        },
        enumerable: false,
        configurable: true
    });
    FormModel.prototype.validate = function (option) {
        if (option === void 0) { option = ValidateOption.Default |
            ValidateOption.IncludeChildrenRecursively; }
        return _super.prototype.validate.call(this, option);
    };
    FormModel.prototype.addWorkingValidator = function (v) {
        this.workingValidators.add(v);
        this.updateIsValidating();
    };
    FormModel.prototype.removeWorkingValidator = function (v) {
        this.workingValidators.delete(v);
        this.updateIsValidating();
    };
    FormModel.prototype.updateIsValidating = function () {
        var isValidating = this.workingValidators.size > 0;
        if (isValidating !== this.isValidating$.getValue()) {
            this.isValidating$.next(isValidating);
        }
    };
    FormModel.prototype.dispose = function () {
        _super.prototype.dispose.call(this);
        this.workingValidators.clear();
        this.isValidating$.complete();
        this.isValidating$ = createSentinelSubject(this._displayName, false);
    };
    return FormModel;
}(FieldSetModel));
FormModel.prototype[FORM_ID] = true;
export { FormStrategy, FormModel };
