'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 Actions = require('../actions');

/**
 * The LDAP auth role component.
 */

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

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

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

  _createClass(LDAP, [{
    key: 'onUsernameChanged',


    /**
     * Handle the username change.
     *
     * @param {Event} evt - The event.
     */
    value: function onUsernameChanged(evt) {
      Actions.onLDAPUsernameChanged(evt.target.value);
    }

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

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

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

  }, {
    key: 'onLDAPHelp',
    value: function onLDAPHelp() {
      shell.openExternal('https://docs.mongodb.com/manual/core/security-ldap/');
    }

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

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

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

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

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

  }, {
    key: 'render',
    value: function render() {
      return React.createElement(
        'div',
        { id: 'ldap-authentication', className: 'form-group' },
        React.createElement(FormInput, {
          label: 'Username',
          name: 'ldap-username',
          error: this.getUsernameError(),
          changeHandler: this.onUsernameChanged.bind(this),
          value: this.props.currentConnection.ldap_username || '',
          linkHandler: this.onLDAPHelp.bind(this) }),
        React.createElement(FormInput, {
          label: 'Password',
          name: 'ldap-password',
          type: 'password',
          error: this.getPasswordError(),
          changeHandler: this.onPasswordChanged.bind(this),
          value: this.props.currentConnection.ldap_password || '' })
      );
    }
  }]);

  return LDAP;
}(React.Component);

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

LDAP.displayName = 'LDAP';

module.exports = LDAP;