'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 _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 }; }

const messages = (0, _reactIntl.defineMessages)({
  fields: {
    'id': 'RangeSearchField.fields',
    'defaultMessage': '{startField} and {endField}'
  }
});

const 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
};

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

class RangeSearchField extends _react.Component {
  constructor() {
    super();

    this.handleStartFieldCommit = this.handleStartFieldCommit.bind(this);
    this.handleEndFieldCommit = this.handleEndFieldCommit.bind(this);
  }

  handleStartFieldCommit(path, startValue) {
    var _props = this.props;
    const value = _props.value,
          onCommit = _props.onCommit;


    if (onCommit) {
      onCommit(path, value.set(0, startValue));
    }
  }

  handleEndFieldCommit(path, endValue) {
    var _props2 = this.props;
    const value = _props2.value,
          onCommit = _props2.onCommit;


    if (onCommit) {
      onCommit(path, value.set(1, endValue));
    }
  }

  render() {
    var _props3 = this.props;
    const inline = _props3.inline,
          parentPath = _props3.parentPath,
          name = _props3.name,
          value = _props3.value,
          readOnly = _props3.readOnly;


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

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

    const 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, endField }
      }))
    );
  }
}

exports.default = RangeSearchField;
RangeSearchField.propTypes = propTypes;
RangeSearchField.defaultProps = defaultProps;