import { createThunkAction } from 'utils/redux';
import upperFirst from 'lodash/upperFirst';
import { encodeStateForUrl, decodeUrlForState } from 'utils/stateToUrl';

import * as treeLossActions from 'pages/country/widget/widgets/widget-tree-loss/widget-tree-loss-actions';
import * as treeCoverActions from 'pages/country/widget/widgets/widget-tree-cover/widget-tree-cover-actions';
import * as treeLocatedActions from 'pages/country/widget/widgets/widget-tree-located/widget-tree-located-actions';
import * as treeGainActions from 'pages/country/widget/widgets/widget-tree-gain/widget-tree-gain-actions';
import * as FAOReforestationActions from 'pages/country/widget/widgets/widget-fao-reforestation/widget-fao-reforestation-actions';
import * as FAOCoverActions from 'pages/country/widget/widgets/widget-fao-cover/widget-fao-cover-actions';

const widgetActions = {
  ...treeLossActions.default,
  ...treeCoverActions.default,
  ...treeLocatedActions.default,
  ...treeGainActions.default,
  ...FAOReforestationActions.default,
  ...FAOCoverActions.default
};

export const setWidgetSettingsUrl = createThunkAction(
  'setWidgetSettingsUrl',
  ({ value, widget }) => (dispatch, state) => {
    const { location } = state();
    let params = value;
    if (location.query && location.query[widget]) {
      params = {
        ...decodeUrlForState(location.query[widget]),
        ...value
      };
    }
    dispatch({
      type: 'location/COUNTRY',
      payload: location.payload,
      query: {
        ...location.query,
        [widget]: encodeStateForUrl(params)
      }
    });
  }
);

export const setWidgetSettingsStore = createThunkAction(
  'setWidgetSettingsStore',
  query => dispatch => {
    Object.keys(query).forEach(widgetKey => {
      const actionFunc = widgetActions[`set${upperFirst(widgetKey)}Settings`];
      if (actionFunc) {
        dispatch(actionFunc(decodeUrlForState(query[widgetKey])));
      }
    });
  }
);

export default {
  setWidgetSettingsUrl,
  setWidgetSettingsStore
};

@@ -86,16 +86,86 @@
 First';%0A
+import isEqual from 'lodash/isEqual';%0Aimport pick from 'lodash/pick';%0A
 import %7B
@@ -1656,24 +1656,148 @@
 %7D);%0A  %7D%0A);%0A%0A
+function isObjectContained(contained, container) %7B%0A  return isEqual(pick(container, Object.keys(contained)), contained);%0A%7D%0A%0A
 export const
@@ -1880,24 +1880,25 @@
 uery =%3E 
+(
 dispatch
  =%3E %7B%0A  
@@ -1889,16 +1889,27 @@
 dispatch
+, getState)
  =%3E %7B%0A  
@@ -1948,24 +1948,382 @@
 getKey =%3E %7B%0A
+      const widgetConfig = decodeUrlForState(query%5BwidgetKey%5D);%0A      const %7B settings %7D = getState()%5B%60widget$%7BupperFirst(widgetKey)%7D%60%5D;%0A      // Check if the state needs and update checking the values of the new config%0A      // with the existing in the url to avoid dispatch actions without changes%0A      if (!isObjectContained(widgetConfig, settings)) %7B%0A  
       const 
@@ -2391,24 +2391,26 @@
 gs%60%5D;%0A      
+  
 if (actionFu
@@ -2411,24 +2411,26 @@
 tionFunc) %7B%0A
+  
         disp
@@ -2449,46 +2449,33 @@
 unc(
-decodeUrlForState(query%5BwidgetKey%5D)));
+widgetConfig));%0A        %7D
 %0A   

Check changes before actions dispatch