import { __extends } from "tslib";
import { FormModel } from '../models';
import { FieldSetBuilder, } from './set';
import { Some, or } from '../maybe';
import { hasOwnProperty } from '../../../utils/hasOwn';
var FormBuilder = (function (_super) {
    __extends(FormBuilder, _super);
    function FormBuilder() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    FormBuilder.prototype.build = function (defaultValues) {
        var _this = this;
        var defaults = or(defaultValues, function () { return ({}); });
        var children = {};
        Object.keys(this._childBuilders).forEach(function (key) {
            var childBuilder = _this._childBuilders[key];
            if (hasOwnProperty(defaults, key)) {
                children[key] = childBuilder.build(Some(defaults[key]));
            }
            else {
                children[key] = childBuilder.build(null);
            }
        });
        var model = new FormModel(children);
        model.validators = this._validators;
        model.builder = this;
        return model;
    };
    return FormBuilder;
}(FieldSetBuilder));
export { FormBuilder };
