'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 ValidationAction = require('../../actions');
var BSONTypeSelector = require('../common/bson-type-selector');
var _ = require('lodash');

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

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

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

    return _possibleConstructorReturn(this, (RuleCategoryType.__proto__ || Object.getPrototypeOf(RuleCategoryType)).apply(this, arguments));
  }

  _createClass(RuleCategoryType, [{
    key: 'onTypeClicked',
    value: function onTypeClicked(type, evt) {
      evt.preventDefault();
      ValidationAction.setRuleParameters(this.props.id, {
        type: type.number
      });
    }

    /**
     * get the initial parameters for this rule category.
     *
     * @return {Object}   the parameters for this rule.
     */

  }, {
    key: 'render',


    /**
     * Render TypeParameters component.
     *
     * @returns {React.Component} The view component.
     */
    value: function render() {
      var typeNumber = this.props.parameters.type;

      return React.createElement(BSONTypeSelector, {
        typeNumber: typeNumber,
        serverVersion: this.props.serverVersion,
        isDisabled: !this.props.isWritable,
        onTypeClicked: this.onTypeClicked.bind(this) });
    }
  }], [{
    key: 'getInitialParameters',
    value: function getInitialParameters() {
      return {
        type: 1
      };
    }

    /**
     * Convert the parameters describing the state of this rule to a query
     * value that MongoDB understands.
     *
     * @param {Object} params   the parameters describing the state of this rule
     *
     * @return {Object}   the value describing this rule.
     */

  }, {
    key: 'paramsToQuery',
    value: function paramsToQuery(params) {
      if (_.has(params, 'type')) {
        return {
          $type: _.get(params, 'type')
        };
      }
      return false;
    }

    /**
     * Detect if a query can be represented by this rule, and if it can,
     * convert the a query value returned from the server to an object of
     * parameters describing the state of this rule.
     *
     * @param {Object} query   The query value for this field.
     *
     * @return {Object|Boolean}  the parameters describing the state of this rule
     *                           or false if the query cannot be described by
     *                           this rule.
     */

  }, {
    key: 'queryToParams',
    value: function queryToParams(query) {
      if (_.isEqual(_.keys(query), ['$type'])) {
        // if $type is number, use directly
        if (_.isNumber(query.$type)) {
          return { type: query.$type };
        }
        // if $type is string, assume alias and try to get a type
        if (_.isString(query.$type)) {
          var type = BSONTypeSelector.getTypeByAlias(query.$type);
          if (type) {
            return { type: type.number };
          }
        }
      }
      // in all other cases, this rule cannot represent the query
      return false;
    }
  }]);

  return RuleCategoryType;
}(React.Component);

RuleCategoryType.propTypes = {
  id: PropTypes.string.isRequired,
  parameters: PropTypes.object.isRequired,
  serverVersion: PropTypes.string.isRequired,
  isWritable: PropTypes.bool.isRequired
};

RuleCategoryType.displayName = 'RuleCategoryType';

module.exports = RuleCategoryType;