'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 _defineMessages, _defineMessages2;

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 _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

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

var _SearchField2 = _interopRequireDefault(_SearchField);

var _RangeSearchField = require('../RangeSearchField');

var _RangeSearchField2 = _interopRequireDefault(_RangeSearchField);

var _configHelpers = require('../../../helpers/configHelpers');

var _searchOperators = require('../../../constants/searchOperators');

var _FieldConditionInput = require('../../../../styles/cspace-ui/FieldConditionInput.css');

var _FieldConditionInput2 = _interopRequireDefault(_FieldConditionInput);

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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

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

var operatorMessages = {
  full: (0, _reactIntl.defineMessages)((_defineMessages = {}, _defineProperty(_defineMessages, _searchOperators.OP_CONTAIN, {
    'id': 'fieldConditionInput.OP_CONTAIN.full',
    'defaultMessage': 'contains'
  }), _defineProperty(_defineMessages, _searchOperators.OP_EQ, {
    'id': 'fieldConditionInput.OP_EQ.full',
    'defaultMessage': 'is'
  }), _defineProperty(_defineMessages, _searchOperators.OP_GT, {
    'id': 'fieldConditionInput.OP_GT.full',
    'defaultMessage': 'is greater than'
  }), _defineProperty(_defineMessages, _searchOperators.OP_GTE, {
    'id': 'fieldConditionInput.OP_GTE.full',
    'defaultMessage': 'is greater than or equal to'
  }), _defineProperty(_defineMessages, _searchOperators.OP_LT, {
    'id': 'fieldConditionInput.OP_LT.full',
    'defaultMessage': 'is less than'
  }), _defineProperty(_defineMessages, _searchOperators.OP_LTE, {
    'id': 'fieldConditionInput.OP_LTE.full',
    'defaultMessage': 'is less than or equal to'
  }), _defineProperty(_defineMessages, _searchOperators.OP_MATCH, {
    'id': 'fieldConditionInput.OP_MATCH.full',
    'defaultMessage': 'matches'
  }), _defineProperty(_defineMessages, _searchOperators.OP_RANGE, {
    'id': 'fieldConditionInput.OP_RANGE.full',
    'defaultMessage': 'is between'
  }), _defineMessages)),
  compact: (0, _reactIntl.defineMessages)((_defineMessages2 = {}, _defineProperty(_defineMessages2, _searchOperators.OP_CONTAIN, {
    'id': 'fieldConditionInput.OP_CONTAIN.compact',
    'defaultMessage': 'contains'
  }), _defineProperty(_defineMessages2, _searchOperators.OP_EQ, {
    'id': 'fieldConditionInput.OP_EQ.compact',
    'defaultMessage': '='
  }), _defineProperty(_defineMessages2, _searchOperators.OP_GT, {
    'id': 'fieldConditionInput.OP_GT.compact',
    'defaultMessage': '>'
  }), _defineProperty(_defineMessages2, _searchOperators.OP_GTE, {
    'id': 'fieldConditionInput.OP_GTE.compact',
    'defaultMessage': '\u2265'
  }), _defineProperty(_defineMessages2, _searchOperators.OP_LT, {
    'id': 'fieldConditionInput.OP_LT.compact',
    'defaultMessage': '<'
  }), _defineProperty(_defineMessages2, _searchOperators.OP_LTE, {
    'id': 'fieldConditionInput.OP_LTE.compact',
    'defaultMessage': '\u2264'
  }), _defineProperty(_defineMessages2, _searchOperators.OP_MATCH, {
    'id': 'fieldConditionInput.OP_MATCH.compact',
    'defaultMessage': 'matches'
  }), _defineProperty(_defineMessages2, _searchOperators.OP_RANGE, {
    'id': 'fieldConditionInput.OP_RANGE.compact',
    'defaultMessage': 'between'
  }), _defineMessages2))
};

var propTypes = {
  condition: _propTypes2.default.instanceOf(_immutable2.default.Map),
  inline: _propTypes2.default.bool,
  readOnly: _propTypes2.default.bool,
  onCommit: _propTypes2.default.func
};

var contextTypes = {
  config: _propTypes2.default.object,
  recordType: _propTypes2.default.string
};

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

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

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

    _this.handleValueCommit = _this.handleValueCommit.bind(_this);
    return _this;
  }

  _createClass(FieldConditionInput, [{
    key: 'handleValueCommit',
    value: function handleValueCommit(path, value) {
      var _props = this.props,
          condition = _props.condition,
          onCommit = _props.onCommit;


      if (onCommit) {
        onCommit(condition.set('value', value));
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          condition = _props2.condition,
          inline = _props2.inline,
          readOnly = _props2.readOnly;
      var _context = this.context,
          config = _context.config,
          recordType = _context.recordType;


      var operator = condition.get('op');
      var pathSpec = condition.get('path');
      var value = condition.get('value');

      var path = ['document'].concat(_toConsumableArray(pathSpec.split('/')));
      var name = path[path.length - 1];
      var parentPath = path.slice(0, path.length - 1);

      var messages = (0, _get2.default)(config, ['recordTypes', recordType, 'fields'].concat(_toConsumableArray(path), [_configHelpers.configKey, 'messages']));

      var label = messages ? _react2.default.createElement(_reactIntl.FormattedMessage, messages.fullName || messages.name) : name;

      var SearchFieldComponent = operator === _searchOperators.OP_RANGE ? _RangeSearchField2.default : _SearchField2.default;
      var className = inline ? _FieldConditionInput2.default.inline : _FieldConditionInput2.default.normal;
      var opMessages = inline ? operatorMessages.compact : operatorMessages.full;

      return _react2.default.createElement(
        'div',
        { className: className },
        _react2.default.createElement(
          'div',
          null,
          label
        ),
        ' ',
        _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, opMessages[operator], { tagName: 'div' })),
        ' ',
        _react2.default.createElement(
          'div',
          null,
          _react2.default.createElement(SearchFieldComponent, {
            inline: inline,
            parentPath: parentPath,
            name: name,
            readOnly: readOnly,
            value: value,
            onCommit: this.handleValueCommit
          })
        )
      );
    }
  }]);

  return FieldConditionInput;
}(_react.Component);

exports.default = FieldConditionInput;


FieldConditionInput.propTypes = propTypes;
FieldConditionInput.contextTypes = contextTypes;