'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 lint_git = require('../lint/git.js');
var lint_message = require('../lint/message.js');
var tgit_helper = require('../tgit/helper.js');
var wecomRobot_batchSend = require('../wecom-robot/batch-send.js');
var ts_tsError = require('./ts-error.js');
require('axios');
require('../git/git.js');
require('../node/node-command.js');
require('@babel/runtime/helpers/typeof');
require('../nodejs/child_process.js');
require('../validate/validate.js');
require('../wecom-robot/base.js');
require('../wecom-robot/helper.js');
require('../wecom-robot/send-img.js');
require('../nodejs/path.js');
require('../node-img/img.js');
require('../nodejs/crypto.js');
require('../nodejs/fs.js');
require('../nodejs/request.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);

function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var genTitle = function genTitle(prefix, checkAll) {
  var name = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'LINT';
  return "".concat(prefix, "\u3010").concat(name, "\u3011");
};
function genRobotMessage(_ref) {
  var errorFiles = _ref.errorFiles,
    workspace = _ref.workspace,
    mrUrl = _ref.mrUrl,
    sourceBranch = _ref.sourceBranch,
    targetBranch = _ref.targetBranch,
    repo = _ref.repo,
    repoUrl = _ref.repoUrl,
    checkAll = _ref.checkAll,
    buildUrl = _ref.buildUrl,
    docLink = _ref.docLink,
    mentionList = _ref.mentionList,
    _ref$postFixList = _ref.postFixList,
    postFixList = _ref$postFixList === void 0 ? [] : _ref$postFixList;
  var reportInfo = lint_message.genReportInfo({
    workspace: workspace,
    mrUrl: mrUrl,
    sourceBranch: sourceBranch,
    targetBranch: targetBranch,
    repo: repo,
    repoUrl: repoUrl,
    checkAll: checkAll
  });
  if (!errorFiles.length) {
    return [genTitle('✅', checkAll, 'TS CHECK')].concat(_toConsumableArray__default["default"](reportInfo), ['未发现代码规范异常'], _toConsumableArray__default["default"](postFixList)).join('，');
  }
  return [[genTitle('⚠️', checkAll, 'TS CHECK')].concat(_toConsumableArray__default["default"](reportInfo), ["\u5171".concat(errorFiles.length, "\u4E2ATS\u7C7B\u578B\u9519\u8BEF"), "\u53EF\u5728[\u6D41\u6C34\u7EBF](".concat(buildUrl, ")\u4E2D\u67E5\u770B\u8BE6\u60C5\uFF0C\u6216\u672C\u5730\u8FD0\u884C `npx tsc --noEmit` \u7B49\u547D\u4EE4"), docLink ? "[\u8BF4\u660E\u6587\u6863](".concat(docLink, ")") : '', mentionList.length ? mentionList.map(function (mention) {
    return "<@".concat(mention, ">");
  }).join('') : ''], _toConsumableArray__default["default"](postFixList || [])).filter(function (item) {
    return item;
  }).join('，')].join('\n');
}
function checkTSErrorInMrOrAll(_ref2) {
  var privateToken = _ref2.privateToken,
    _ref2$gitApiPrefix = _ref2.gitApiPrefix,
    gitApiPrefix = _ref2$gitApiPrefix === void 0 ? tgit_helper.T_GIT_BASE_URL : _ref2$gitApiPrefix,
    workspace = _ref2.workspace,
    repo = _ref2.repo,
    repoUrl = _ref2.repoUrl,
    mrId = _ref2.mrId,
    mrUrl = _ref2.mrUrl,
    sourceBranch = _ref2.sourceBranch,
    targetBranch = _ref2.targetBranch,
    checkAll = _ref2.checkAll,
    buildUrl = _ref2.buildUrl,
    docLink = _ref2.docLink,
    mentionList = _ref2.mentionList,
    postFixList = _ref2.postFixList,
    _ref2$chatId = _ref2.chatId,
    chatId = _ref2$chatId === void 0 ? ['ALL'] : _ref2$chatId,
    webhookUrl = _ref2.webhookUrl,
    command = _ref2.command;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var errorFiles, _iterator, _step, file, robotMessage, _t, _t2, _t3;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          errorFiles = ts_tsError.getTSErrorFiles({
            root: workspace,
            command: command
          });
          if (!(mrId && !checkAll)) {
            _context.next = 7;
            break;
          }
          _iterator = _createForOfIteratorHelper(errorFiles);
          _context.prev = 1;
          _iterator.s();
        case 2:
          if ((_step = _iterator.n()).done) {
            _context.next = 4;
            break;
          }
          file = _step.value;
          _context.next = 3;
          return lint_git.createMRNote({
            privateToken: privateToken,
            gitApiPrefix: gitApiPrefix,
            projectName: repo,
            mrId: mrId,
            body: "".concat(file.code ? "".concat(file.code, " - ") : '').concat(file.message) || '格式错误',
            line: file.line,
            path: file.file
          });
        case 3:
          _context.next = 2;
          break;
        case 4:
          _context.next = 6;
          break;
        case 5:
          _context.prev = 5;
          _t = _context["catch"](1);
          _iterator.e(_t);
        case 6:
          _context.prev = 6;
          _iterator.f();
          return _context.finish(6);
        case 7:
          robotMessage = genRobotMessage({
            errorFiles: errorFiles,
            workspace: workspace,
            mrUrl: mrUrl,
            sourceBranch: sourceBranch,
            targetBranch: targetBranch,
            repo: repo,
            repoUrl: repoUrl,
            checkAll: checkAll,
            buildUrl: buildUrl,
            docLink: docLink,
            mentionList: mentionList,
            postFixList: postFixList
          });
          if (!(mrId && !checkAll)) {
            _context.next = 11;
            break;
          }
          _context.prev = 8;
          _context.next = 9;
          return lint_git.createMRComment({
            projectName: repo,
            mrId: mrId,
            data: robotMessage,
            privateToken: privateToken,
            gitApiPrefix: gitApiPrefix
          });
        case 9:
          _context.next = 11;
          break;
        case 10:
          _context.prev = 10;
          _t2 = _context["catch"](8);
          console.log('[createMRComment] err', _t2);
        case 11:
          _context.prev = 11;
          _context.next = 12;
          return wecomRobot_batchSend.batchSendWxRobotMarkdown({
            content: robotMessage,
            chatId: chatId,
            webhookUrl: webhookUrl
          });
        case 12:
          _context.next = 14;
          break;
        case 13:
          _context.prev = 13;
          _t3 = _context["catch"](11);
          console.log('[batchSendWxRobotMarkdown] err', _t3);
        case 14:
          return _context.abrupt("return", errorFiles);
        case 15:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[1, 5, 6, 7], [8, 10], [11, 13]]);
  }));
}

exports.checkTSErrorInMrOrAll = checkTSErrorInMrOrAll;
