import { Children } from 'react';
import { createSelector } from 'reselect';

const getDefaultValues = (children, data = {}, defaultValue = {}) => {
    const globalDefaultValue = typeof defaultValue === 'function' ? defaultValue() : defaultValue;
    const defaultValueFromChildren = children
        .map(child => ({ source: child.props.source, defaultValue: child.props.defaultValue }))
        .reduce((prev, next) => {
            if (next.defaultValue != null) {
                prev[next.source] = typeof next.defaultValue === 'function' ? next.defaultValue() : next.defaultValue; // eslint-disable-line no-param-reassign
            }
            return prev;
        }, {});
    return { ...globalDefaultValue, ...defaultValueFromChildren, ...data };
};

const getChildren = (state, props) => props.children;
const getRecord = (state, props) => props.record;
const getDefaultValue = (state, props) => props.defaultValue;


export default createSelector(
    getChildren, getRecord, getDefaultValue,
    (children, record, defaultValue) => getDefaultValues(Children.toArray(children), record, defaultValue)
);

@@ -69,16 +69,46 @@
 select';
+%0Aimport set from 'lodash.set';
 %0A%0Aconst 
@@ -512,27 +512,91 @@
     
-prev%5Bnext.source%5D =
+set(%0A                    prev,%0A                    next.source,%0A                   
  typ
@@ -677,50 +677,28 @@
 alue
-; // eslint-disable-line no-param-reassign
+,%0A                );
 %0A   
@@ -1069,24 +1069,24 @@
 faultValue,%0A
-
     (childre
@@ -1179,12 +1179,13 @@
 ltValue)
+,
 %0A);%0A

fix incorrect defaultValue assignment for nested resource attributes in SimpleForm input components