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

var _react2 = _interopRequireDefault(_react);

var _LineInput = require('./LineInput');

var _LineInput2 = _interopRequireDefault(_LineInput);

var _RecordTypeInput = require('./RecordTypeInput');

var _RecordTypeInput2 = _interopRequireDefault(_RecordTypeInput);

var _VocabularyInput = require('./VocabularyInput');

var _VocabularyInput2 = _interopRequireDefault(_VocabularyInput);

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

var _changeable2 = _interopRequireDefault(_changeable);

var _committable = require('../enhancers/committable');

var _committable2 = _interopRequireDefault(_committable);

var _QuickSearchInput = require('../../styles/cspace-input/QuickSearchInput.css');

var _QuickSearchInput2 = _interopRequireDefault(_QuickSearchInput);

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

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 LineInput = (0, _committable2.default)((0, _changeable2.default)(_LineInput2.default));

var propTypes = {
  formatRecordTypeLabel: _react.PropTypes.func,
  formatVocabularyLabel: _react.PropTypes.func,
  keywordValue: _react.PropTypes.string,
  placeholder: _react.PropTypes.string,
  recordTypes: _react.PropTypes.object,
  recordTypeValue: _react.PropTypes.string,
  vocabularyValue: _react.PropTypes.string,
  onSearch: _react.PropTypes.func,
  onKeywordCommit: _react.PropTypes.func,
  onRecordTypeCommit: _react.PropTypes.func,
  onVocabularyCommit: _react.PropTypes.func
};

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

  function QuickSearchInput() {
    _classCallCheck(this, QuickSearchInput);

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

    _this.handleKeyPress = _this.handleKeyPress.bind(_this);
    _this.handleKeywordInputCommit = _this.handleKeywordInputCommit.bind(_this);
    _this.handleRecordTypeDropdownCommit = _this.handleRecordTypeDropdownCommit.bind(_this);
    _this.handleRecordTypeDropdownUpdate = _this.handleRecordTypeDropdownUpdate.bind(_this);
    _this.handleVocabularyDropdownCommit = _this.handleVocabularyDropdownCommit.bind(_this);
    _this.handleVocabularyDropdownUpdate = _this.handleVocabularyDropdownUpdate.bind(_this);
    return _this;
  }

  _createClass(QuickSearchInput, [{
    key: 'handleKeywordInputCommit',
    value: function handleKeywordInputCommit(path, value) {
      var onKeywordCommit = this.props.onKeywordCommit;


      if (onKeywordCommit) {
        onKeywordCommit(value);
      }
    }
  }, {
    key: 'handleKeyPress',
    value: function handleKeyPress(event) {
      if (event.key === 'Enter') {
        var onSearch = this.props.onSearch;


        if (onSearch) {
          onSearch();
        }
      }
    }
  }, {
    key: 'handleRecordTypeDropdownCommit',
    value: function handleRecordTypeDropdownCommit(path, value) {
      var onRecordTypeCommit = this.props.onRecordTypeCommit;


      if (onRecordTypeCommit) {
        onRecordTypeCommit(value);
      }
    }
  }, {
    key: 'handleRecordTypeDropdownUpdate',
    value: function handleRecordTypeDropdownUpdate(_ref) {
      var value = _ref.value;

      if (value !== this.props.recordTypeValue) {
        // A default was selected.

        var onRecordTypeCommit = this.props.onRecordTypeCommit;


        if (onRecordTypeCommit) {
          onRecordTypeCommit(value);
        }
      }
    }
  }, {
    key: 'handleVocabularyDropdownCommit',
    value: function handleVocabularyDropdownCommit(path, value) {
      var onVocabularyCommit = this.props.onVocabularyCommit;


      if (onVocabularyCommit) {
        onVocabularyCommit(value);
      }
    }
  }, {
    key: 'handleVocabularyDropdownUpdate',
    value: function handleVocabularyDropdownUpdate(_ref2) {
      var value = _ref2.value;

      if (value !== this.props.vocabularyValue) {
        // A default was selected.

        var onVocabularyCommit = this.props.onVocabularyCommit;


        if (onVocabularyCommit) {
          onVocabularyCommit(value);
        }
      }
    }
  }, {
    key: 'renderRecordTypeDropdown',
    value: function renderRecordTypeDropdown() {
      var _props = this.props,
          formatRecordTypeLabel = _props.formatRecordTypeLabel,
          recordTypes = _props.recordTypes,
          recordTypeValue = _props.recordTypeValue;


      return _react2.default.createElement(_RecordTypeInput2.default, {
        embedded: true,
        formatRecordTypeLabel: formatRecordTypeLabel,
        recordTypes: recordTypes,
        value: recordTypeValue,
        onCommit: this.handleRecordTypeDropdownCommit,
        onKeyPress: this.handleKeyPress,
        onMount: this.handleRecordTypeDropdownUpdate,
        onUpdate: this.handleRecordTypeDropdownUpdate
      });
    }
  }, {
    key: 'renderVocabularyDropdown',
    value: function renderVocabularyDropdown() {
      var _props2 = this.props,
          formatVocabularyLabel = _props2.formatVocabularyLabel,
          recordTypes = _props2.recordTypes,
          recordTypeValue = _props2.recordTypeValue,
          vocabularyValue = _props2.vocabularyValue;


      return _react2.default.createElement(_VocabularyInput2.default, {
        embedded: true,
        formatVocabularyLabel: formatVocabularyLabel,
        recordTypes: recordTypes,
        recordType: recordTypeValue,
        value: vocabularyValue,
        onCommit: this.handleVocabularyDropdownCommit,
        onKeyPress: this.handleKeyPress,
        onMount: this.handleVocabularyDropdownUpdate,
        onUpdate: this.handleVocabularyDropdownUpdate
      });
    }
  }, {
    key: 'renderKeywordInput',
    value: function renderKeywordInput() {
      var _props3 = this.props,
          keywordValue = _props3.keywordValue,
          placeholder = _props3.placeholder;


      return _react2.default.createElement(LineInput, {
        embedded: true,
        placeholder: placeholder,
        value: keywordValue,
        onCommit: this.handleKeywordInputCommit,
        onKeyPress: this.handleKeyPress
      });
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        { className: _QuickSearchInput2.default.normal },
        this.renderRecordTypeDropdown(),
        this.renderVocabularyDropdown(),
        this.renderKeywordInput()
      );
    }
  }]);

  return QuickSearchInput;
}(_react.Component);

exports.default = QuickSearchInput;


QuickSearchInput.propTypes = propTypes;