import React, { useState } from "react";
import { on_select, on_remove, get_selected_suggestion, filter_for_search, on_remove_all, on_select_all, add, filter_options_check } from "../utils/MultiSelect.manage";
import useCommonReducer from "../../../libraries/reducerHandler";
import constants from "../actions/MultiSelect.actionConstants";
import multiSelectStateReducers from "../actions/MultiSelect.action";
export default function useMultiSelectWithSearch(props, reducer) {
  let {
    state = {},
    dispatch
  } = useCommonReducer(multiSelectStateReducers, reducer, {
    searchString: "",
    selectedSuggestions: [],
    filteredOptions: [],
    list: []
  });

  function onHandleSelect(value) {
    on_select(value, state);
    let selected_suggestions = state.selectedSuggestions;
    let newState = { ...state,
      selectedSuggestions: selected_suggestions
    };
    dispatch({
      type: constants.ON_MULTISELECT_SELECT,
      data: newState
    });
  }

  function addOptions(obj) {
    add(obj, state);
    dispatch({
      type: constants.ON_MULTISELECT_ADD,
      data: state
    });
  }

  function get_data() {
    return state.list;
  }

  function get_filtered_options_data() {
    return state.filteredOptions;
  }

  function get_selected_suggestion() {
    return state.selectedSuggestions;
  }

  function filterOptionsCheck(value) {
    return filter_options_check(value, state.filteredOptions);
  }

  function updateSelectedValues(value) {
    let {
      selectedSuggestions,
      filteredOptions,
      list
    } = state;
    dispatch({
      type: constants.ON_MULTISELECT_UPDATE_SELECTED_VALUES,
      data: value
    });
  }

  function onHandleRemove(value) {
    on_remove(value, state);
    let selected_suggestions = state.selectedSuggestions;
    let newState = { ...state,
      selectedSuggestions: selected_suggestions
    };
    dispatch({
      type: constants.ON_MULTISELECT_REMOVE,
      data: newState
    });
  }

  function shotcutKeys(e, value) {
    const {
      keyCode,
      ctrlKey,
      metaKey,
      shiftKey
    } = e;

    if (keyCode === 8 || keyCode === 46) {
      let selected_suggestions = state.selectedSuggestions;
      let index = selected_suggestions.findIndex((ele, i) => {
        if (ele == value) {
          return i;
        }
      });
      let val = index > -1 ? selected_suggestions[index] : selected_suggestions.pop();
      on_remove(val, state);
      selected_suggestions = state.selectedSuggestions;
      dispatch({
        type: constants.ON_MULTISELECT_REMOVE,
        data: state
      });
    }
  }

  function onSearch(str) {
    let fil = filter_for_search(str, state);
    dispatch({
      type: constants.ON_MULTISELECT_SEARCH,
      data: { ...fil,
        searchString: str
      }
    });
  }

  function onSelectAll() {
    let data = on_select_all(state);
    updateSelectedValues(data, state);
    dispatch({
      type: constants.ON_MULTISELECT_SELECT_ALL,
      data: data
    });
  }

  function onRemoveAll() {
    on_remove_all(state);
    updateSelectedValues([], state);
    dispatch({
      type: constants.ON_MULTISELECT_REMOVE_ALL,
      data: state
    });
  }

  return {
    filteredOptions: state.filteredOptions,
    updateSelectedValues,
    shotcutKeys,
    addOptions,
    selectedValues: state.selectedSuggestions,
    onHandleSelect,
    onHandleRemove,
    onSearch,
    searchString: state.searchString,
    onSelectAll,
    onRemoveAll,
    getFilteredOptionsData: get_filtered_options_data,
    filterOptionsCheck
  };
}