'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../tslib.es6-0d92ef81.js');
var auditor_buildAuditContent = require('./build-audit-content.js');
require('../rtx/mention.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);

var STATUS_MAP = {
  ABORT: 'ABORT',
  PROCESS: 'PROCESS'
};
/**
 * 统一审核结果检查
 *
 * 检查审核结果，通过则 resolve，驳回则发送企微通知并 reject。
 * 适用于 H5 发布、组件库发布等所有需要审核的流水线。
 *
 * @example H5 发布
 * ```ts
 * const { batchSendWxRobotMarkdown, checkAuditResult } = require('t-comm');
 *
 * await checkAuditResult({
 *   resultInfo,
 *   title: '【H5发布】',
 *   contentLines: [`项目: \`my-project\``, `子工程：\`my-sub\``],
 *   creator: 'novlan1',
 *   auditDesc: '需求发布',
 *   webhookUrl: '0482249e-bf24-4168-b3e2-f72d012840c2',
 *   sendMarkdown: batchSendWxRobotMarkdown,
 * });
 * ```
 */
function checkAuditResult(options) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var resultInfo, title, contentLines, creator, auditDesc, webhookUrl, sendMarkdown, isPass, realReviewer, symbol, content, _t;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          resultInfo = options.resultInfo, title = options.title, contentLines = options.contentLines, creator = options.creator, auditDesc = options.auditDesc, webhookUrl = options.webhookUrl, sendMarkdown = options.sendMarkdown;
          console.log('[checkAuditResult][resultInfo]', resultInfo);
          isPass = resultInfo.status === STATUS_MAP.PROCESS;
          realReviewer = auditor_buildAuditContent.getReviewerFromSuggest(resultInfo.suggest) || resultInfo.reviewer;
          symbol = isPass ? '✅' : '❌';
          if (!isPass) {
            _context.next = 1;
            break;
          }
          console.log('[checkAuditResult][PASS]');
          return _context.abrupt("return");
        case 1:
          content = ["".concat(symbol).concat(title, "\u5BA1\u6838\u7ED3\u679C\uFF1A`").concat(isPass ? '通过' : '驳回', "`"), "\u5BA1\u6838\u610F\u89C1\uFF1A`".concat(resultInfo.suggest || '无', "`"), "\u5BA1\u6838\u4EBA\uFF1A<@".concat(realReviewer, ">")].concat(_toConsumableArray__default["default"](contentLines), ["\u53D1\u5E03\u539F\u56E0\uFF1A`".concat(auditDesc, "`"), "\u53D1\u8D77\u4EBA\uFF1A<@".concat(creator, ">"), "[\u8BE6\u60C5](".concat(resultInfo.bkBuildUrl || '', ")")]).join('，');
          _context.prev = 2;
          _context.next = 3;
          return sendMarkdown({
            content: content,
            chatId: ['ALL'],
            webhookUrl: webhookUrl
          });
        case 3:
          _context.next = 5;
          break;
        case 4:
          _context.prev = 4;
          _t = _context["catch"](2);
          console.error('[checkAuditResult][sendMarkdown error]', _t);
        case 5:
          throw new Error('Audit Failed');
        case 6:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[2, 4]]);
  }));
}

exports.checkAuditResult = checkAuditResult;
