import compose from './compose';

/**
 * Creates a store enhancer that applies middleware to the dispatch method
 * of the Redux store. This is handy for a variety of tasks, such as expressing
 * asynchronous actions in a concise manner, or logging every action payload.
 *
 * See `redux-thunk` package as an example of the Redux middleware.
 *
 * Because middleware is potentially asynchronous, this should be the first
 * store enhancer in the composition chain.
 *
 * Note that each middleware will be given the `dispatch` and `getState` functions
 * as named arguments.
 *
 * @param {...Function} middlewares The middleware chain to be applied.
 * @returns {Function} A store enhancer applying the middleware.
 */
export default function applyMiddleware(...middlewares) {
  return function applyGivenMiddleware(createStore) {
    middlewares = middlewares.slice();
    middlewares.reverse();

    return function createStoreWithMiddleware(reducer, initialState) {
      const store = createStore(reducer, initialState);

      let dispatch = store.dispatch;
      let middlewareAPI = {
        getState: store.getState,
        dispatch: (action) => dispatch(action)
      };
      middlewares.forEach(middleware =>
        dispatch = middleware(store)(dispatch)
      );

      return Object.assign({}, store, { dispatch });
    };
  }
}

@@ -782,165 +782,18 @@
 urn 
-function applyGivenMiddleware(createStore) %7B%0A    middlewares = middlewares.slice();%0A    middlewares.reverse();%0A%0A    return function createStoreWithMiddleware
+(next) =%3E 
 (red
@@ -815,22 +815,21 @@
 ate)
+ =%3E
  %7B%0A    
- 
- const
+var
  sto
@@ -837,19 +837,12 @@
 e = 
-createStore
+next
 (red
@@ -862,26 +862,23 @@
 State);%0A
-%0A
     
-  let
+var
  dispatc
@@ -905,13 +905,32 @@
     
-  let
+var chain = %5B%5D;%0A%0A    var
  mid
@@ -944,18 +944,16 @@
 API = %7B%0A
-  
       ge
@@ -976,18 +976,16 @@
 tState,%0A
-  
       di
@@ -1029,18 +1029,22 @@
     
-  
 %7D;%0A    
- 
+chain =
  mid
@@ -1056,15 +1056,11 @@
 res.
-forEach
+map
 (mid
@@ -1073,46 +1073,76 @@
 e =%3E
-%0A        dispatch = middleware(
+ middleware(middlewareAPI));%0A    dispatch = compose(...chain, 
 store
-)(
+.
 disp
@@ -1150,21 +1150,11 @@
 tch)
-%0A      )
 ;%0A%0A
-  
     
@@ -1164,34 +1164,31 @@
 urn 
-Object.assign(%7B%7D, store, %7B
+%7B%0A      ...store,%0A     
  dis
@@ -1196,12 +1196,8 @@
 atch
- %7D);
 %0A   
@@ -1203,11 +1203,12 @@
   %7D;%0A  %7D
+;
 %0A%7D%0A

Fix accidental broken impl