import { createSelector } from 'reselect';

export const FETCH_MAP_BASE_LAYERS_REQUEST = 'FETCH_MAP_BASE_LAYERS_REQUEST';
export const FETCH_MAP_BASE_LAYERS_SUCCESS = 'FETCH_MAP_BASE_LAYERS_SUCCESS';
export const FETCH_MAP_BASE_LAYERS_FAILURE = 'FETCH_MAP_BASE_LAYERS_FAILURE';

const initialState = {
  items: [],
  isLoading: false,
  error: null
};

const getUrlTemplateOfActiveLayer = (layers, value) => {
  const activeLayer = layers.find((layer) => layer.value === value);
  return activeLayer ? activeLayer.urlTemplate : '';
};

const getAllBaseLayers = (state) => state.mapLayers.baseLayers.items;

export const getBaseLayers = createSelector([getAllBaseLayers],
  (baseLayers) => baseLayers.reduce((result, item) => ({
    ...result,
    [item.category]: result[item.category] ? [...result[item.category], item] :
      [item]
  }), {}));

const getActiveBaseLayer = (state) => state.map.baseLayer;

const getBase = (state, layerOptions) => ({
  layers: state.mapLayers.baseLayers.items,
  layerOptions
});

export const getBaseLayer = createSelector([getBase, getActiveBaseLayer],
  (base, activeLayerId) => ({
    urlTemplate: getUrlTemplateOfActiveLayer(base.layers, activeLayerId),
    baseLayerOptions: base.layerOptions
  }));

export default function MapBaseLayersReducer(state = initialState, action) {
  switch (action.type) {
    case FETCH_MAP_BASE_LAYERS_REQUEST:
      return { ...state, isLoading: true, error: null };

    case FETCH_MAP_BASE_LAYERS_SUCCESS:
      return { ...state, isLoading: false, items: action.mapBaseLayers };

    case FETCH_MAP_BASE_LAYERS_FAILURE:
      return { ...state, isLoading: false, error: action.error };

    default:
      return state;
  }
}

export const fetchMapBaseLayers = () => ({ type: FETCH_MAP_BASE_LAYERS_REQUEST });

window.reducers = window.reducers || {};
window.reducers.MapBaseLayersReducer = MapBaseLayersReducer;

@@ -901,17 +901,16 @@
 eLayer;%0A
-%0A
 const ge
@@ -910,20 +910,28 @@
 onst get
-Base
+LayerOptions
  = (stat
@@ -933,17 +933,21 @@
 (state, 
-l
+baseL
 ayerOpti
@@ -958,20 +958,54 @@
  =%3E 
-(%7B%0A  layers:
+baseLayerOptions;%0Aconst getLayers = (state) =%3E
  sta
@@ -1037,27 +1037,8 @@
 tems
-,%0A  layerOptions%0A%7D)
 ;%0A%0Ae
@@ -1083,20 +1083,22 @@
 tor(%5Bget
-Base
+Layers
 , getAct
@@ -1109,16 +1109,33 @@
 aseLayer
+, getLayerOptions
 %5D,%0A  (ba
@@ -1136,16 +1136,22 @@
 %0A  (base
+Layers
 , active
@@ -1157,16 +1157,34 @@
 eLayerId
+, baseLayerOptions
 ) =%3E (%7B%0A
@@ -1228,26 +1228,25 @@
 veLayer(base
-.l
+L
 ayers, activ
@@ -1280,27 +1280,8 @@
 ions
-: base.layerOptions
 %0A  %7D

Refactor according to review