import "choerodon-ui/pro/lib/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import React, { memo } from 'react';
import { FormattedMessage } from 'react-intl';
import classnames from 'classnames';
import "./index.less";
export var statusMappings = {
  STOPPED: 'stopped',
  OPERATING: 'operating',
  FAILED: 'failed',
  RUNNING: 'running',
  DISCONNECTED: 'disconnect'
};
/**
 * @param connect
 * @param synchronize
 * @param active
 * @param failed
 * @returns {string}
 *
 * 四个字段分成三个层次，组合判断状态
 *
 *   - synchronize 与 failed 是集群创建层面：[false, -] 处理中，[true, true] 创建成功，[true, false] 创建失败
 *   - connect 环境与集群连接层面
 *   - active 环境本身层面，表示环境自身停启用
 *
 * 判断逻辑
 *
 *   1. 是否在处理中（创建中，删除中，变更中）
 *   2. 是否创建失败
 *   3. 是否为停用 （停用的环境不用管连接未连接）
 *   4. 是否已连接集群
 *
 */

export function getEnvStatus(_ref) {
  var connect = _ref.connect,
      synchronize = _ref.synchronize,
      active = _ref.active,
      failed = _ref.failed;

  if (!synchronize) {
    return statusMappings.OPERATING;
  } else if (failed) {
    return statusMappings.FAILED;
  } else if (!active) {
    return statusMappings.STOPPED;
  } else if (connect) {
    return statusMappings.RUNNING;
  } else {
    return statusMappings.DISCONNECTED;
  }
}
var StatusDot = /*#__PURE__*/memo(function (_ref2) {
  var _classnames;

  var connect = _ref2.connect,
      synchronize = _ref2.synchronize,
      active = _ref2.active,
      size = _ref2.size,
      getStatus = _ref2.getStatus,
      failed = _ref2.failed;
  var status;
  var text;

  if (getStatus && typeof getStatus === 'function') {
    var _getStatus = getStatus();

    var _getStatus2 = _slicedToArray(_getStatus, 2);

    status = _getStatus2[0];
    text = _getStatus2[1];
  } else {
    status = getEnvStatus({
      connect: connect,
      synchronize: synchronize,
      active: active,
      failed: failed
    });
  }

  var styled = classnames((_classnames = {
    'c7ncd-env-status': true
  }, _defineProperty(_classnames, "c7ncd-env-status-".concat(status), true), _defineProperty(_classnames, "c7ncd-env-status-".concat(size), true), _classnames));
  var dot = /*#__PURE__*/React.createElement("i", {
    className: styled
  });
  return status ? /*#__PURE__*/React.createElement(_Tooltip, {
    placement: "top",
    title: /*#__PURE__*/React.createElement(FormattedMessage, {
      id: text || status
    })
  }, dot) : dot;
});
StatusDot.defaultProps = {
  size: 'normal',
  active: true,
  failed: false
};
export default StatusDot;