/* eslint camelcase: 0 */
import React from 'react';
import controls from './controls';
import visTypes, { sectionsToRender } from './visTypes';

export function getFormDataFromControls(controlsState) {
  const formData = {};
  Object.keys(controlsState).forEach((controlName) => {
    formData[controlName] = controlsState[controlName].value;
  });
  return formData;
}

export function getControlNames(vizType, datasourceType) {
  const controlNames = [];
  sectionsToRender(vizType, datasourceType).forEach(
    section => section.controlSetRows.forEach(
      fsr => fsr.forEach(
        f => controlNames.push(f))));
  return controlNames;
}

function handleDeprecatedControls(formData) {
  // Reacffectation / handling of deprecated controls
  /* eslint-disable no-param-reassign */

  // y_axis_zero was a boolean forcing 0 to be part of the Y Axis
  if (formData.y_axis_zero) {
    formData.y_axis_bounds = [0, null];
  }
}

export function getControlsState(state, form_data) {
  /*
  * Gets a new controls object to put in the state. The controls object
  * is similar to the configuration control with only the controls
  * related to the current viz_type, materializes mapStateToProps functions,
  * adds value keys coming from form_data passed here. This can't be an action creator
  * just yet because it's used in both the explore and dashboard views.
  * */

  // Getting a list of active control names for the current viz
  const formData = Object.assign({}, form_data);
  const vizType = formData.viz_type || 'table';

  handleDeprecatedControls(formData);

  const controlNames = getControlNames(vizType, state.datasource.type);

  const viz = visTypes[vizType];
  const controlOverrides = viz.controlOverrides || {};
  const controlsState = {};
  controlNames.forEach((k) => {
    if (React.isValidElement(k)) {
      // no state
      return;
    }
    const control = Object.assign({}, controls[k], controlOverrides[k]);
    if (control.mapStateToProps) {
      Object.assign(control, control.mapStateToProps(state, control));
      delete control.mapStateToProps;
    }

    formData[k] = (control.multi && formData[k] && !Array.isArray(formData[k])) ? [formData[k]]
      : formData[k];

    // If the value is not valid anymore based on choices, clear it
    if (control.type === 'SelectControl' && control.choices && k !== 'datasource' && formData[k]) {
      const choiceValues = control.choices.map(c => c[0]);
      if (control.multi && formData[k].length > 0 && choiceValues.indexOf(formData[k][0]) < 0) {
        delete formData[k];
      } else if (!control.multi && !control.freeForm && choiceValues.indexOf(formData[k]) < 0) {
        delete formData[k];
      }
    }

    if (typeof control.default === 'function') {
      control.default = control.default(control);
    }
    control.validationErrors = [];
    control.value = control.default;
    // formData[k]'s type should match control value type
    if (formData[k] !== undefined &&
      (Array.isArray(formData[k]) && control.multi || !control.multi)
    ) {
      control.value = formData[k];
    }
    controlsState[k] = control;
  });
  if (viz.onInit) {
    return viz.onInit(controlsState);
  }
  return controlsState;
}

export function applyDefaultFormData(form_data) {
  const datasourceType = form_data.datasource.split('__')[1];
  const vizType = form_data.viz_type || 'table';
  const viz = visTypes[vizType];
  const controlNames = getControlNames(vizType, datasourceType);
  const controlOverrides = viz.controlOverrides || {};
  const formData = {};
  controlNames.forEach((k) => {
    const control = Object.assign({}, controls[k]);
    if (controlOverrides[k]) {
      Object.assign(control, controlOverrides[k]);
    }
    if (form_data[k] === undefined) {
      if (typeof control.default === 'function') {
        formData[k] = control.default(controls[k]);
      } else {
        formData[k] = control.default;
      }
    } else {
      formData[k] = form_data[k];
    }
  });
  // fill in additional params stored in form_data but not used by control
  Object.keys(form_data)
    .forEach((key) => {
      if (formData[key] === undefined) {
        formData[key] = form_data[key];
      }
    });
  return formData;
}

export const autoQueryControls = [
  'datasource',
  'viz_type',
];

const defaultControls = Object.assign({}, controls);
Object.keys(controls).forEach((f) => {
  defaultControls[f].value = controls[f].default;
});

const defaultState = {
  controls: defaultControls,
  form_data: getFormDataFromControls(defaultControls),
};

export { defaultControls, defaultState };

@@ -2272,32 +2272,39 @@
 lear it%0A    if (
+%0A      
 control.type ===
@@ -2322,16 +2322,49 @@
 trol' &&
+%0A      !control.freeForm &&%0A     
  control
@@ -2374,16 +2374,22 @@
 oices &&
+%0A     
  k !== '
@@ -2402,16 +2402,22 @@
 urce' &&
+%0A     
  formDat
@@ -2420,16 +2420,21 @@
 mData%5Bk%5D
+%0A    
 ) %7B%0A    
@@ -2541,82 +2541,89 @@
  %3E 0
- && choiceValues.indexOf(formData%5Bk%5D%5B0%5D) %3C 0) %7B%0A        delete formData%5Bk%5D
+) %7B%0A        formData%5Bk%5D = formData%5Bk%5D.filter(el =%3E choiceValues.indexOf(el) %3E -1)
 ;%0A  
@@ -2654,37 +2654,16 @@
 multi &&
- !control.freeForm &&
  choiceV

Fix time shift (#5529)