/* eslint-disable @typescript-eslint/no-empty-function */
import React from 'react';
import PropTypes from 'prop-types';
import Autosuggest from 'react-autosuggest';
import debounce from 'lodash.debounce';

class AUAutoSuggestComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: '',
      suggestions: [],
      noSuggestions: false,
    };

    this.onChange = this.onChange.bind(this);
    this.onSuggestionsFetchRequested = debounce(this.onSuggestionsFetchRequested.bind(this), 400);
    this.onSuggestionsClearRequested = this.onSuggestionsClearRequested.bind(this);
    this.onSuggestionSelected = this.onSuggestionSelected.bind(this);
  }

  onChange(_, { newValue }) {
    const { setQuery } = this.props;
    this.setState({
      value: newValue,
    }, () => {
      setQuery(newValue);
    });
  }

  onSuggestionsFetchRequested({ value }) {
    const { getSuggestions, collection } = this.props;
    getSuggestions(value, collection, (suggestions) => {
      this.setState({
        suggestions,
        noSuggestions: suggestions.length === 0 && value.trim() !== '' && value.trim().length > 1,
      });
    });
  }

  onSuggestionsClearRequested() {
    this.setState({
      suggestions: [],
    });
  }

  onSuggestionSelected(event, { suggestion }) {
    const { setResults, clearInput } = this.props;
    setResults([suggestion]);
    if (clearInput) {
      this.setState({
        value: '',
      });
    }
  }

  render() {
    const { value, suggestions, noSuggestions } = this.state;
    const {
      placeholder,
      getSuggestionValue,
      renderSuggestion,
      id,
      theme,
      type,
      initialValue,
      disabled,
      noResultsText,
    } = this.props;
    const inputProps = {
      placeholder,
      value: initialValue || value,
      id,
      type,
      disabled,
      'aria-label': placeholder,
      onChange: this.onChange,
    };

    return (
      <>
        <Autosuggest
          suggestions={suggestions}
          onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
          onSuggestionsClearRequested={this.onSuggestionsClearRequested}
          onSuggestionSelected={this.onSuggestionSelected}
          getSuggestionValue={getSuggestionValue}
          renderSuggestion={renderSuggestion}
          inputProps={inputProps}
          theme={theme}
        />
        {
          (noSuggestions && noResultsText) && (
            <p className="no-results">
              <em>{noResultsText}</em>
            </p>
          )
        }
      </>
    );
  }
}

AUAutoSuggestComponent.displayName = 'AUAutoSuggestComponent';

AUAutoSuggestComponent.defaultProps = {
  id: 'autosuggest1',
  theme: {
    container: 'autosuggest',
    containerOpen: 'autosuggest--open',
    input: 'autosuggest__input',
    inputOpen: 'autosuggest__input--open',
    inputFocused: 'autosuggest__input--focused',
    suggestionsContainer: 'autosuggest__suggestions',
    suggestionsContainerOpen: 'autosuggest__suggestions--open',
    suggestionsList: 'autosuggest__suggestions__list',
    suggestion: 'autosuggest__suggestions__list__item',
    suggestionFirst: 'autosuggest__suggestions__list--first',
    suggestionHighlighted: 'autosuggest__suggestions__list--highlighted',
    sectionContainer: 'autosuggest__section',
    sectionContainerFirst: 'autosuggest__section--first',
    sectionTitle: 'autosuggest__section__title',
  },
  clearInput: false,
  type: 'text',
  initialValue: '',
  disabled: false,
  setQuery: () => { },
  noResultsText: '',
};

AUAutoSuggestComponent.propTypes = {
  setQuery: PropTypes.func,
  getSuggestions: PropTypes.func.isRequired,
  getSuggestionValue: PropTypes.func.isRequired,
  renderSuggestion: PropTypes.func.isRequired,
  setResults: PropTypes.func.isRequired,
  collection: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
  placeholder: PropTypes.string.isRequired,
  id: PropTypes.string,
  theme: PropTypes.shape({}),
  clearInput: PropTypes.bool,
  type: PropTypes.string,
  initialValue: PropTypes.string,
  disabled: PropTypes.bool,
  noResultsText: PropTypes.string,
};
export default AUAutoSuggestComponent;
