import _keys from "lodash/fp/keys";
import _isEmpty from "lodash/fp/isEmpty";
import _isNil from "lodash/fp/isNil";
import _noop from "lodash/fp/noop";
import React, { useCallback, useMemo } from 'react';
import Autosuggest from 'react-autosuggest';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { NovaSolidStatusClose as ErrorIcon, NovaCompositionCoorpacademyInformationIcon as InfoIcon } from '@coorpacademy/nova-icons';
import getClassState from '../../util/get-class-state';
import style from './style.css';
const THEME_STYLE = {
  coorpmanager: style.coorpmanager,
  default: style.defaultStyle
};
const renderSuggestion = suggestion => /*#__PURE__*/React.createElement("span", null, suggestion.name);
const Autocomplete = props => {
  const {
    placeholder = '',
    value,
    description,
    required,
    modified = false,
    error = false,
    errorMessage,
    suggestions = [],
    onInput = _noop,
    onFetch = _noop,
    onClear = _noop,
    onBlur = _noop,
    onSuggestionSelected = _noop,
    title: propsTitle,
    theme = 'default'
  } = props;
  const mainClass = THEME_STYLE[theme];
  const title = useMemo(() => `${propsTitle}${required ? '*' : ''}`, [propsTitle, required]);
  const className = useMemo(() => getClassState(style.defaultStyle, style.modified, style.error, modified, error), [modified, error]);
  const handleInput = useCallback(e => {
    onInput(e);
  }, [onInput]);
  const handleBlur = useCallback((e, selectedSuggestion) => onBlur(e, selectedSuggestion), [onBlur]);
  const handleSuggestionsFetchRequested = useCallback(e => onFetch(e), [onFetch]);
  const handleSuggestionsClearRequested = useCallback(e => onClear(e), [onClear]);
  const handleSuggestionsSelected = useCallback((e, data) => onSuggestionSelected(data), [onSuggestionSelected]);
  const inputProps = {
    placeholder,
    value,
    onChange: _noop,
    onBlur: handleBlur,
    onInput: handleInput,
    'data-testid': 'autocomplete-input'
  };
  const descriptionView = description && theme !== 'coorpmanager' ? /*#__PURE__*/React.createElement("div", {
    className: style.description
  }, description) : null;
  const toolTipView = description && theme === 'coorpmanager' ? /*#__PURE__*/React.createElement("div", {
    className: style.infoIconWrapper
  }, /*#__PURE__*/React.createElement(InfoIcon, {
    className: style.infoIcon
  }), /*#__PURE__*/React.createElement("div", {
    className: style.descriptionLabel
  }, description)) : null;
  const errorIconView = theme === 'coorpmanager' && error ? /*#__PURE__*/React.createElement(ErrorIcon, {
    className: style.leftIcon
  }) : null;
  const errorText = theme === 'coorpmanager' && error ? /*#__PURE__*/React.createElement("div", {
    className: style.errorText
    // eslint-disable-next-line react/no-danger
    ,
    dangerouslySetInnerHTML: {
      __html: errorMessage
    }
  }) : null;
  return /*#__PURE__*/React.createElement("div", {
    className: classnames(mainClass, className, _isNil(propsTitle) && style.isNoTitle)
  }, /*#__PURE__*/React.createElement("label", null, /*#__PURE__*/React.createElement("span", {
    className: classnames(style.title, _isEmpty(value) && style.noValue)
  }, title, toolTipView), /*#__PURE__*/React.createElement("div", {
    className: style.inputContainer
  }, /*#__PURE__*/React.createElement(Autosuggest, {
    theme: {
      container: style.container,
      input: style.input,
      suggestionsContainer: style.suggestionsContainer,
      suggestionsContainerOpen: style.suggestionsContainerOpen,
      suggestionsList: style.suggestionsList,
      suggestion: style.suggestion,
      suggestionHighlighted: style.suggestionHighlighted
    },
    suggestions: suggestions,
    onSuggestionsFetchRequested: handleSuggestionsFetchRequested,
    onSuggestionsClearRequested: handleSuggestionsClearRequested,
    getSuggestionValue: _noop,
    renderSuggestion: renderSuggestion,
    inputProps: inputProps,
    focusInputOnSuggestionClick: false,
    onSuggestionSelected: handleSuggestionsSelected
  }), errorIconView, errorText)), descriptionView);
};
Autocomplete.propTypes = process.env.NODE_ENV !== "production" ? {
  title: PropTypes.string,
  placeholder: PropTypes.string,
  value: PropTypes.string,
  description: PropTypes.string,
  required: PropTypes.bool,
  modified: PropTypes.bool,
  error: PropTypes.bool,
  errorMessage: PropTypes.string,
  suggestions: PropTypes.arrayOf(PropTypes.shape({
    name: PropTypes.string,
    value: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
  })),
  theme: PropTypes.oneOf(_keys(THEME_STYLE)),
  onInput: PropTypes.func,
  onFetch: PropTypes.func,
  onClear: PropTypes.func,
  onBlur: PropTypes.func,
  onSuggestionSelected: PropTypes.func
} : {};
export default Autocomplete;
//# sourceMappingURL=index.js.map