import { __extends } from "tslib";
import { BasicBuilder } from './basic';
import { FieldSetModel } from '../models';
import { Some, None, or } from '../maybe';
import { hasOwnProperty } from '../../../utils/hasOwn';
var FieldSetBuilder = (function (_super) {
    __extends(FieldSetBuilder, _super);
    function FieldSetBuilder(_childBuilders) {
        var _this = _super.call(this) || this;
        _this._childBuilders = _childBuilders;
        return _this;
    }
    FieldSetBuilder.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(None());
            }
        });
        var model = new FieldSetModel(children);
        model.validators = this._validators;
        model.normalizeBeforeSubmit = this._normalizeBeforeSubmit;
        model.builder = this;
        return model;
    };
    FieldSetBuilder.prototype.get = function (name) {
        return this._childBuilders[name];
    };
    return FieldSetBuilder;
}(BasicBuilder));
export { FieldSetBuilder };
