export function add(obj, stateVal) {
  stateVal.list.push(obj);
  stateVal.filteredOptions.push(obj.value);
  return stateVal;
}
export function get_data(stateVal) {
  return stateVal.list;
}
export function get_filtered_options_data(stateVal) {
  return stateVal && stateVal.filteredOptions;
}
export function get_selected_suggestion(stateVal) {
  return stateVal.selectedSuggestions;
} // filter_options_check name -->
// renderOptionsBasedOncriteria

export function filter_options_update(stateVal) {
  let {
    filteredOptions,
    selectedSuggestions,
    list
  } = stateVal; //stateVal.filteredOptions = [];

  list.map(obj => {
    if (selectedSuggestions.indexOf(obj.value) == -1) {
      filteredOptions.push(obj.value);
    }
  }); //stateVal.filteredOptions=filteredOptions

  return filteredOptions;
}
export function filter_options_check(value, options) {
  return options.indexOf(value) != -1;
}
export function filter_options(val, options) {
  return options.filter(value => {
    return value != val;
  });
}
export function filter_for_search(str, stateVal) {
  filter_options_update(stateVal);
  let searchStr = str.toLowerCase();
  let data = [];
  stateVal.list.map(obj => {
    let text = obj.text && obj.text.toLowerCase();

    if (stateVal.filteredOptions.indexOf(obj.value) != -1 && text.indexOf(searchStr) != -1) {
      data.push(obj.value);
    }
  });
  stateVal.filteredOptions = data;
  return stateVal;
}
export function on_select(value, stateVal) {
  let {
    selectedSuggestions
  } = stateVal;
  selectedSuggestions.push(value);
  stateVal.selectedSuggestions = selectedSuggestions;
  stateVal.filteredOptions = filter_options(value, stateVal.filteredOptions);
  return stateVal;
}
export function on_remove(value, stateVal) {
  let {
    selectedSuggestions,
    filteredOptions
  } = stateVal;
  filteredOptions.push(value);
  selectedSuggestions = selectedSuggestions.filter(val => {
    return value != val;
  });
  stateVal.selectedSuggestions = selectedSuggestions;
  stateVal.filteredOptions = filteredOptions;
  return stateVal;
}
export function on_select_all(stateVal) {
  let {
    list
  } = stateVal;
  stateVal.filteredOptions = [];
  list.map(obj => {
    if (!filter_options_check(obj.value, stateVal.selectedSuggestions)) {
      stateVal.selectedSuggestions.push(obj.value);
    }
  });
  return stateVal;
}
export function on_remove_all(stateVal) {
  let {
    list,
    filteredOptions
  } = stateVal;
  list.map(obj => {
    if (!filter_options_check(obj.value, stateVal.filteredOptions)) {
      filteredOptions.push(obj.value);
    }
  });
  stateVal.selectedSuggestions = [];
  stateVal.filteredOptions = filteredOptions;
  return stateVal;
}