"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 _cspaceInput = require("cspace-input");

var _SearchConditionInput = _interopRequireDefault(require("./SearchConditionInput"));

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

var _BooleanConditionInput = _interopRequireDefault(require("../../../../styles/cspace-ui/BooleanConditionInput.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 {
  DropdownMenuInput
} = _cspaceInput.baseComponents;
const messages = {
  [_searchOperators.OP_AND]: (0, _reactIntl.defineMessages)({
    label: {
      "id": "booleanConditionInput.and.label",
      "defaultMessage": "and"
    },
    opSelectorLabel: {
      "id": "booleanConditionInput.and.opSelectorLabel",
      "defaultMessage": "All"
    }
  }),
  [_searchOperators.OP_OR]: (0, _reactIntl.defineMessages)({
    label: {
      "id": "booleanConditionInput.or.label",
      "defaultMessage": "or"
    },
    opSelectorLabel: {
      "id": "booleanConditionInput.or.opSelectorLabel",
      "defaultMessage": "Any"
    }
  }),
  opSelector: (0, _reactIntl.defineMessages)({
    label: {
      "id": "booleanConditionInput.opSelector.label",
      "defaultMessage": "{opSelectorInput} of the following conditions {operator, select, and {must} or {may}} be satisfied:"
    }
  })
};
const propTypes = {
  condition: _propTypes.default.instanceOf(_immutable.default.Map),
  fields: _propTypes.default.object,
  inline: _propTypes.default.bool,
  intl: _reactIntl.intlShape,
  readOnly: _propTypes.default.bool,
  onCommit: _propTypes.default.func
};

class BooleanConditionInput extends _react.Component {
  constructor() {
    super();
    this.handleOpSelectorCommit = this.handleOpSelectorCommit.bind(this);
  }

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

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

  handleChildConditionCommit(index, childCondition) {
    const {
      condition,
      onCommit
    } = this.props;

    if (onCommit) {
      onCommit(condition.setIn(['value', index], childCondition));
    }
  }

  render() {
    const {
      condition,
      fields,
      inline,
      intl,
      readOnly
    } = this.props;

    const handleChildConditionCommit = index => childCondition => {
      this.handleChildConditionCommit(index, childCondition);
    };

    const operator = condition.get('op');
    const childConditions = condition.get('value');
    const inputs = childConditions.map((childCondition, index) => {
      const operatorLabel = index > 0 ? _react.default.createElement(_reactIntl.FormattedMessage, messages[operator].label) : _react.default.createElement("span", null);
      return _react.default.createElement("li", {
        key: index
      }, operatorLabel, _react.default.createElement(_SearchConditionInput.default, {
        condition: childCondition,
        fields: fields,
        index: index,
        inline: inline,
        readOnly: readOnly,
        onCommit: handleChildConditionCommit(index)
      }));
    });
    let opSelector;

    if (!readOnly) {
      const opSelectorInput = _react.default.createElement(DropdownMenuInput, {
        name: "booleanSearchOp",
        options: [{
          value: _searchOperators.OP_OR,
          label: intl.formatMessage(messages[_searchOperators.OP_OR].opSelectorLabel)
        }, {
          value: _searchOperators.OP_AND,
          label: intl.formatMessage(messages[_searchOperators.OP_AND].opSelectorLabel)
        }],
        value: operator,
        onCommit: this.handleOpSelectorCommit
      });

      opSelector = _react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.opSelector.label, {
        tagName: "div",
        values: {
          opSelectorInput,
          operator
        }
      }));
    }

    const className = inline ? _BooleanConditionInput.default.inline : _BooleanConditionInput.default.normal;
    return _react.default.createElement("div", {
      className: className
    }, opSelector, _react.default.createElement("ul", null, inputs));
  }

}

BooleanConditionInput.propTypes = propTypes;

var _default = (0, _reactIntl.injectIntl)(BooleanConditionInput);

exports.default = _default;