'use strict';

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

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 _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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _DropdownMenuInput = require('./DropdownMenuInput');

var _DropdownMenuInput2 = _interopRequireDefault(_DropdownMenuInput);

var _changeable = require('../enhancers/changeable');

var _changeable2 = _interopRequireDefault(_changeable);

var _optionHelpers = require('../helpers/optionHelpers');

var _pathHelpers = require('../helpers/pathHelpers');

var _ComboBoxInput = require('../../styles/cspace-input/ComboBoxInput.css');

var _ComboBoxInput2 = _interopRequireDefault(_ComboBoxInput);

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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 DropdownMenuInput = (0, _changeable2.default)(_DropdownMenuInput2.default);

var propTypes = _extends({}, DropdownMenuInput.propTypes, {
  blankable: _react.PropTypes.bool,
  onAddOption: _react.PropTypes.func
});

var defaultProps = {
  blankable: true
};

var ComboBoxInput = function (_Component) {
  _inherits(ComboBoxInput, _Component);

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

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

    _this.handleDropdownInputChange = _this.handleDropdownInputChange.bind(_this);
    _this.handleDropdownInputClose = _this.handleDropdownInputClose.bind(_this);
    _this.handleDropdownInputCommit = _this.handleDropdownInputCommit.bind(_this);
    _this.handleDropdownInputKeyDown = _this.handleDropdownInputKeyDown.bind(_this);
    _this.handleDropdownInputOpen = _this.handleDropdownInputOpen.bind(_this);

    _this.state = {
      isAdding: false,
      open: false,
      value: _this.props.value
    };
    return _this;
  }

  _createClass(ComboBoxInput, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      this.setState({
        value: nextProps.value
      });
    }
  }, {
    key: 'commit',
    value: function commit(value) {
      var onCommit = this.props.onCommit;


      if (onCommit) {
        onCommit((0, _pathHelpers.getPath)(this.props), value);
      }
    }
  }, {
    key: 'handleDropdownInputChange',
    value: function handleDropdownInputChange(value) {
      this.setState({
        isAdding: true,
        open: true,
        valueLabel: value
      });
    }
  }, {
    key: 'handleDropdownInputCommit',
    value: function handleDropdownInputCommit(path, value) {
      this.setState({
        value: value,
        isAdding: false,
        open: false
      });

      this.commit(value);
    }
  }, {
    key: 'handleDropdownInputKeyDown',
    value: function handleDropdownInputKeyDown(event) {
      if (event.key === 'Enter') {
        var _state = this.state,
            isAdding = _state.isAdding,
            valueLabel = _state.valueLabel;


        if (isAdding) {
          var _props = this.props,
              blankable = _props.blankable,
              options = _props.options,
              onAddOption = _props.onAddOption;

          // If the current content of the input matches an option exactly, select it.

          var matchingOption = (0, _optionHelpers.getOptionForLabel)(options, valueLabel);

          if (matchingOption) {
            this.setState({
              isAdding: false,
              open: false,
              value: matchingOption.value,
              valueLabel: matchingOption.valueLabel
            });

            this.commit(matchingOption.value);
          } else if (valueLabel !== '' || blankable) {
            this.setState({
              valueLabel: valueLabel,
              open: false
            });

            if (onAddOption) {
              onAddOption(valueLabel);
            }
          }
        }
      }
    }
  }, {
    key: 'handleDropdownInputClose',
    value: function handleDropdownInputClose() {
      this.setState({
        isAdding: false,
        open: false
      });

      var onClose = this.props.onClose;


      if (onClose) {
        onClose();
      }
    }
  }, {
    key: 'handleDropdownInputOpen',
    value: function handleDropdownInputOpen() {
      this.setState({
        isAdding: false,
        open: true
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _state2 = this.state,
          isAdding = _state2.isAdding,
          open = _state2.open,
          value = _state2.value,
          valueLabel = _state2.valueLabel;

      var _props2 = this.props,
          className = _props2.className,
          onAddOption = _props2.onAddOption,
          remainingProps = _objectWithoutProperties(_props2, ['className', 'onAddOption']);

      var valueProp = void 0;

      if (isAdding) {
        valueProp = { valueLabel: valueLabel };
      } else if ((0, _optionHelpers.getOptionForValue)(this.props.options, value)) {
        valueProp = { value: value };
      } else {
        valueProp = { valueLabel: value };
      }

      var classes = (0, _classnames2.default)(className, _defineProperty({}, _ComboBoxInput2.default.adding, isAdding));

      return _react2.default.createElement(DropdownMenuInput, _extends({}, remainingProps, valueProp, {
        className: classes,
        open: open,
        onChange: this.handleDropdownInputChange,
        onClose: this.handleDropdownInputClose,
        onCommit: this.handleDropdownInputCommit,
        onKeyDown: this.handleDropdownInputKeyDown,
        onOpen: this.handleDropdownInputOpen
      }));
    }
  }]);

  return ComboBoxInput;
}(_react.Component);

exports.default = ComboBoxInput;


ComboBoxInput.propTypes = propTypes;
ComboBoxInput.defaultProps = defaultProps;