'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 _messages;

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

var _SearchConditionInput = require('./SearchConditionInput');

var _SearchConditionInput2 = _interopRequireDefault(_SearchConditionInput);

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

var _BooleanConditionInput = require('../../../../styles/cspace-ui/BooleanConditionInput.css');

var _BooleanConditionInput2 = _interopRequireDefault(_BooleanConditionInput);

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

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 DropdownMenuInput = _cspaceInput.baseComponents.DropdownMenuInput;


var messages = (_messages = {}, _defineProperty(_messages, _searchOperators.OP_AND, (0, _reactIntl.defineMessages)({
  label: {
    'id': 'booleanConditionInput.and.label',
    'defaultMessage': 'and'
  },
  opSelectorLabel: {
    'id': 'booleanConditionInput.and.opSelectorLabel',
    'defaultMessage': 'All'
  }
})), _defineProperty(_messages, _searchOperators.OP_OR, (0, _reactIntl.defineMessages)({
  label: {
    'id': 'booleanConditionInput.or.label',
    'defaultMessage': 'or'
  },
  opSelectorLabel: {
    'id': 'booleanConditionInput.or.opSelectorLabel',
    'defaultMessage': 'Any'
  }
})), _defineProperty(_messages, 'opSelector', (0, _reactIntl.defineMessages)({
  label: {
    'id': 'booleanConditionInput.opSelector.label',
    'defaultMessage': '{opSelectorInput} of the following conditions must be satisfied:'
  }
})), _messages);

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

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

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

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

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

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


      if (onCommit) {
        onCommit(condition.set('op', value));
      }
    }
  }, {
    key: 'handleChildConditionCommit',
    value: function handleChildConditionCommit(index, childCondition) {
      var _props2 = this.props,
          condition = _props2.condition,
          onCommit = _props2.onCommit;


      if (onCommit) {
        onCommit(condition.setIn(['value', index], childCondition));
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props3 = this.props,
          condition = _props3.condition,
          fields = _props3.fields,
          inline = _props3.inline,
          intl = _props3.intl,
          readOnly = _props3.readOnly;


      var handleChildConditionCommit = function handleChildConditionCommit(index) {
        return function (childCondition) {
          _this2.handleChildConditionCommit(index, childCondition);
        };
      };

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

      var inputs = childConditions.map(function (childCondition, index) {
        var operatorLabel = index > 0 ? _react2.default.createElement(_reactIntl.FormattedMessage, messages[operator].label) : _react2.default.createElement('span', null);

        return _react2.default.createElement(
          'li',
          { key: index },
          operatorLabel,
          _react2.default.createElement(_SearchConditionInput2.default, {
            condition: childCondition,
            fields: fields,
            index: index,
            inline: inline,
            readOnly: readOnly,
            onCommit: handleChildConditionCommit(index)
          })
        );
      });

      var opSelector = void 0;

      if (!readOnly) {
        var opSelectorInput = _react2.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 = _react2.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.opSelector.label, {
          tagName: 'div',
          values: { opSelectorInput: opSelectorInput }
        }));
      }

      var className = inline ? _BooleanConditionInput2.default.inline : _BooleanConditionInput2.default.normal;

      return _react2.default.createElement(
        'div',
        { className: className },
        opSelector,
        _react2.default.createElement(
          'ul',
          null,
          inputs
        )
      );
    }
  }]);

  return BooleanConditionInput;
}(_react.Component);

BooleanConditionInput.propTypes = propTypes;

exports.default = (0, _reactIntl.injectIntl)(BooleanConditionInput);