import "choerodon-ui/pro/lib/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";
import "choerodon-ui/pro/lib/button/style";
import _Button from "choerodon-ui/pro/lib/button";
import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";

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, { Fragment, useMemo } from 'react';
import { FormattedMessage } from 'react-intl';
import { Permission } from '@choerodon/boot';
import { observer } from 'mobx-react-lite';
import { useREStore } from "../stores";
import { useResourceStore } from "../../../../stores";
import Tips from "../../../../../../components/new-tips";
var SyncSituation = observer(function () {
  var _useResourceStore = useResourceStore(),
      prefixCls = _useResourceStore.prefixCls,
      intlPrefix = _useResourceStore.intlPrefix,
      treeDs = _useResourceStore.treeDs;

  var _useREStore = useREStore(),
      formatMessage = _useREStore.intl.formatMessage,
      gitopsLogDs = _useREStore.gitopsLogDs,
      gitopsSyncDs = _useREStore.gitopsSyncDs,
      retryDs = _useREStore.retryDs;
  /**
   * 打开重试弹窗
   */


  function showRetry() {
    _Modal.open({
      key: 'retry',
      title: formatMessage({
        id: "".concat(intlPrefix, ".environment.retry")
      }),
      children: /*#__PURE__*/React.createElement("span", null, formatMessage({
        id: "".concat(intlPrefix, ".environment.retry.des")
      })),
      onOk: handleRetry
    });
  }

  function refresh() {
    treeDs.query();
    gitopsSyncDs.query();
    gitopsLogDs.query();
  }
  /**
   * 重试gitOps
   */


  function handleRetry() {
    return _handleRetry.apply(this, arguments);
  }

  function _handleRetry() {
    _handleRetry = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.prev = 0;
              _context.next = 3;
              return retryDs.query();

            case 3:
              _context.t0 = _context.sent;

              if (!(_context.t0 !== false)) {
                _context.next = 8;
                break;
              }

              refresh();
              _context.next = 9;
              break;

            case 8:
              return _context.abrupt("return", false);

            case 9:
              _context.next = 14;
              break;

            case 11:
              _context.prev = 11;
              _context.t1 = _context["catch"](0);
              return _context.abrupt("return", false);

            case 14:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[0, 11]]);
    }));
    return _handleRetry.apply(this, arguments);
  }

  var content = useMemo(function () {
    return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement("p", {
      className: "log-help-desc"
    }, /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "".concat(intlPrefix, ".environment.help")
    })), /*#__PURE__*/React.createElement("h4", {
      className: "log-help-title"
    }, /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "".concat(intlPrefix, ".environment.config")
    })), /*#__PURE__*/React.createElement("p", {
      className: "log-help-desc"
    }, /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "".concat(intlPrefix, ".environment.config.des")
    })), /*#__PURE__*/React.createElement("h4", {
      className: "log-help-title"
    }, /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "".concat(intlPrefix, ".environment.parsed")
    })), /*#__PURE__*/React.createElement("p", {
      className: "log-help-desc"
    }, /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "".concat(intlPrefix, ".environment.parsed.des")
    })), /*#__PURE__*/React.createElement("h4", {
      className: "log-help-title"
    }, /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "".concat(intlPrefix, ".environment.executed")
    })), /*#__PURE__*/React.createElement("p", {
      className: "log-help-desc"
    }, /*#__PURE__*/React.createElement(FormattedMessage, {
      id: "".concat(intlPrefix, ".environment.executed.des")
    })));
  }, []);
  var getDetail = useMemo(function () {
    var record = gitopsSyncDs.current;

    if (record) {
      var commitUrl = record.get('commitUrl');
      var sagaSyncCommit = record.get('sagaSyncCommit');
      var devopsSyncCommit = record.get('devopsSyncCommit');
      var agentSyncCommit = record.get('agentSyncCommit');
      return /*#__PURE__*/React.createElement("div", {
        className: "log-sync-line"
      }, /*#__PURE__*/React.createElement("div", {
        className: "log-sync-line-card"
      }, /*#__PURE__*/React.createElement("div", {
        className: "log-sync-line-card-title"
      }, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "".concat(intlPrefix, ".environment.config-lab")
      })), /*#__PURE__*/React.createElement("div", {
        className: "log-sync-line-card-commit"
      }, /*#__PURE__*/React.createElement("a", {
        href: "".concat(commitUrl).concat(sagaSyncCommit),
        target: "_blank",
        rel: "nofollow me noopener noreferrer"
      }, sagaSyncCommit ? sagaSyncCommit.slice(0, 8) : null))), /*#__PURE__*/React.createElement("div", {
        className: "log-sync-line-arrow log-sync-line-retry"
      }, /*#__PURE__*/React.createElement(Permission, {
        service: []
      }, /*#__PURE__*/React.createElement(_Tooltip, {
        title: /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "".concat(intlPrefix, ".environment.retry")
        })
      }, /*#__PURE__*/React.createElement(_Button, {
        icon: "replay",
        color: "primary",
        funcType: "flat",
        onClick: showRetry
      }))), /*#__PURE__*/React.createElement("div", {
        className: "c7n-log-arrow-detail c7n-log-arrow-detail-absolute"
      }, "\u2192")), /*#__PURE__*/React.createElement("div", {
        className: "log-sync-line-card"
      }, /*#__PURE__*/React.createElement("div", {
        className: "log-sync-line-card-title"
      }, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "".concat(intlPrefix, ".environment.resolved")
      })), /*#__PURE__*/React.createElement("div", {
        className: "log-sync-line-card-commit"
      }, /*#__PURE__*/React.createElement("a", {
        href: "".concat(commitUrl).concat(devopsSyncCommit),
        target: "_blank",
        rel: "nofollow me noopener noreferrer"
      }, devopsSyncCommit ? devopsSyncCommit.slice(0, 8) : null))), /*#__PURE__*/React.createElement("div", {
        className: "log-sync-line-arrow"
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7n-log-arrow-detail"
      }, "\u2192")), /*#__PURE__*/React.createElement("div", {
        className: "log-sync-line-card"
      }, /*#__PURE__*/React.createElement("div", {
        className: "log-sync-line-card-title"
      }, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "".concat(intlPrefix, ".environment.executed")
      })), /*#__PURE__*/React.createElement("div", {
        className: "log-sync-line-card-commit"
      }, /*#__PURE__*/React.createElement("a", {
        href: "".concat(commitUrl).concat(agentSyncCommit),
        target: "_blank",
        rel: "nofollow me noopener noreferrer"
      }, agentSyncCommit ? agentSyncCommit.slice(0, 8) : null))));
    }

    return null;
  }, [gitopsSyncDs.data, showRetry]);
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-environment-sync-detail")
  }, /*#__PURE__*/React.createElement("div", {
    className: "log-sync-title"
  }, /*#__PURE__*/React.createElement(Tips, {
    helpText: content,
    title: formatMessage({
      id: "".concat(intlPrefix, ".environment.sync")
    }),
    popoverClassName: "".concat(prefixCls, "-environment-sync-help"),
    placement: "top"
  })), getDetail);
});
export default SyncSituation;