'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _reactAutosuggest = require('react-autosuggest');

var _reactAutosuggest2 = _interopRequireDefault(_reactAutosuggest);

var _Input = require('../Input');

var _Input2 = _interopRequireDefault(_Input);

var _Suggestion = require('./Suggestion');

var _Suggestion2 = _interopRequireDefault(_Suggestion);

var _SearchBoxSuggestionsList = require('./styles/SearchBoxSuggestionsList');

var _SearchBoxSuggestionsList2 = _interopRequireDefault(_SearchBoxSuggestionsList);

var _SearchBoxSuggestionsSectionTitle = require('./styles/SearchBoxSuggestionsSectionTitle');

var _SearchBoxSuggestionsSectionTitle2 = _interopRequireDefault(_SearchBoxSuggestionsSectionTitle);

var _SearchBoxSearchButton = require('./styles/SearchBoxSearchButton');

var _SearchBoxSearchButton2 = _interopRequireDefault(_SearchBoxSearchButton);

var _SearchBoxWrapper = require('./styles/SearchBoxWrapper');

var _SearchBoxWrapper2 = _interopRequireDefault(_SearchBoxWrapper);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var SearchBox = function (_React$Component) {
  (0, _inherits3.default)(SearchBox, _React$Component);

  function SearchBox() {
    var _ref,
        _this2 = this;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, SearchBox);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = SearchBox.__proto__ || (0, _getPrototypeOf2.default)(SearchBox)).call.apply(_ref, [this].concat(args))), _this), _this.renderInput = function (_ref2) {
      var ref = _ref2.ref,
          inputProps = (0, _objectWithoutProperties3.default)(_ref2, ['ref']);

      var self = _this;
      return _react2.default.createElement(_this2.props.Input, (0, _extends3.default)({}, inputProps, { inputRef: ref }));
    }, _this.renderSuggestion = function (suggestion, _ref3) {
      var query = _ref3.query,
          isHighlighted = _ref3.isHighlighted;
      return _react2.default.createElement(
        _this2.props.Suggestion,
        {
          query: query,
          isHighlighted: isHighlighted,
          matchSuggestionContent: _this.props.matchSuggestionContent
        },
        _this.props.renderSuggestionContent(suggestion, query)
      );
    }, _this.renderSuggestionsContainer = function (_ref4) {
      var containerProps = _ref4.containerProps,
          children = _ref4.children;
      return children ? _react2.default.createElement(
        _this2.props.SuggestionsList,
        containerProps,
        children
      ) : null;
    }, _this.renderSectionTitle = function (section) {
      return _react2.default.createElement(
        _this2.props.SuggestionsSectionTitle,
        null,
        _this.props.getSectionTitle(section)
      );
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(SearchBox, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          inputProps = _props.inputProps,
          onChange = _props.onChange,
          value = _props.value,
          showSubmitButton = _props.showSubmitButton,
          onSubmit = _props.onSubmit,
          shouldShowSubmitButton = _props.shouldShowSubmitButton,
          Wrapper = _props.Wrapper,
          SearchButton = _props.SearchButton;


      return _react2.default.createElement(
        Wrapper,
        null,
        _react2.default.createElement(_reactAutosuggest2.default, (0, _extends3.default)({}, this.props, {
          inputProps: (0, _extends3.default)({}, inputProps, {
            onChange: onChange,
            value: value
          }),
          renderInputComponent: this.renderInput,
          renderSuggestion: this.renderSuggestion,
          renderSuggestionsContainer: this.renderSuggestionsContainer,
          renderSectionTitle: this.renderSectionTitle
        })),
        /* Using the button as a glyph if submit is disabled */
        showSubmitButton ? _react2.default.createElement(SearchButton, {
          onClick: onSubmit,
          disabled: !shouldShowSubmitButton(value)
        }) : _react2.default.createElement(SearchButton, { disabled: true })
      );
    }
  }]);
  return SearchBox;
}(_react2.default.Component);

