"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactIntl = require("react-intl");

var _immutable = _interopRequireDefault(require("immutable"));

var _get = _interopRequireDefault(require("lodash/get"));

var _SearchField = _interopRequireDefault(require("../SearchField"));

var _RangeSearchField = _interopRequireDefault(require("../RangeSearchField"));

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

var _dataTypes = require("../../../constants/dataTypes");

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

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

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _extends() { _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; }; return _extends.apply(this, arguments); }

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: _propTypes.default.instanceOf(_immutable.default.Map),
  inline: _propTypes.default.bool,
  readOnly: _propTypes.default.bool,
  onCommit: _propTypes.default.func
};
const contextTypes = {
  config: _propTypes.default.object,
  recordType: _propTypes.default.string
};

class FieldConditionInput extends _react.Component {
  constructor() {
    super();
    this.handleValueCommit = this.handleValueCommit.bind(this);
  }

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

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

  render() {
    const {
      condition,
      inline,
      readOnly
    } = this.props;
    const {
      config,
      recordType
    } = this.context;
    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 fieldConfig = (0, _get.default)(config, ['recordTypes', recordType, 'fields', ...path, _configHelpers.configKey]);
    const dataType = (0, _get.default)(fieldConfig, 'dataType');
    const messages = (0, _get.default)(fieldConfig, 'messages');
    const label = messages ? _react.default.createElement(_reactIntl.FormattedMessage, messages.fullName || messages.name) : name;
    const SearchFieldComponent = operator === _searchOperators.OP_RANGE ? _RangeSearchField.default : _SearchField.default;
    const className = inline ? _FieldConditionInput.default.inline : _FieldConditionInput.default.normal;
    const opMessages = inline ? operatorMessages.compact : operatorMessages.full;
    return _react.default.createElement("div", {
      className: className
    }, _react.default.createElement("div", null, label), ' ', _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, opMessages[operator], {
      tagName: "div"
    })), ' ', _react.default.createElement("div", null, _react.default.createElement(SearchFieldComponent, {
      inline: inline,
      parentPath: parentPath,
      name: name,
      readOnly: readOnly // Booleans only have two possible values, so null (don't care) or a single desired
      // value is sufficient to describe all searches, and there's no need to allow multiple
      // values.
      ,
      repeating: dataType !== _dataTypes.DATA_TYPE_BOOL,
      value: value,
      onCommit: this.handleValueCommit
    })));
  }

}

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