import { Map } from 'immutable';

import {
  GET_ITEMS,
  ITEMS_LOADED,
} from '../constants/actionTypes.js';

export const items = (state = new Map(), action) => {
  switch (action.type) {
    case ITEMS_LOADED:
      const mapped = action.payload.toMap().mapEntries((entry) => {
        const [, item] = entry;
        return [`${item.id}`, item];
      });
      return state.merge(mapped);
    case GET_ITEMS:
    default:
      return state;
  }
};
