import { normalize } from 'normalizr';
import update from 'immutability-helper';
import ActionTypes from '../constants/ActionTypes';
import { getTagIds, getTagById } from '../selectors/tags';
import * as schemas from '../schemas';
import isFetching from './helpers/isFetching';

export default new Map([
  [ActionTypes.INVALIDATE_TAGS, state =>
    update(state, {
      $merge: {
        didInvalidate: true
      }
    })
  ],
  [ActionTypes.REQUEST_TAGS, state =>
    update(state, {
      $merge: {
        isFetching: true
      }
    })
  ],
  [ActionTypes.RECEIVE_TAGS, (state, action) =>
    update(state, {
      $merge: {
        isFetching: false,
        didInvalidate: false,
        items: normalize(action.items, [schemas.tag])
      }
    })
  ],
  [ActionTypes.POSTED_TAG_TO_RESTAURANT, (state, action) =>
    update(state, {
      items: {
        entities: {
          tags: {
            [action.id]: {
              restaurant_count: {
                $set: parseInt(getTagById({ tags: state }, action.id).restaurant_count, 10) + 1
              }
            }
          }
        }
      }
    })
  ],
  [ActionTypes.POSTED_NEW_TAG_TO_RESTAURANT, (state, action) =>
    update(state, {
      items: {
        result: {
          $push: [action.tag.id]
        },
        entities: {
          tags: state.items.entities.tags ? {
            $merge: {
              [action.tag.id]: action.tag
            }
          } : {
            $set: {
              [action.tag.id]: action.tag
            }
          }
        }
      }
    })
  ],
  [ActionTypes.DELETED_TAG_FROM_RESTAURANT, (state, action) =>
    update(state, {
      isFetching: {
        $set: false
      },
      items: {
        entities: {
          tags: {
            [action.id]: {
              $merge: {
                restaurant_count:
                  parseInt(state.items.entities.tags[action.id].restaurant_count, 10) - 1
              }
            }
          }
        }
      }
    })
  ],
  [ActionTypes.DELETE_TAG, isFetching],
  [ActionTypes.TAG_DELETED, (state, action) =>
  {
    const tagIndex = getTagIds({ tags: state }).indexOf(action.id);
    let items = {};
    if (tagIndex !== -1) {
      items = {
        result: {
          $splice: [[tagIndex, 1]]
        }
      };
    }
    const newState = {
      isFetching: {
        $set: false
      },
      items
    };
    return update(state, newState)
  }
  ]
]);

@@ -2078,18 +2078,22 @@
 n) =%3E%0A  
-%7B%0A
+  %7B%0A  
     cons
@@ -2160,12 +2160,107 @@
     
-let 
+  const newState = %7B%0A        isFetching: %7B%0A          $set: false%0A        %7D%0A      %7D;%0A      newState.
 item
@@ -2267,16 +2267,18 @@
 s = %7B%7D;%0A
+  
     if (
@@ -2298,24 +2298,35 @@
 -1) %7B%0A      
+ 
+ newState.
 items = %7B%0A  
@@ -2319,24 +2319,26 @@
 e.items = %7B%0A
+  
         resu
@@ -2353,16 +2353,18 @@
         
+ 
+ 
 $splice:
@@ -2388,99 +2388,34 @@
     
-    %7D%0A
       %7D
-;
 %0A    
-%7D%0A
     
-const newState = %7B%0A      isFetching: %7B%0A        $set: false
+%7D;
 %0A      %7D
 ,%0A  
@@ -2414,29 +2414,11 @@
    %7D
-,
 %0A  
-    items%0A    %7D;%0A
     
@@ -2448,16 +2448,18 @@
 wState)%0A
+  
   %7D%0A  %5D%0A

update tags.js