'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 _require = require('react-bootstrap'),
    Popover = _require.Popover,
    OverlayTrigger = _require.OverlayTrigger,
    Button = _require.Button;

var DeploymentAwarenessActions = require('../actions');

var BASE_CLASS = 'deploymeny-awareness';
var SERVER_CLASS = BASE_CLASS + '-server';
var SET_NAME_CLASS = BASE_CLASS + '-set-name';
var BUTTON_CLASS = BASE_CLASS + '-button';
var ADDRESS_CLASS = SERVER_CLASS + '-address';
var SERVERS_ID = BASE_CLASS + '-servers';

/**
 * Topology types to class name mappings.
 */
var TOPOLOGY_TYPES = {
  'Standalone': 'standalone',
  'ReplicaSetNoPrimary': 'replica-set',
  'ReplicaSetWithPrimary': 'replica-set',
  'Sharded': 'cluster',
  'Unknown': 'unknown'
};

/**
 * Server types to class name mappings.
 */
var SERVER_TYPES = {
  'Standalone': 'primary',
  'Mongos': 'primary',
  'PossiblePrimary': 'primary',
  'RSPrimary': 'primary',
  'RSSecondary': 'secondary',
  'RSArbiter': 'arbiter',
  'RSOther': 'nostate',
  'RSGhost': 'nostate',
  'Unknown': 'nostate'
};

/**
 * <i class="mms-icon-leaf"> For version.
 */

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

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

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

  _createClass(DeploymentAwarenessComponent, [{
    key: 'renderServers',


    /**
     * Render the servers that are connected to.
     *
     * @returns {React.Component} The rendered component.
     */
    value: function renderServers() {
      var servers = this.props.servers.map(function (server, i) {
        return React.createElement(
          'div',
          { className: SERVER_CLASS, key: i },
          React.createElement(
            'span',
            null,
            React.createElement('i', { className: 'mms-icon-' + SERVER_TYPES[server.type] })
          ),
          React.createElement(
            'span',
            { className: ADDRESS_CLASS },
            server.address
          )
        );
      });
      return React.createElement(
        Popover,
        { id: SERVERS_ID },
        servers
      );
    }

    /**
     * Render the replica set name.
     *
     * @returns {React.Component} The rendered component.
     */

  }, {
    key: 'renderSetName',
    value: function renderSetName() {
      if (this.props.setName) {
        return React.createElement(
          'span',
          { className: SET_NAME_CLASS },
          this.props.setName
        );
      }
    }

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

  }, {
    key: 'render',
    value: function render() {
      return React.createElement(
        'div',
        { className: BASE_CLASS },
        React.createElement(
          OverlayTrigger,
          { trigger: 'click', placement: 'bottom', overlay: this.renderServers() },
          React.createElement(
            Button,
            { className: BUTTON_CLASS },
            React.createElement('i', { className: 'mms-icon-' + TOPOLOGY_TYPES[this.props.topologyType] }),
            this.renderSetName()
          )
        )
      );
    }
  }]);

  return DeploymentAwarenessComponent;
}(React.Component);

DeploymentAwarenessComponent.displayName = 'DeploymentAwarenessComponent';

module.exports = DeploymentAwarenessComponent;