'use strict';

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

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

var _extends3 = _interopRequireDefault(_extends2);

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 _reactSelect = require('react-select');

var _reactSelect2 = _interopRequireDefault(_reactSelect);

var _arrow = require('./arrow.png');

var _arrow2 = _interopRequireDefault(_arrow);

var _theme = require('../../theme');

var _theme2 = _interopRequireDefault(_theme);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _selectItemPrimaryValue = require('../../extensions/selectItemPrimaryValue');

var _selectItemPrimaryValue2 = _interopRequireDefault(_selectItemPrimaryValue);

var _Loader = require('../Loader');

var _Loader2 = _interopRequireDefault(_Loader);

var _SelectWrapper = require('./styles/SelectWrapper');

var _SelectWrapper2 = _interopRequireDefault(_SelectWrapper);

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

var Select = function (_React$PureComponent) {
  (0, _inherits3.default)(Select, _React$PureComponent);

  function Select() {
    var _ref;

    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, (_ref = Select.__proto__ || (0, _getPrototypeOf2.default)(Select)).call.apply(_ref, [this].concat(args))), _this), _this.convertOptions = function () {
      var _this$props = _this.props,
          options = _this$props.options,
          renderOption = _this$props.renderOption,
          getOptionValue = _this$props.getOptionValue,
          selectOptionValue = _this$props.selectOptionValue;


      return options.map(function (opt) {
        return {
          label: renderOption(opt),
          value: (getOptionValue || selectOptionValue)(opt)
        };
      });
    }, _this.handleChange = function (newValue) {
      if (!newValue) {
        _this.props.onChange(newValue);
      } else if (_this.props.multi) {
        _this.props.onChange(newValue.map(function (item) {
          return item.value;
        }));
      } else {
        _this.props.onChange(newValue.value);
      }
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  /**
   * Takes the options provided and converts them into
   * react-select options using user-provided conversion
   * functions to generate a displayed label and a backing value
   *
   * @memberof Select
   */


  /**
   * Intercepts the change event and calls the onChange prop
   * with the value alone.
   *
   * @memberof Select
   */


  (0, _createClass3.default)(Select, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          required = _props.required,
          placeholder = _props.placeholder,
          noneText = _props.noneText,
          allowNone = _props.allowNone,
          isLoading = _props.isLoading,
          loading = _props.loading,
          invalid = _props.invalid,
          Wrapper = _props.Wrapper;

      var combinedLoading = loading || isLoading;
      var combinedPlaceholder = placeholder || noneText;

      return _react2.default.createElement(
        Wrapper,
        { invalid: invalid },
        _react2.default.createElement(_reactSelect2.default, (0, _extends3.default)({}, this.props, {
          options: this.convertOptions(),
          onChange: this.handleChange,
          clearable: !required || allowNone,
          placeholder: combinedLoading ? _react2.default.createElement(_Loader2.default, { size: '14px' }) : combinedPlaceholder,
          isLoading: combinedLoading
        }))
      );
    }
  }]);
  return Select;
}(_react2.default.PureComponent);

Select.propTypes = {
  /**
   * Whether to blur the component upon selection
   */
  autoBlur: _propTypes2.default.bool,
  /**
   * Whether to focus the component on mount
   */
  autoFocus: _propTypes2.default.bool,
  /**
   * Whether to close the menu when a value is selected
   */
  closeOnSelect: _propTypes2.default.bool,
  /**
   * Whether pressing backspace will clear a value.
   */
  deleteRemoves: _propTypes2.default.bool,
  /**
   * delimiter to use to join multiple values for the hidden field value
   */
  delimiter: _propTypes2.default.string,
  /**
   * method to filter a single option (option, filterString)
   */
  filterOption: _propTypes2.default.func,
  /**
   * boolean to enable default filtering or function to filter the options array ([options], filterString, [values])
   */
  filterOptions: _propTypes2.default.func,
  /**
   * whether to strip diacritics when filtering
   */
  ignoreAccents: _propTypes2.default.bool,
  /**
   * whether to perform case-insensitive filtering
   */
  ignoreCase: _propTypes2.default.bool,
  /**
   * custom attributes for the Input
   */
  inputProps: _propTypes2.default.object,
  /**
   * controls whether the component renders in a loading state
   */
  isLoading: _propTypes2.default.bool,
  /**
   * Enables multi-select mode
   */
  multi: _propTypes2.default.bool,
  /**
   * The name to apply to the input field
   */
  name: _propTypes2.default.string,
  /**
   * Handler for blur events on the input
   */
  onBlur: _propTypes2.default.func,
  /**
   * Handler for closing the options list
   */
  onClose: _propTypes2.default.func,
  /**
   * Handler for focus events on the input
   */
  onFocus: _propTypes2.default.func,
  /**
   * Handles raw input change events
   */
  onInputChange: _propTypes2.default.func,
  /**
   * Handles raw input key down events
   */
  onInputKeyDown: _propTypes2.default.func,
  /**
   * Handler for opening the options list
   */
  onOpen: _propTypes2.default.func,
  /**
   * Handler for when an option is selected (value, event) => {}
   */
  onValueClick: _propTypes2.default.func,
  /**
   * Text or node to show if none is selected
   */
  placeholder: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.node]),
  /**
   * Adds a class name to the element.
   */
  className: _propTypes2.default.string,
  /**
   * Adds an id to the element.
   */
  id: _propTypes2.default.string,
  /**
   * Indicates whether the user can interact with this field.
   */
  disabled: _propTypes2.default.bool,
  /**
   * Indicates whether this field is required for form submission. Non-required fields allow none.
   */
  required: _propTypes2.default.bool,
  /**
   * Styles this field as being invalid
   */
  invalid: _propTypes2.default.bool,
  /**
   * A list of selection options. Can be complex objects.
   */
  options: _propTypes2.default.array.isRequired,
  /**
   * A function which takes an option and renders text for the select. Has
   * a sane default.
   */
  renderOption: _propTypes2.default.func,
  /**
   * A function which takes an option and computes a single string value to
   * represent it. Has a sane default.
   */
  getOptionValue: _propTypes2.default.func,
  /**
   * The currently selected value.
   */
  value: _propTypes2.default.any,
  /**
   * Handler for change events on the select. It will be called with the new value
   * computed from getOptionValue.
   */
  onChange: _propTypes2.default.func.isRequired,
  /**
   * Whether to enable searching to filter to the desired item.
   */
  searchable: _propTypes2.default.bool,
  /**
   * Alias for isLoading
   */
  loading: _propTypes2.default.bool,
  /**
   * DEPRECATED: use getOptionValue
   */
  selectOptionValue: _propTypes2.default.func,
  /**
   * DEPRECATED: use required to prevent None, or omit it to allow None
   */
  allowNone: _propTypes2.default.bool,
  /**
   * DEPRECATED: use placeholder
   */
  noneText: _propTypes2.default.string,
  /**
   * A component that wraps the library select component, letting you
   * change styles.
   */
  Wrapper: _propTypes2.default.func
};
Select.defaultProps = {
  className: null,
  id: null,
  disabled: false,
  required: false,
  allowNone: false,
  invalid: false,
  placeholder: 'None',
  renderOption: _selectItemPrimaryValue2.default,
  getOptionValue: _selectItemPrimaryValue2.default,
  searchable: false,
  Wrapper: _SelectWrapper2.default
};
exports.default = Select;