import { property, flow, set, mergeWith, isString } from 'lodash/fp';
const createPathLense = path => ({
select: property(path),
update: (state, value) => set(path, value, state),
});
export const createModuleFactory = (
selectors,
reducer = selectors,
) => (lense) => {
const { select, update } = isString(lense) ? createPathLense(lense) : lense;
const reduce = (state, action) => {
const cursor = select(state, action);
const newStateAtCursor = reducer(cursor, action);
if (newStateAtCursor === cursor) {
return state;
}
return update(state, newStateAtCursor);
};
selectors = mergeWith( //eslint-disable-line
selector => flow(select, selector), {}, selectors,
);
return Object.assign(reduce, selectors);
};