import forEach from 'lodash/collection/forEach';
import isEmpty from 'lodash/lang/isEmpty';
import isFunction from 'lodash/lang/isFunction';
import snakeCase from 'lodash/string/snakeCase';
import { Store, toImmutable } from 'nuclear-js';

export default function createStore(initialState, handlers) {
  const immutableState = toImmutable(initialState);

  const spec = {
    getInitialState() {
      return immutableState;
    },

    initialize() {
      if (!isEmpty(handlers)) {
        forEach(handlers, (handler, handlerName) => {
          const ACTION_NAME = snakeCase(handlerName || '').toUpperCase();

          if (!ACTION_NAME) {
            throw new Error('Frux#createStore: handler must be a named function.');
          }

          if (isFunction(handler)) {
            this.on(ACTION_NAME, (currentState, payload) => {
              return handler.call(null, currentState, payload, immutableState);
            });
          }
        });
      }
    }
  };

  return new Store(spec);
}

@@ -41,17 +41,16 @@
 orEach';
-%0D
 %0Aimport 
@@ -84,17 +84,16 @@
 sEmpty';
-%0D
 %0Aimport 
@@ -133,17 +133,16 @@
 nction';
-%0D
 %0Aimport 
@@ -182,17 +182,16 @@
 keCase';
-%0D
 %0Aimport 
@@ -231,19 +231,17 @@
 ear-js';
-%0D%0A%0D
+%0A
 %0Aexport 
@@ -294,17 +294,16 @@
 dlers) %7B
-%0D
 %0A  const
@@ -346,19 +346,17 @@
 lState);
-%0D%0A%0D
+%0A
 %0A  const
@@ -364,17 +364,16 @@
 spec = %7B
-%0D
 %0A    get
@@ -384,25 +384,24 @@
 ialState() %7B
-%0D
 %0A      retur
@@ -421,19 +421,16 @@
 ate;
-%0D
 %0A    %7D,
-%0D%0A%0D
+%0A
 %0A   
@@ -444,17 +444,16 @@
 lize() %7B
-%0D
 %0A      i
@@ -472,25 +472,24 @@
 handlers)) %7B
-%0D
 %0A        for
@@ -524,23 +524,27 @@
 dlerName
+ = ''
 ) =%3E %7B
-%0D
 %0A       
@@ -591,14 +591,8 @@
 Name
- %7C%7C ''
 ).to
@@ -603,19 +603,17 @@
 rCase();
-%0D%0A%0D
+%0A
 %0A       
@@ -634,17 +634,16 @@
 _NAME) %7B
-%0D
 %0A       
@@ -718,17 +718,16 @@
 tion.');
-%0D
 %0A       
@@ -730,19 +730,17 @@
        %7D
-%0D%0A%0D
+%0A
 %0A       
@@ -768,17 +768,16 @@
 dler)) %7B
-%0D
 %0A       
@@ -830,17 +830,16 @@
 ad) =%3E %7B
-%0D
 %0A       
@@ -910,17 +910,16 @@
 eState);
-%0D
 %0A       
@@ -922,25 +922,24 @@
          %7D);
-%0D
 %0A          %7D
@@ -934,25 +934,24 @@
 %0A          %7D
-%0D
 %0A        %7D);
@@ -954,17 +954,16 @@
  %7D);
-%0D
 %0A      %7D
 %0D%0A  
@@ -962,24 +962,20 @@
    %7D
-%0D
 %0A    %7D
-%0D
 %0A  %7D;
-%0D%0A%0D
+%0A
 %0A  r
@@ -1000,9 +1000,7 @@
 ec);
-%0D
 %0A%7D
-%0D
 %0A

Replace line endings to lf and add default param as empty string for `handlerName`