SearchBox.propTypes = {
  /**
   * An array of suggestions. Suggestions can be any shape you like.
   */
  suggestions: _propTypes2.default.array.isRequired,
  /**
   * Called when the component needs to refresh suggestions. ({ value, reson }) => {}
   */
  onSuggestionsFetchRequested: _propTypes2.default.func.isRequired,
  /**
   * Called when suggestions need to be reset.
   */
  onSuggestionsClearRequested: _propTypes2.default.func.isRequired,
  /**
   * Computes a value from a single suggestion item. (suggestion) => value
   */
  getSuggestionValue: _propTypes2.default.func.isRequired,
  /**
   * Turns a suggestion into a renderable item. Can be a string or a node.
   * (suggestion, filterValue) => node
   */
  renderSuggestionContent: _propTypes2.default.func,
  /**
   * Called when the value changes. (event, { newValue, method }) => {}
   */
  onChange: _propTypes2.default.func.isRequired,
  /**
   * The current value in the search field.
   */
  value: _propTypes2.default.any.isRequired,
  /**
   * Whether suggestions should always be shown
   */
  alwaysRenderSuggestions: _propTypes2.default.bool,
  /**
   * Computes a title from a section object. Has a default (section) => seciton.title
   */
  getSectionTitle: _propTypes2.default.func,
  /**
   * Computes the suggestions for a section. Has a default (section) => section.suggestions
   */
  getSectionSuggestions: _propTypes2.default.func,
  /**
   * Additional props to pass to the input element
   */
  inputProps: _propTypes2.default.object,
  /**
   * Called when the user selects something
   */
  onSuggestionSelected: _propTypes2.default.func,
  /**
   * Invoked with the current input value and the content of a suggestion
   * as parameters. Must return an array [startIndex, endIndex] where
   * the value matches the suggestion content, so that the suggestion
   * knows which part of the string to highlight.
   *
   * Example, matchSuggestionContent('rg', 'corgi') = [2,4]
   *
   * The default for this will match case-insensitive
   */
  matchSuggestionContent: _propTypes2.default.func,
  /**
   * Whether to render a button for submitting the search
   */
  showSubmitButton: _propTypes2.default.bool,
  /**
   * Called when the user activates the search button
   */
  onSubmit: _propTypes2.default.func,
  /**
   * Passed the current search value, calculates whether the submit button
   * should be enabled.
   */
  shouldShowSubmitButton: _propTypes2.default.func,
  /**
   * A component to render a wrapper around the box
   */
  Wrapper: _propTypes2.default.func,
  /**
   * A component to render the list of suggestions
   */
  SuggestionsList: _propTypes2.default.func,
  /**
   * A component to render a search button integrated into the field
   */
  SearchButton: _propTypes2.default.func,
  /**
   * A component to render a section title in the suggestions list
   */
  SuggestionsSectionTitle: _propTypes2.default.func,
  /**
   * A component to render a suggestion item
   */
  Suggestion: _propTypes2.default.func,
  /**
   * A component to render the input element (defaults Input)
   */
  Input: _propTypes2.default.func
};
SearchBox.defaultProps = {
  renderSuggestionContent: _lodash2.default.identity,
  alwaysRenderSuggestions: false,
  getSectionTitle: function getSectionTitle(sec) {
    return sec.title;
  },
  getSectionSuggestions: function getSectionSuggestions(sec) {
    return sec.suggestions;
  },
  inputProps: {},
  onSuggestionSelected: null,
  matchSuggestionContent: undefined,
  showSubmitButton: false,
  onSubmit: function onSubmit() {
    return null;
  },
  shouldShowSubmitButton: function shouldShowSubmitButton() {
    return false;
  },
  Wrapper: _SearchBoxWrapper2.default,
  SuggestionsList: _SearchBoxSuggestionsList2.default,
  SuggestionsSectionTitle: _SearchBoxSuggestionsSectionTitle2.default,
  SearchButton: _SearchBoxSearchButton2.default,
  Suggestion: _Suggestion2.default,
  Input: _Input2.default
};


SearchBox.Suggestion = _Suggestion2.default;
SearchBox.SuggestionsList = _SearchBoxSuggestionsList2.default;
SearchBox.SuggestionsSectionTitle = _SearchBoxSuggestionsSectionTitle2.default;
SearchBox.Button = _SearchBoxSearchButton2.default;

exports.default = SearchBox;