'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 isEmpty = require('lodash.isempty');

var _require = require('hadron-react-components'),
    FormInput = _require.FormInput;

var _require2 = require('electron'),
    shell = _require2.shell;

var CnameInput = require('./cname-input');
var Actions = require('../actions');

/**
 * The kerberos auth role component.
 */

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

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

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

  _createClass(Kerberos, [{
    key: 'onPrincipalChanged',


    /**
     * Handle the principal change.
     *
     * @param {Event} evt - The event.
     */
    value: function onPrincipalChanged(evt) {
      Actions.onKerberosPrincipalChanged(evt.target.value);
    }

    /**
     * Handle the password change.
     *
     * @param {Event} evt - The event.
     */

  }, {
    key: 'onPasswordChanged',
    value: function onPasswordChanged(evt) {
      Actions.onKerberosPasswordChanged(evt.target.value);
    }

    /**
     * Handle the service name change.
     *
     * @param {Event} evt - The event.
     */

  }, {
    key: 'onServiceNameChanged',
    value: function onServiceNameChanged(evt) {
      Actions.onKerberosServiceNameChanged(evt.target.value);
    }

    /**
     * Open the help page for the principal.
     */

  }, {
    key: 'onPrincipalHelp',
    value: function onPrincipalHelp() {
      shell.openExternal('https://docs.mongodb.com/manual/core/kerberos/#principals');
    }

    /**
     * Get the error for the required principal field.
     *
     * @returns {String} The error message.
     */

  }, {
    key: 'getPrincipalError',
    value: function getPrincipalError() {
      var connection = this.props.currentConnection;
      if (!this.props.isValid && isEmpty(connection.kerberos_principal)) {
        return 'Principal is required';
      }
    }

    /**
     * Render the kerberos component.
     *
     * @returns {React.Component} The component.
     */

  }, {
    key: 'render',
    value: function render() {
      return React.createElement(
        'div',
        { id: 'kerberos-authentication', className: 'form-group' },
        React.createElement(FormInput, {
          label: 'Principal',
          name: 'kerberos-principal',
          error: this.getPrincipalError(),
          changeHandler: this.onPrincipalChanged.bind(this),
          value: this.props.currentConnection.kerberos_principal || '',
          linkHandler: this.onPrincipalHelp.bind(this) }),
        React.createElement(FormInput, {
          label: 'Password',
          name: 'kerberos-password',
          type: 'password',
          changeHandler: this.onPasswordChanged.bind(this),
          value: this.props.currentConnection.kerberos_password || '' }),
        React.createElement(FormInput, {
          label: 'Service Name',
          placeholder: 'mongodb',
          name: 'kerberos-service-name',
          changeHandler: this.onServiceNameChanged.bind(this),
          value: this.props.currentConnection.kerberos_service_name || '' }),
        React.createElement(CnameInput, { canonicalize_hostname: this.props.currentConnection.kerberos_canonicalize_hostname })
      );
    }
  }]);

  return Kerberos;
}(React.Component);

Kerberos.propTypes = {
  currentConnection: PropTypes.object.isRequired,
  isValid: PropTypes.bool
};

Kerberos.displayName = 'Kerberos';

module.exports = Kerberos;