"use strict";

exports.__esModule = true;
exports.default = void 0;
var _keys2 = _interopRequireDefault(require("lodash/fp/keys"));
var _isEmpty2 = _interopRequireDefault(require("lodash/fp/isEmpty"));
var _isNil2 = _interopRequireDefault(require("lodash/fp/isNil"));
var _noop2 = _interopRequireDefault(require("lodash/fp/noop"));
var _react = _interopRequireWildcard(require("react"));
var _reactAutosuggest = _interopRequireDefault(require("react-autosuggest"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _getClassState = _interopRequireDefault(require("../../util/get-class-state"));
var _style = _interopRequireDefault(require("./style.css"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const THEME_STYLE = {
  coorpmanager: _style.default.coorpmanager,
  default: _style.default.defaultStyle
};
const renderSuggestion = suggestion => /*#__PURE__*/_react.default.createElement("span", null, suggestion.name);
const Autocomplete = props => {
  const {
    placeholder = '',
    value,
    description,
    required,
    modified = false,
    error = false,
    errorMessage,
    suggestions = [],
    onInput = _noop2.default,
    onFetch = _noop2.default,
    onClear = _noop2.default,
    onBlur = _noop2.default,
    onSuggestionSelected = _noop2.default,
    title: propsTitle,
    theme = 'default'
  } = props;
  const mainClass = THEME_STYLE[theme];
  const title = (0, _react.useMemo)(() => `${propsTitle}${required ? '*' : ''}`, [propsTitle, required]);
  const className = (0, _react.useMemo)(() => (0, _getClassState.default)(_style.default.defaultStyle, _style.default.modified, _style.default.error, modified, error), [modified, error]);
  const handleInput = (0, _react.useCallback)(e => {
    onInput(e);
  }, [onInput]);
  const handleBlur = (0, _react.useCallback)((e, selectedSuggestion) => onBlur(e, selectedSuggestion), [onBlur]);
  const handleSuggestionsFetchRequested = (0, _react.useCallback)(e => onFetch(e), [onFetch]);
  const handleSuggestionsClearRequested = (0, _react.useCallback)(e => onClear(e), [onClear]);
  const handleSuggestionsSelected = (0, _react.useCallback)((e, data) => onSuggestionSelected(data), [onSuggestionSelected]);
  const inputProps = {
    placeholder,
    value,
    onChange: _noop2.default,
    onBlur: handleBlur,
    onInput: handleInput,
    'data-testid': 'autocomplete-input'
  };
  const descriptionView = description && theme !== 'coorpmanager' ? /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.description
  }, description) : null;
  const toolTipView = description && theme === 'coorpmanager' ? /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.infoIconWrapper
  }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyInformationIcon, {
    className: _style.default.infoIcon
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.descriptionLabel
  }, description)) : null;
  const errorIconView = theme === 'coorpmanager' && error ? /*#__PURE__*/_react.default.createElement(_novaIcons.NovaSolidStatusClose, {
    className: _style.default.leftIcon
  }) : null;
  const errorText = theme === 'coorpmanager' && error ? /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.errorText
    // eslint-disable-next-line react/no-danger
    ,
    dangerouslySetInnerHTML: {
      __html: errorMessage
    }
  }) : null;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(mainClass, className, (0, _isNil2.default)(propsTitle) && _style.default.isNoTitle)
  }, /*#__PURE__*/_react.default.createElement("label", null, /*#__PURE__*/_react.default.createElement("span", {
    className: (0, _classnames.default)(_style.default.title, (0, _isEmpty2.default)(value) && _style.default.noValue)
  }, title, toolTipView), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.inputContainer
  }, /*#__PURE__*/_react.default.createElement(_reactAutosuggest.default, {
    theme: {
      container: _style.default.container,
      input: _style.default.input,
      suggestionsContainer: _style.default.suggestionsContainer,
      suggestionsContainerOpen: _style.default.suggestionsContainerOpen,
      suggestionsList: _style.default.suggestionsList,
      suggestion: _style.default.suggestion,
      suggestionHighlighted: _style.default.suggestionHighlighted
    },
    suggestions: suggestions,
    onSuggestionsFetchRequested: handleSuggestionsFetchRequested,
    onSuggestionsClearRequested: handleSuggestionsClearRequested,
    getSuggestionValue: _noop2.default,
    renderSuggestion: renderSuggestion,
    inputProps: inputProps,
    focusInputOnSuggestionClick: false,
    onSuggestionSelected: handleSuggestionsSelected
  }), errorIconView, errorText)), descriptionView);
};
Autocomplete.propTypes = process.env.NODE_ENV !== "production" ? {
  title: _propTypes.default.string,
  placeholder: _propTypes.default.string,
  value: _propTypes.default.string,
  description: _propTypes.default.string,
  required: _propTypes.default.bool,
  modified: _propTypes.default.bool,
  error: _propTypes.default.bool,
  errorMessage: _propTypes.default.string,
  suggestions: _propTypes.default.arrayOf(_propTypes.default.shape({
    name: _propTypes.default.string,
    value: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number])
  })),
  theme: _propTypes.default.oneOf((0, _keys2.default)(THEME_STYLE)),
  onInput: _propTypes.default.func,
  onFetch: _propTypes.default.func,
  onClear: _propTypes.default.func,
  onBlur: _propTypes.default.func,
  onSuggestionSelected: _propTypes.default.func
} : {};
var _default = exports.default = Autocomplete;
//# sourceMappingURL=index.js.map