const {PUSH, POP, REPLACE} = require('../constants/navigation');

module.exports = function navigate(state, {type, payload}) {
  if (!state) {
    return {__nav: {}};
  }

  const stack = state.__nav.stack;
  const index = state.__nav.index;

  switch (type) {
    case PUSH:
      return Object.assign({}, state, {
        __nav: {
          stack: stack.unshift(payload),
          index: 0,
        },
      });

    case POP:
      const incremented = index + 1;

      if (incremented === stack.count()) {
        return state;
      }

      return Object.assign({}, state, {
        __nav: {
          stack: stack,
          index: incremented,
        },
      });

    case REPLACE:
      if (!stack.count()) {
        return state;
      }

      return Object.assign({}, state, {
        __nav: {
          stack: stack.splice(index, 1, payload),
          index: index,
        },
      });

    default:
      return state;
  }
};

@@ -152,17 +152,8 @@
 rn %7B
-__nav: %7B%7D
 %7D;%0A 
@@ -178,22 +178,16 @@
 = state.
-__nav.
 stack;%0A 
@@ -211,14 +211,8 @@
 ate.
-__nav.
 inde
@@ -288,35 +288,16 @@
 tate, %7B%0A
-        __nav: %7B%0A  
         
@@ -331,26 +331,24 @@
 d),%0A        
-  
 index: 0,%0A  
@@ -345,27 +345,16 @@
 dex: 0,%0A
-        %7D,%0A
       %7D)
@@ -522,35 +522,16 @@
 tate, %7B%0A
-        __nav: %7B%0A  
         
@@ -544,18 +544,16 @@
  stack,%0A
-  
         
@@ -572,27 +572,16 @@
 mented,%0A
-        %7D,%0A
       %7D)
@@ -704,27 +704,8 @@
 , %7B%0A
-        __nav: %7B%0A  
     
@@ -752,26 +752,24 @@
 d),%0A        
-  
 index: index
@@ -774,19 +774,8 @@
 ex,%0A
-        %7D,%0A
     

Remove __nav prefix due to conflicts with combineReducers function