'use strict';

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

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 _reactAutosuggest = require('react-autosuggest');

var _reactAutosuggest2 = _interopRequireDefault(_reactAutosuggest);

var _TextField = require('@material-ui/core/TextField');

var _TextField2 = _interopRequireDefault(_TextField);

var _Paper = require('@material-ui/core/Paper');

var _Paper2 = _interopRequireDefault(_Paper);

var _MenuItem = require('@material-ui/core/MenuItem');

var _MenuItem2 = _interopRequireDefault(_MenuItem);

var _reactIsolatedScroll = require('react-isolated-scroll');

var _reactIsolatedScroll2 = _interopRequireDefault(_reactIsolatedScroll);

var _match = require('autosuggest-highlight/match');

var _match2 = _interopRequireDefault(_match);

var _parse = require('autosuggest-highlight/parse');

var _parse2 = _interopRequireDefault(_parse);

var _omit = require('lodash/omit');

var _omit2 = _interopRequireDefault(_omit);

var _isEqual = require('lodash/isEqual');

var _isEqual2 = _interopRequireDefault(_isEqual);

var _objectAssign = require('object-assign');

var _objectAssign2 = _interopRequireDefault(_objectAssign);

var _styles = require('@material-ui/core/styles');

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

var ITEM_HEIGHT = 48;

var styleSheet = function styleSheet(theme) {
  return {
    container: {
      flexGrow: 1,
      position: 'relative',
      fontSize: 14
    },
    suggestionsContainerOpen: {
      position: 'absolute',
      marginTop: theme.spacing.unit,
      marginBottom: theme.spacing.unit * 3,
      left: 0,
      right: 0,
      zIndex: 99
    },
    suggestionsContainerInner: {
      maxHeight: ITEM_HEIGHT * 6.5,
      overflowY: 'auto'
    },
    suggestion: {
      display: 'block',
      fontSize: 14
    },
    suggestionsList: {
      margin: 0,
      padding: 0,
      listStyleType: 'none'
    },
    textField: {
      width: '100%'
    },
    input: {
      fontSize: 'inherit'
    }
  };
};

function renderInput(props) {
  var classes = props.classes,
      home = props.home,
      value = props.value,
      inputRef = props.inputRef,
      ref = props.ref,
      style = props.style;

  var inputProps = (0, _objectAssign2.default)({}, (0, _omit2.default)(props, ['classes', 'home', 'value', 'inputRef', 'ref', 'style']), { className: classes.input });
  function callRef(el) {
    inputRef(el);
    ref(el);
  }
  return _react2.default.createElement(_TextField2.default, {
    style: style,
    autoFocus: home,
    className: classes.textField,
    value: value,
    inputRef: callRef,
    InputProps: inputProps
  });
}

function renderSuggestion(suggestion, _ref) {
  var query = _ref.query,
      isHighlighted = _ref.isHighlighted;

  var matches = (0, _match2.default)(suggestion, query);
  var parts = (0, _parse2.default)(suggestion, matches);

  return _react2.default.createElement(
    _MenuItem2.default,
    { dense: true, selected: isHighlighted, component: 'div', style: { fontSize: 14 } },
    _react2.default.createElement(
      'div',
      null,
      parts.map(function (part, index) {
        return part.highlight ? _react2.default.createElement(
          'span',
          { key: index, style: { fontWeight: 500 } },
          part.text
        ) : _react2.default.createElement(
          'strong',
          { key: index, style: { fontWeight: 300 } },
          part.text
        );
      })
    )
  );
}

function getSuggestionValue(suggestion) {
  return suggestion;
}

function shouldRenderSuggestions() {
  return true;
}

function getSuggestions(value, suggestions) {
  var inputValue = value.trim().toLowerCase();
  var inputLength = inputValue.length;
  var count = 0;

  return inputLength === 0 ? suggestions : suggestions.filter(function (suggestion) {
    var keep = count < 5 && suggestion.toLowerCase().slice(0, inputLength) === inputValue;

    if (keep) {
      count += 1;
    }

    return keep;
  });
}

