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

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _SearchField = require('./SearchField');

var _SearchField2 = _interopRequireDefault(_SearchField);

var _RangeSearchField = require('../../../styles/cspace-ui/RangeSearchField.css');

var _RangeSearchField2 = _interopRequireDefault(_RangeSearchField);

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 messages = (0, _reactIntl.defineMessages)({
  fields: {
    'id': 'RangeSearchField.fields',
    'defaultMessage': '{startField} and {endField}'
  }
});

var propTypes = {
  parentPath: _propTypes2.default.arrayOf(_propTypes2.default.string),
  name: _propTypes2.default.string,
  value: _propTypes2.default.instanceOf(_immutable2.default.List),
  readOnly: _propTypes2.default.bool,
  inline: _propTypes2.default.bool,
  onCommit: _propTypes2.default.func
};

var defaultProps = {
  value: _immutable2.default.List()
};

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

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

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

    _this.handleStartFieldCommit = _this.handleStartFieldCommit.bind(_this);
    _this.handleEndFieldCommit = _this.handleEndFieldCommit.bind(_this);
    return _this;
  }

  _createClass(RangeSearchField, [{
    key: 'handleStartFieldCommit',
    value: function handleStartFieldCommit(path, startValue) {
      var _props = this.props,
          value = _props.value,
          onCommit = _props.onCommit;


      if (onCommit) {
        onCommit(path, value.set(0, startValue));
      }
    }
  }, {
    key: 'handleEndFieldCommit',
    value: function handleEndFieldCommit(path, endValue) {
      var _props2 = this.props,
          value = _props2.value,
          onCommit = _props2.onCommit;


      if (onCommit) {
        onCommit(path, value.set(1, endValue));
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _props3 = this.props,
          inline = _props3.inline,
          parentPath = _props3.parentPath,
          name = _props3.name,
          value = _props3.value,
          readOnly = _props3.readOnly;


      var startField = _react2.default.createElement(_SearchField2.default, {
        parentPath: parentPath,
        name: name,
        value: value.get(0),
        readOnly: readOnly,
        repeating: false,
        onCommit: this.handleStartFieldCommit
      });

      var endField = _react2.default.createElement(_SearchField2.default, {
        parentPath: parentPath,
        name: name,
        value: value.get(1),
        readOnly: readOnly,
        repeating: false,
        onCommit: this.handleEndFieldCommit
      });

      var className = inline ? _RangeSearchField2.default.inline : _RangeSearchField2.default.normal;

      return _react2.default.createElement(
        'div',
        { className: className },
        _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.fields, {
          tagName: 'div',
          values: { startField: startField, endField: endField }
        }))
      );
    }
  }]);

  return RangeSearchField;
}(_react.Component);

exports.default = RangeSearchField;


RangeSearchField.propTypes = propTypes;
RangeSearchField.defaultProps = defaultProps;