'use strict';

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; }; }();

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

var React = require('react');
var PropTypes = require('prop-types');
var RuleFieldSelector = require('./rule-field-selector');
var RuleCategorySelector = require('./rule-category-selector');
var RuleDeleteButton = require('./rule-delete-button');
var ValidationActions = require('../actions');

var Form = require('react-bootstrap').Form;

var ruleCategories = require('./rule-categories');
var _ = require('lodash');

// const debug = require('debug')('compass:validation:rule');

/**
 * Implements a single rule with RuleFieldSelector, RuleCategorySelector and
 * elements specific to the selected category.
 */

var Rule = function (_React$Component) {
  _inherits(Rule, _React$Component);

  function Rule(props) {
    _classCallCheck(this, Rule);

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

    _this.state = {
      isValid: true
    };
    return _this;
  }

  _createClass(Rule, [{
    key: 'onDeleteClicked',
    value: function onDeleteClicked() {
      ValidationActions.deleteValidationRule(this.props.id);
    }
  }, {
    key: 'checkBoxClicked',
    value: function checkBoxClicked() {
      ValidationActions.setRuleNullable(this.props.id, !this.props.nullable);
    }

    /**
     * validates all children components and combines the result for its own
     * isValid state.
     * @param  {Boolean} force    force validation (before submit)
     * @return {Boolean}          whether or not it valid inputs.
     */

  }, {
    key: 'validate',
    value: function validate(force) {
      var fieldValid = this.refs.RuleFieldSelector.validate(force);
      var categoryValid = this.refs.RuleCategorySelector.validate(force);

      var isValid = fieldValid && categoryValid;
      if (this.refs.Parameters && this.refs.Parameters.validate) {
        isValid = isValid && this.refs.Parameters.validate(force);
      }

      this.setState({
        isValid: isValid
      });
      return isValid;
    }
  }, {
    key: 'render',
    value: function render() {
      var Parameters = _.get(ruleCategories, this.props.category, null);
      var ruleParameters = Parameters ? React.createElement(Parameters, {
        ref: 'Parameters',
        id: this.props.id,
        serverVersion: this.props.serverVersion,
        parameters: this.props.parameters,
        isWritable: this.props.isWritable
      }) : null;

      var nullableDisabled = _.includes(['exists', 'mustNotExist', ''], this.props.category);

      return React.createElement(
        'tr',
        null,
        React.createElement(
          'td',
          null,
          React.createElement(RuleFieldSelector, {
            ref: 'RuleFieldSelector',
            id: this.props.id,
            field: this.props.field,
            isWritable: this.props.isWritable
          })
        ),
        React.createElement(
          'td',
          null,
          React.createElement(
            Form,
            { inline: true, className: 'rule-category-form' },
            React.createElement(RuleCategorySelector, {
              ref: 'RuleCategorySelector',
              id: this.props.id,
              category: this.props.category,
              isWritable: this.props.isWritable
            }),
            ruleParameters
          )
        ),
        React.createElement(
          'td',
          null,
          React.createElement('input', {
            className: 'nullable',
            type: 'checkbox',
            checked: this.props.nullable,
            disabled: nullableDisabled || !this.props.isWritable,
            onChange: this.checkBoxClicked.bind(this) })
        ),
        React.createElement(
          'td',
          null,
          this.props.isWritable ? React.createElement(RuleDeleteButton, {
            id: this.props.id,
            onClick: this.onDeleteClicked.bind(this) }) : null
        )
      );
    }
  }]);

  return Rule;
}(React.Component);

Rule.propTypes = {
  id: PropTypes.string.isRequired,
  field: PropTypes.string.isRequired,
  category: PropTypes.string.isRequired,
  parameters: PropTypes.object.isRequired,
  nullable: PropTypes.bool.isRequired,
  serverVersion: PropTypes.string.isRequired,
  isWritable: PropTypes.bool.isRequired
};

Rule.displayName = 'Rule';

module.exports = Rule;