'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 _reactOnclickoutside = require('react-onclickoutside');

var _reactOnclickoutside2 = _interopRequireDefault(_reactOnclickoutside);

var _ramda = require('ramda');

var _template = require('./template.js');

var _template2 = _interopRequireDefault(_template);

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 SelectInputContainer = function (_React$PureComponent) {
  _inherits(SelectInputContainer, _React$PureComponent);

  function SelectInputContainer(props) {
    _classCallCheck(this, SelectInputContainer);

    var _this = _possibleConstructorReturn(this, (SelectInputContainer.__proto__ || Object.getPrototypeOf(SelectInputContainer)).call(this, props));

    _this.state = {
      expanded: _this.props.opened,
      search: '',
      value: _this.props.value
    };
    _this.handleBlur = _this.handleBlur.bind(_this);
    _this.handleChange = _this.handleChange.bind(_this);
    _this.handleClick = _this.handleClick.bind(_this);
    _this.handleFocus = _this.handleFocus.bind(_this);
    return _this;
  }

  _createClass(SelectInputContainer, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      if (!(0, _ramda.equals)(this.props.value, nextProps.value)) {
        this.setState({ value: nextProps.value });
      }
    }
  }, {
    key: 'handleClick',
    value: function handleClick(item) {
      this.setState({ value: item.value, expanded: false, search: '' });
      if (this.props.onChange) {
        this.props.onChange(item.value);
      }
    }
  }, {
    key: 'handleChange',
    value: function handleChange(event) {
      this.setState({ search: event.target.value });
    }
  }, {
    key: 'handleBlur',
    value: function handleBlur() {
      this.setState({ expanded: false, search: '' });
      if (this.props.onBlur) {
        this.props.onBlur();
      }
      if (this.props.onChange) {
        this.props.onChange(this.state.value);
      }
    }
  }, {
    key: 'handleFocus',
    value: function handleFocus() {
      this.setState({ expanded: true });
      if (this.props.onFocus) {
        this.props.onFocus();
      }
    }
  }, {
    key: 'handleClickOutside',
    value: function handleClickOutside() {
      this.setState({ expanded: false, search: '' });
    }
  }, {
    key: 'transform',
    value: function transform(elements, search) {
      var items = [];
      elements.map(function (element) {
        if (!search && element.group !== '') {
          items.push({ text: element.group });
        }
        element.items.map(function (item) {
          if (!search || search && (0, _ramda.contains)((0, _ramda.toUpper)(search), (0, _ramda.toUpper)(item.text))) {
            items.push({ text: item.text, value: item.value });
          }
        });
      });
      return items;
    }
  }, {
    key: 'getSelected',
    value: function getSelected(items, value) {
      if ((0, _ramda.isNil)(value) || (0, _ramda.isEmpty)(value)) return undefined;
      return (0, _ramda.head)((0, _ramda.filter)(function (x) {
        return (0, _ramda.equals)(x.value, value);
      }, items));
    }
  }, {
    key: 'render',
    value: function render() {
      var _state = this.state,
          search = _state.search,
          value = _state.value,
          expanded = _state.expanded;

      var _props = this.props,
          elements = _props.elements,
          label = _props.label,
          searchEnabled = _props.searchEnabled,
          disabled = _props.disabled,
          rest = _objectWithoutProperties(_props, ['elements', 'label', 'searchEnabled', 'disabled']);

      var items = this.transform(elements, search);
      var selected = this.getSelected(items, value);

      return _react2.default.createElement(_template2.default, _extends({
        items: items,
        selected: selected,
        defaultDisplay: label,
        expanded: expanded,
        disabled: disabled,
        handleBlur: this.handleBlur,
        handleChange: this.handleChange,
        handleClick: this.handleClick,
        handleFocus: this.handleFocus,
        searchEnabled: this.props.searchEnabled
      }, rest));
    }
  }]);

  return SelectInputContainer;
}(_react2.default.PureComponent);

SelectInputContainer.propTypes = {
  elements: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    group: _propTypes2.default.string.isRequired,
    items: _propTypes2.default.array.isRequired
  })).isRequired,
  label: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.object]),
  searchEnabled: _propTypes2.default.bool,
  opened: _propTypes2.default.bool,
  disabled: _propTypes2.default.bool,
  value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number, _propTypes2.default.object]).isRequired,
  onChange: _propTypes2.default.func,
  onBlur: _propTypes2.default.func,
  onFocus: _propTypes2.default.func,
  templateDisplay: _propTypes2.default.func,
  templateHeader: _propTypes2.default.func,
  templateItem: _propTypes2.default.func
};

SelectInputContainer.defaultProps = {
  label: 'Select a value',
  searchEnabled: true,
  opened: false,
  disabled: false
};

exports.default = (0, _reactOnclickoutside2.default)(SelectInputContainer);