'use strict';
function ownKeys(t, e) {
    var r = Object.keys(t);
    if (Object.getOwnPropertySymbols) {
        var n = Object.getOwnPropertySymbols(t);
        if (e) {
            n = n.filter(function (e) {
                return Object.getOwnPropertyDescriptor(t, e).enumerable;
            });
        }
        r.push.apply(r, n);
    }
    return r;
}
function _objectSpread(t) {
    for (var e = 1; e < arguments.length; e++) {
        if (null != arguments[e]) {
            var r = arguments[e];
        } else {
            var r = {};
        }
        if (e % 2) {
            ownKeys(r, true).forEach(function (e) {
                _defineProperty(t, e, r[e]);
            });
        } else {
            Object.getOwnPropertyDescriptors
                ? Object.defineProperties(t, Object.getOwnPropertyDescriptors(r))
                : ownKeys(r).forEach(function (e) {
                      Object.defineProperty(t, e, Object.getOwnPropertyDescriptor(r, e));
                  });
        }
    }
    return t;
}
function _defineProperty(e, t, r) {
    t in e
        ? Object.defineProperty(e, t, {
              value: r,
              enumerable: true,
              configurable: true,
              writable: true
          })
        : (e[t] = r);
    return e;
}
function _classCallCheck(e, t) {
    if (!(e instanceof t)) {
        throw new TypeError('Cannot call a class as a function');
    }
}
function _defineProperties(e, t) {
    for (var r = 0; r < t.length; r++) {
        var n = t[r];
        n.enumerable = n.enumerable || false;
        n.configurable = true;
        if ('value' in n) {
            n.writable = true;
        }
        Object.defineProperty(e, n.key, n);
    }
}
function _createClass(e, t, r) {
    if (t) {
        _defineProperties(e.prototype, t);
    }
    if (r) {
        _defineProperties(e, r);
    }
    return e;
}
Object.defineProperty(exports, '__esModule', {
    value: true
});
exports.default = createFieldsStore;
var FieldsStore = (function () {
    function t() {
        if (0 < arguments.length && void 0 !== arguments[0]) {
            var e = arguments[0];
        } else {
            var e = {};
        }
        _classCallCheck(this, t);
        this.fields = e;
    }
    _createClass(t, [
        {
            key: 'setFields',
            value: function (e) {
                Object.assign(this.fields, e);
            }
        },
        {
            key: 'updateFields',
            value: function (e) {
                this.fields = e;
            }
        },
        {
            key: 'clearField',
            value: function (e) {
                delete this.fields[e];
            }
        },
        {
            key: 'getValueFromFields',
            value: function (e, t) {
                var r = t[e];
                return r && 'value' in r ? r.value : r.initialValue;
            }
        },
        {
            key: 'getAllFieldsName',
            value: function () {
                var e = this.fields;
                return e ? Object.keys(e) : [];
            }
        },
        {
            key: 'getField',
            value: function (e) {
                return _objectSpread({}, this.fields[e], {
                    name: e
                });
            }
        },
        {
            key: 'getFieldValuePropValue',
            value: function (e) {
                var t = e.name;
                var r = e.valuePropName;
                var n = this.getField(t);
                return _defineProperty({}, r, 'value' in n ? n.value : n.initialValue);
            }
        },
        {
            key: 'getFieldValue',
            value: function (e) {
                return this.getValueFromFields(e, this.fields);
            }
        },
        {
            key: 'getFieldsValue',
            value: function (e) {
                var that = this;
                return (e || this.getAllFieldsName()).reduce(function (e, t) {
                    e[t] = that.getFieldValue(t);
                    return e;
                }, {});
            }
        },
        {
            key: 'resetFields',
            value: function (e) {
                var n = this.fields;
                return (e || this.getAllFieldsName()).reduce(function (e, t) {
                    var r = n[t];
                    if (r) {
                        e[t] = r.initialValue;
                    }
                    return e;
                }, {});
            }
        }
    ]);
    return t;
})();
function createFieldsStore(e) {
    return new FieldsStore(e);
}