var Select = function (_Component) {
  (0, _inherits3.default)(Select, _Component);

  function Select() {
    var _ref2;

    var _temp, _this, _ret;

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

    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, (_ref2 = Select.__proto__ || (0, _getPrototypeOf2.default)(Select)).call.apply(_ref2, [this].concat(args))), _this), _this.state = {
      suggestions: _this.props.suggestions
    }, _this.renderSuggestionsContainer = function (_ref3) {
      var containerProps = _ref3.containerProps,
          children = _ref3.children;

      var callRef = function callRef(isolatedScroll) {
        if (isolatedScroll !== null) {
          containerProps.ref(isolatedScroll.component);
        }
      };
      return _react2.default.createElement(
        _Paper2.default,
        containerProps,
        _react2.default.createElement(
          _reactIsolatedScroll2.default,
          { ref: callRef, className: _this.props.classes.suggestionsContainerInner },
          children
        )
      );
    }, _this.getSuggestions = function (_ref4) {
      var value = _ref4.value;

      _this.setState({
        suggestions: _this.justFocussed ? _this.props.suggestions : getSuggestions(value, _this.props.suggestions)
      });
    }, _this.resetSuggestions = function () {
      _this.setState({
        suggestions: _this.props.suggestions
      });
    }, _this.selectInputText = function (e) {
      e.target.select();
      _this.justFocussed = true;
      // Naughty? But it works... *WARNING* relies on an internal method of react-autosuggest
      _this.autosuggest && _this.autosuggest.revealSuggestions();
    }, _this.handleChange = function (e, d) {
      _this.justFocussed = false;
      _this.props.onChange(e, d);
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(Select, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      if (!(0, _isEqual2.default)(nextProps.suggestions, this.props.suggestions)) {
        this.setState({ suggestions: nextProps.suggestions });
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          classes = _props.classes,
          className = _props.className,
          value = _props.value,
          onSuggestionSelected = _props.onSuggestionSelected,
          onKeyDown = _props.onKeyDown,
          style = _props.style;

      return _react2.default.createElement(_reactAutosuggest2.default, {
        ref: function ref(el) {
          return _this2.autosuggest = el;
        },
        theme: {
          container: classes.container + ' ' + className,
          suggestionsContainerOpen: classes.suggestionsContainerOpen,
          suggestionsList: classes.suggestionsList,
          suggestion: classes.suggestion
        },
        shouldRenderSuggestions: shouldRenderSuggestions,
        renderInputComponent: renderInput,
        suggestions: this.state.suggestions,
        onSuggestionsFetchRequested: this.getSuggestions,
        onSuggestionsClearRequested: this.resetSuggestions,
        onSuggestionSelected: onSuggestionSelected,
        renderSuggestionsContainer: this.renderSuggestionsContainer,
        getSuggestionValue: getSuggestionValue,
        renderSuggestion: renderSuggestion,
        inputProps: {
          inputRef: function inputRef(el) {
            return _this2.input = el;
          },
          style: style,
          classes: classes,
          value: value,
          onChange: this.handleChange,
          onFocus: this.selectInputText,
          onClick: this.selectInputText,
          onKeyDown: onKeyDown
        }
      });
    }
  }]);
  return Select;
}(_react.Component);

Select.propTypes = {
  classes: _propTypes2.default.object.isRequired,
  suggestions: _propTypes2.default.arrayOf(_propTypes2.default.string),
  value: _propTypes2.default.string,
  onChange: _propTypes2.default.func.isRequired,
  onSuggestionSelected: _propTypes2.default.func,
  onKeyDown: _propTypes2.default.func,
  style: _propTypes2.default.object
};

Select.defaultProps = {
  onSuggestionSelected: function onSuggestionSelected() {
    return null;
  },
  onKeyDown: function onKeyDown() {
    return null;
  },
  value: '',
  classes: {}
};

exports.default = (0, _styles.withStyles)(styleSheet)(Select);
//# sourceMappingURL=Select.js.map