'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 _require = require('hadron-react-components'),
    StatusRow = _require.StatusRow;

var React = require('react');
var PropTypes = require('prop-types');
var ValidationActions = require('../actions');
var ValidationStatusRow = require('./common/status-row');
var ViewSwitcher = require('./common/view-switcher');
var RuleBuilder = require('./rule-builder');
var JSONView = require('./json-view');

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

/**
 * Top-level Validation component which includes status rows at the top,
 * the ViewSwitcher element to toggle Rule Builder vs. Raw JSON editor view,
 * and the actual view.
 */

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

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

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

    _this.CollectionStore = global.hadronApp.appRegistry.getStore('App.CollectionStore');
    return _this;
  }

  /**
   * The view switcher was clicked, switch to the correct view.
   *
   * @param {String} viewName   Name of the clicked view: `Rule Builder` or `JSON`
   */


  _createClass(Validation, [{
    key: 'switchView',
    value: function switchView(viewName) {
      ValidationActions.switchView(viewName);
    }

    /**
     * Render ValidationComponent
     *
     * @returns {React.Component} The rendered component.
     */

  }, {
    key: 'renderComponent',
    value: function renderComponent() {
      var view = this.props.viewMode === 'Rule Builder' ? React.createElement(
        'div',
        { className: 'validation validation-rule-builder-wrapper' },
        React.createElement(RuleBuilder, {
          validationRules: this.props.validationRules,
          validationAction: this.props.validationAction,
          validationLevel: this.props.validationLevel,
          editState: this.props.editState,
          serverVersion: this.props.serverVersion,
          isWritable: this.CollectionStore.isWritable()
        })
      ) : React.createElement(JSONView, {
        validatorDoc: this.props.validatorDoc,
        validationAction: this.props.validationAction,
        validationLevel: this.props.validationLevel,
        editState: this.props.editState,
        isWritable: this.CollectionStore.isWritable()
      });

      return React.createElement(
        'div',
        { className: 'column-container' },
        React.createElement(
          'div',
          { className: 'column main' },
          view
        )
      );
    }
  }, {
    key: 'renderReadonly',
    value: function renderReadonly() {
      return React.createElement(
        StatusRow,
        { style: 'warning' },
        'Document validation rules may not be added to readonly views.'
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var activeButton = this.props.isExpressibleByRules ? this.props.viewMode : 'JSON';
      return React.createElement(
        'div',
        { className: 'validation-container' },
        React.createElement(
          'div',
          { className: 'controls-container' },
          React.createElement(
            ValidationStatusRow,
            null,
            React.createElement(ViewSwitcher, {
              label: 'View as:',
              buttonLabels: ['Rule Builder', 'JSON'],
              activeButton: activeButton,
              onClick: this.switchView.bind(this),
              disabled: !this.props.isExpressibleByRules
            })
          )
        ),
        this.CollectionStore.isReadonly() ? this.renderReadonly() : this.renderComponent()
      );
    }
  }]);

  return Validation;
}(React.Component);

Validation.propTypes = {
  editState: PropTypes.oneOf(['unmodified', 'modified', 'updating', 'error', 'success']).isRequired,
  viewMode: PropTypes.oneOf(['Rule Builder', 'JSON']).isRequired,
  isExpressibleByRules: PropTypes.bool.isRequired,
  validationAction: PropTypes.oneOf(['warn', 'error']).isRequired,
  validatorDoc: PropTypes.object.isRequired,
  validationLevel: PropTypes.oneOf(['off', 'moderate', 'strict']).isRequired,
  validationRules: PropTypes.array.isRequired,
  serverVersion: PropTypes.string
};

Validation.defaultProps = {
  editState: 'unmodified',
  viewMode: 'Rule Builder',
  isExpressibleByRules: true,
  validationAction: 'warn',
  validatorDoc: {},
  validationLevel: 'off',
  validationRules: []
};

Validation.displayName = 'Validation';

module.exports = Validation;