'use strict';

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

var _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; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

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 }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var SearchBox = function (_React$Component) {
  _inherits(SearchBox, _React$Component);

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

    var _temp, _this, _ret;

    _classCallCheck(this, SearchBox);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = SearchBox.__proto__ || Object.getPrototypeOf(SearchBox)).call.apply(_ref, [this].concat(args))), _this), _this.renderInput = function (_ref2) {
      var ref = _ref2.ref,
          inputProps = _objectWithoutProperties(_ref2, ['ref']);

      var self = _this;
      return _react2.default.createElement(_this2.props.Input, _extends({}, 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), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(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, _extends({}, this.props, {
          inputProps: _extends({}, 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;