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

const DropdownMenuInput = _cspaceInput.baseComponents.DropdownMenuInput;


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 must be satisfied:'
    }
  })
};

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

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

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

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


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

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


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

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


    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 ? _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)
        })
      );
    });

    let opSelector;

    if (!readOnly) {
      const 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 }
      }));
    }

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

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

BooleanConditionInput.propTypes = propTypes;

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