import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
import "choerodon-ui/pro/lib/output/style";
import _Output from "choerodon-ui/pro/lib/output";
import "choerodon-ui/pro/lib/spin/style";
import _Spin from "choerodon-ui/pro/lib/spin";
import "choerodon-ui/pro/lib/icon/style";
import _Icon from "choerodon-ui/pro/lib/icon";

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

import React, { useMemo, Fragment } from 'react';
import { observer } from 'mobx-react-lite';
import { useRecordDetailStore } from "./stores";
import StatusTag from "../PipelineFlow/components/StatusTag";
import UserInfo from "../../../../components/userInfo";
import getDuration from "../../../../utils/getDuration";
import "./index.less";
export default observer(function () {
  var _useRecordDetailStore = useRecordDetailStore(),
      formatMessage = _useRecordDetailStore.intl.formatMessage,
      projectId = _useRecordDetailStore.AppState.currentMenuType.projectId,
      intlPrefix = _useRecordDetailStore.intlPrefix,
      prefixCls = _useRecordDetailStore.prefixCls,
      detailDs = _useRecordDetailStore.detailDs,
      store = _useRecordDetailStore.store;

  var record = useMemo(function () {
    return detailDs.current;
  }, [detailDs.current]);

  function renderUser() {
    var _ref = record.get('ciCdPipelineVO') || {},
        createUserUrl = _ref.createUserUrl,
        createUserName = _ref.createUserName;

    return /*#__PURE__*/React.createElement(UserInfo, {
      name: createUserName || '',
      avatar: createUserUrl
    });
  }

  function renderDuration() {
    var _ref2 = record.get('ciCdPipelineVO') || {},
        time = _ref2.time;

    return getDuration({
      value: time,
      unit: 's'
    });
  }

  function renderPipelineName() {
    var _ref3 = record.get('ciCdPipelineVO') || {},
        name = _ref3.name;

    return name;
  }

  function appServiceName() {
    var _ref4 = record.get('ciCdPipelineVO') || {},
        name = _ref4.appServiceName;

    return name;
  }

  function linkToGitlab(_x) {
    return _linkToGitlab.apply(this, arguments);
  }

  function _linkToGitlab() {
    _linkToGitlab = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(url) {
      var _ref9, appServiceId;

      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.prev = 0;
              _ref9 = record.get('ciCdPipelineVO') || {}, appServiceId = _ref9.appServiceId;
              _context.next = 4;
              return store.checkLinkToGitlab(projectId, appServiceId);

            case 4:
              window.open(url);
              _context.next = 9;
              break;

            case 7:
              _context.prev = 7;
              _context.t0 = _context["catch"](0);

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[0, 7]]);
    }));
    return _linkToGitlab.apply(this, arguments);
  }

  function renderCreateDate() {
    var _ref5 = record.get('ciCdPipelineVO') || {},
        latestExecuteDate = _ref5.latestExecuteDate;

    return latestExecuteDate;
  }

  function renderCommit(_ref6) {
    var value = _ref6.value;

    var _ref7 = value || {},
        commitContent = _ref7.commitContent,
        commitSha = _ref7.commitSha,
        commitUrl = _ref7.commitUrl,
        ref = _ref7.ref,
        userHeadUrl = _ref7.userHeadUrl,
        userName = _ref7.userName,
        gitlabProjectUrl = _ref7.gitlabProjectUrl;

    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefixCls, "-commit")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefixCls, "-commit-title")
    }, /*#__PURE__*/React.createElement(_Icon, {
      type: "branch",
      className: "".concat(prefixCls, "-commit-title-branch")
    }), /*#__PURE__*/React.createElement("span", {
      onClick: function onClick() {
        return linkToGitlab("".concat(gitlabProjectUrl, "/commits/").concat(ref));
      },
      className: "".concat(prefixCls, "-commit-title-ref")
    }, /*#__PURE__*/React.createElement("span", null, ref)), /*#__PURE__*/React.createElement(_Icon, {
      type: "point",
      className: "".concat(prefixCls, "-commit-title-point")
    }), /*#__PURE__*/React.createElement("span", {
      onClick: function onClick() {
        return linkToGitlab(commitUrl);
      },
      className: "".concat(prefixCls, "-commit-title-sha")
    }, /*#__PURE__*/React.createElement("span", null, commitSha ? commitSha.slice(0, 8) : null))), /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefixCls, "-commit-content")
    }, /*#__PURE__*/React.createElement(UserInfo, {
      name: userName || '?',
      avatar: userHeadUrl,
      showName: false
    }), /*#__PURE__*/React.createElement("span", {
      onClick: function onClick() {
        return linkToGitlab(commitUrl);
      },
      className: "".concat(prefixCls, "-commit-content-text")
    }, /*#__PURE__*/React.createElement("span", null, commitContent))));
  }

  if (!record) {
    return /*#__PURE__*/React.createElement(_Spin, null);
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls)
  }, /*#__PURE__*/React.createElement(_Form, {
    dataSet: detailDs,
    labelLayout: "horizontal",
    labelAlign: "left",
    labelWidth: 110
  }, /*#__PURE__*/React.createElement(_Output, {
    name: "pipelineName",
    renderer: renderPipelineName
  }), /*#__PURE__*/React.createElement(_Output, {
    name: "appServiceName",
    renderer: appServiceName
  }), /*#__PURE__*/React.createElement(_Output, {
    name: "status",
    renderer: function renderer(_ref8) {
      var value = _ref8.value;
      return /*#__PURE__*/React.createElement(StatusTag, {
        status: value,
        size: 12
      });
    }
  }), /*#__PURE__*/React.createElement(_Output, {
    name: "userDTO",
    renderer: renderUser
  }), /*#__PURE__*/React.createElement(_Output, {
    name: "createdDate",
    renderer: renderCreateDate
  }), /*#__PURE__*/React.createElement(_Output, {
    name: "durationSeconds",
    renderer: renderDuration
  }), /*#__PURE__*/React.createElement(_Output, {
    name: "commit",
    renderer: renderCommit
  })));
});