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

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

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

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

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

    this.handleValueCommit = this.handleValueCommit.bind(this);
  }

  handleValueCommit(path, value) {
    var _props = this.props;
    const condition = _props.condition,
          onCommit = _props.onCommit;


    if (onCommit) {
      onCommit(condition.set('value', value));
    }
  }

  render() {
    var _props2 = this.props;
    const condition = _props2.condition,
          inline = _props2.inline,
          readOnly = _props2.readOnly;
    var _context = this.context;
    const config = _context.config,
          recordType = _context.recordType;


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

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

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

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

    const SearchFieldComponent = operator === _searchOperators.OP_RANGE ? _RangeSearchField2.default : _SearchField2.default;
    const className = inline ? _FieldConditionInput2.default.inline : _FieldConditionInput2.default.normal;
    const 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
        })
      )
    );
  }
}

exports.default = FieldConditionInput;
FieldConditionInput.propTypes = propTypes;
FieldConditionInput.contextTypes = contextTypes;