text: import * as actions from '../actions/eotcActions';
  import { distinct } from '../../helpers/arrayHelper';
  
  const initialState = {
    priceUpdates: {},
    lastPrices: {}
  };
  
  const formatPrice = priceUpdate => ({
    Time: priceUpdate[0],
    Bid: priceUpdate[2],
    Ask: priceUpdate[3],
    Mid: priceUpdate[4]
  });
  
  const isPriceNewer = (lastPrice, newPrice) => {
    if (!lastPrice) return true;
    if (lastPrice.Time < newPrice[0]) return true;
    return false;
  };
  
  export default function eotcPriceReducer(
    state = initialState,
    { type, payload }
  ) {
    switch (type) {
      case actions.ADD_PRICE:
        const InstrumentId = payload[1];
  
        const stateUpdate = {
          ...state,
          priceUpdates: {
            [InstrumentId]: distinct(
              [formatPrice(payload), ...(state.priceUpdates[InstrumentId] || [])],
              /**
               * Treat timestamp as unique ID.
               */
              price => price.Time
            )
          }
        };
  
        if (isPriceNewer(state.lastPrices[InstrumentId], payload)) {
          /**
           * Save as last price in state.
           */
          stateUpdate.lastPrices[InstrumentId] = formatPrice(payload);
        }
  
        return stateUpdate;
  
      default:
        return state;
    }
  }
  