'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 ValidationActions = require('../actions');
var ruleCategories = require('./rule-categories');

var _require = require('react-bootstrap'),
    FormGroup = _require.FormGroup;

var _require2 = require('hadron-react-components'),
    OptionSelector = _require2.OptionSelector;

var _ = require('lodash');

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

/**
 * This component is a dropdown to choose one of the rules defined under
 * ./rule-categories.
 */

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

  /**
   * constructor sets the initial state.
   *
   * @param {Object} props   initial props, passed to super class.
   */
  function RuleCategorySelector(props) {
    _classCallCheck(this, RuleCategorySelector);

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

    _this.state = {
      hasStartedValidating: false,
      isValid: true,
      category: props.category || ''
    };
    return _this;
  }

  _createClass(RuleCategorySelector, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(props) {
      this.setState({
        category: props.category || ''
      });
    }
  }, {
    key: 'onSelect',
    value: function onSelect(category) {
      this.setState({
        category: category,
        isValid: true,
        hasStartedValidating: true
      });
      ValidationActions.setRuleCategory(this.props.id, category);
    }
  }, {
    key: 'validate',
    value: function validate(force) {
      if (!force && !this.state.hasStartedValidating) {
        return true;
      }
      var isValid = this.state.category !== '';
      this.setState({
        hasStartedValidating: true,
        isValid: isValid
      });
      return isValid;
    }

    /**
     * Render RuleCategorySelector
     *
     * @returns {React.Component} The view component.
     */

  }, {
    key: 'render',
    value: function render() {
      var dropdownOptions = _.zipObject(_.keys(ruleCategories), _.map(_.keys(ruleCategories), _.startCase));

      var validationState = this.state.isValid ? null : 'error';

      return React.createElement(
        FormGroup,
        { validationState: validationState },
        React.createElement(OptionSelector, {
          options: dropdownOptions,
          id: this.props.id,
          label: '',
          title: dropdownOptions[this.state.category] || 'Select rule category',
          onSelect: this.onSelect.bind(this),
          disabled: !this.props.isWritable
        })
      );
    }
  }]);

  return RuleCategorySelector;
}(React.Component);

RuleCategorySelector.propTypes = {
  id: PropTypes.string.isRequired,
  category: PropTypes.string.isRequired,
  isWritable: PropTypes.bool
};

RuleCategorySelector.displayName = 'RuleCategorySelector';

module.exports = RuleCategorySelector;