function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

import React from 'react';
import PropTypes from 'prop-types';
import { noop, get, isEmpty } from 'lodash';
import SearchIcon from '@material-ui/icons/SearchRounded';
import ClearIcon from '@material-ui/icons/ClearRounded';
import { getCSSVariableAsString } from '../../utils/CSSVariables';
import Text from './Text';

const Search = props => {
  const color = getCSSVariableAsString('--input-text');
  const hasValue = !isEmpty(get(props, ['input', 'value'], ''));
  const onChange = get(props, ['input', 'onChange'], noop);

  const handleClear = () => {
    onChange('');
  };

  const adornmentLeft = color && /*#__PURE__*/React.createElement(SearchIcon, {
    style: {
      color
    }
  });
  const adornmentRight = color && hasValue && /*#__PURE__*/React.createElement(ClearIcon, {
    style: {
      color,
      cursor: 'pointer'
    },
    onClick: handleClear
  });
  return /*#__PURE__*/React.createElement(Text, _extends({
    adornmentLeft: adornmentLeft,
    adornmentRight: adornmentRight
  }, props, {
    // eslint-disable-line react/jsx-props-no-spreading
    type: "search"
  }));
};

Search.defaultProps = {
  input: {
    onChange: noop
  }
};
Search.propTypes = {
  input: PropTypes.object
};
export default Search;