import "choerodon-ui/lib/icon/style";
import _Icon from "choerodon-ui/lib/icon";
import "choerodon-ui/lib/tooltip/style";
import _Tooltip from "choerodon-ui/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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

import React, { PureComponent, Fragment } from 'react';
import { FormattedMessage } from 'react-intl';
import classnames from 'classnames';
import _ from 'lodash';
import { timeConvert } from "../../../../../../utils";
import { STAGE_FLOW_MANUAL, STATUS_ICON } from "../Constants";
import "./index.less";

var DetailTitle = /*#__PURE__*/function (_PureComponent) {
  _inherits(DetailTitle, _PureComponent);

  var _super = _createSuper(DetailTitle);

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

    return _super.apply(this, arguments);
  }

  _createClass(DetailTitle, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          name = _this$props.name,
          time = _this$props.time,
          type = _this$props.type,
          user = _this$props.user,
          status = _this$props.status,
          checking = _this$props.checking,
          isCadence = _this$props.isCadence,
          head = _this$props.head,
          onlyOne = _this$props.onlyOne,
          tail = _this$props.tail;
      /**
       * 手动流转模式说明
       * 待流转，未开启审核： 所有 user 的 audit 属性为 false，checking 属性为 null
       * 待流转，已开启审核： 所有 user 的 audit 属性为 false，checking 属性为 true
       * 已审核，通过：             只要有 audit 属性为 true，status   属性为 success
       * 已审核，终止：             只要有 audit 属性为 true，status   属性为 stop
       */

      var triggerDom = null;

      if (type === STAGE_FLOW_MANUAL) {
        var audit = _.find(user, 'audit');

        if (audit) {
          var isStopFlow = status === 'stop' || isCadence && checking;
          var messageCode = isStopFlow ? 'stopped' : type;
          var spanClass = classnames({
            'c7ncd-manualflow-pass': !isStopFlow,
            'c7ncd-manualflow-stop': isStopFlow
          });
          var realName = audit.realName,
              imageUrl = audit.imageUrl,
              loginName = audit.loginName;
          triggerDom = /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(_Tooltip, {
            title: realName || loginName
          }, imageUrl ? /*#__PURE__*/React.createElement("img", {
            className: "c7ncd-trigger-img",
            src: imageUrl,
            alt: "avatar"
          }) : /*#__PURE__*/React.createElement("span", {
            className: "c7ncd-trigger-text"
          }, _.toString(realName || loginName).toUpperCase().substring(0, 1))), /*#__PURE__*/React.createElement("span", {
            className: spanClass
          }, /*#__PURE__*/React.createElement(FormattedMessage, {
            id: "pipeline.flow.".concat(messageCode)
          })));
        } else {
          var userName = _.map(user, function (_ref) {
            var realName = _ref.realName,
                loginName = _ref.loginName;
            return realName || loginName;
          }).join('，');

          var _spanClass = classnames({
            'c7ncd-manualflow-pending': !!checking
          });

          triggerDom = /*#__PURE__*/React.createElement(_Tooltip, {
            title: userName
          }, /*#__PURE__*/React.createElement("span", {
            className: _spanClass
          }, /*#__PURE__*/React.createElement(FormattedMessage, {
            id: "pipeline.flow.".concat(type)
          })));
        }
      } else {
        var _spanClass2 = classnames({
          'c7ncd-autoflow-pass': status === 'success'
        });

        triggerDom = /*#__PURE__*/React.createElement("span", {
          className: _spanClass2
        }, /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "pipeline.flow.".concat(type)
        }));
      }

      var statusStyle = classnames(_defineProperty({
        'c7ncd-pipeline-status': true
      }, "c7ncd-pipeline-status_".concat(status), true));
      var bkColor = classnames(_defineProperty({
        'c7ncd-pipeline-title': true,
        'c7ncd-pipeline-title-head': head,
        'c7ncd-pipeline-title-tail': onlyOne || tail
      }, "c7ncd-pipeline-title_".concat(status), true));
      var arrowDom = null;

      if (!onlyOne) {
        var arrowHeadClass = classnames(_defineProperty({
          'arrow-head': true
        }, "arrow-head_".concat(status), true));
        var arrowTailClass = classnames(_defineProperty({
          'arrow-tail': true
        }, "arrow-tail_".concat(status), true));
        arrowDom = /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement("div", {
          className: arrowHeadClass
        }), /*#__PURE__*/React.createElement("div", {
          className: arrowTailClass
        }));
      }

      return /*#__PURE__*/React.createElement("div", {
        className: bkColor
      }, arrowDom, /*#__PURE__*/React.createElement("div", {
        className: statusStyle
      }, /*#__PURE__*/React.createElement(_Icon, {
        className: "stage-icon",
        type: STATUS_ICON[status]
      })), /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pipeline-detail-execute"
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pipeline-execute-name"
      }, name), /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pipeline-execute-time"
      }, timeConvert(Number(time)))), !tail && /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pipeline-title-trigger"
      }, triggerDom), /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pipeline-title-right-angle"
      }));
    }
  }]);

  return DetailTitle;
}(PureComponent);

DetailTitle.defaultProps = {
  head: false,
  onlyOne: false,
  tail: false
};
export { DetailTitle as default };