import React, {Component} from 'react';
import PropTypes from 'prop-types';

const createStore = (reducer) => {
  let state;
  let listeners = [];

  const getState = () => state;

  const dispatch = action => {
    state = reducer(state, action);
    listeners.forEach(listener => listener());
  };

  const subscribe = listener => {
    listeners.push(listener);
    return () => {
      listeners = listeners.filter(l => l !== listener);
    };
  };

  // get the reducer to return the initial value
  dispatch({});

  return {getState, dispatch, subscribe};
};

export const withNewLocalStore = (reducers) => WrappedComponent => {
  return class LocalStore extends Component {
    static childContextTypes = {
      store: PropTypes.shape({
        subscribe: PropTypes.func,
        dispatch: PropTypes.func,
        getState: PropTypes.func,
      }),
    };

    constructor(props) {
      super(props);

      this.store = createStore(reducers);
    }

    getChildContext() {
      return {
        store: this.store,
      };
    }

    render() {
      return <WrappedComponent {...this.props} />;
    }
  };
};

export const connect = (stateToProps, dispatchToProps) => WrappedComponent => {
  return class Connect extends Component {
    static contextTypes = {
      store: PropTypes.shape({
        subscribe: PropTypes.func,
        dispatch: PropTypes.func,
        getState: PropTypes.func,
      }),
    };

    constructor(props, {store}) {
      super(props);

      this.unsubscribe = store.subscribe(() => this.forceUpdate());
      this.store = store;
    }

    componentWillUnmount() {
      this.unsubscribe();
    }

    mapStateToProps = () => (stateToProps ? stateToProps(this.store.getState(), this.props) : {});

    mapDispatchToProps = () =>
      dispatchToProps
        ? Object.keys(dispatchToProps).reduce((obj, f) => {
            obj[f] = (...args) => dispatchToProps[f](...args)(this.store.dispatch);
            return obj;
          }, {})
        : {};

    render() {
      return <WrappedComponent {...this.props} {...this.mapStateToProps()} {...this.mapDispatchToProps()} />;
    }
  };
};

@@ -1,12 +1,49 @@
+/* eslint react/no-multi-comp: 0 */%0A%0A
 import React
@@ -131,17 +131,15 @@
 e = 
-(
 reducer
-)
  =%3E 
@@ -627,25 +627,24 @@
 Store = 
-(
 reducers
 ) =%3E Wra
@@ -635,17 +635,16 @@
 reducers
-)
  =%3E Wrap
@@ -1870,16 +1870,29 @@
 .reduce(
+%0A            
 (obj, f)
@@ -1891,24 +1891,25 @@
 (obj, f) =%3E 
+(
 %7B%0A          
@@ -1914,16 +1914,36 @@
     
-obj%5Bf%5D =
+  ...obj,%0A              %5Bf%5D:
  (..
@@ -2000,17 +2000,17 @@
 ispatch)
-;
+,
 %0A       
@@ -2018,20 +2018,14 @@
     
-return obj;%0A
+%7D),%0A  
     
@@ -2034,13 +2034,22 @@
     
+%7B
 %7D,
- %7B%7D
+%0A          
 )%0A  

Refactor map dispatch to props for store