'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');

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

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

var DISABLE_STATUS = [100, -1];
function isWhiteProject(whiteList) {
  var codeUrl = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  var projectName = getWholeProjectName(codeUrl);
  if (whiteList.includes(projectName)) return true;
  return false;
}
function getWholeProjectName() {
  var codeUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var reg = /https?:\/\/[\w\-.]+\/([/\w-]+)(.git)?/;
  var match = codeUrl.match(reg);
  return (match === null || match === void 0 ? void 0 : match[1]) || codeUrl;
}
function parseOpenSourceReport(_ref) {
  var reportArr = _ref.reportArr,
    date = _ref.date,
    formattedDate = _ref.formattedDate,
    searchInfo = _ref.searchInfo,
    requestInfo = _ref.requestInfo,
    _ref$maxShowLinkNum = _ref.maxShowLinkNum,
    maxShowLinkNum = _ref$maxShowLinkNum === void 0 ? 0 : _ref$maxShowLinkNum,
    _ref$whiteList = _ref.whiteList,
    whiteList = _ref$whiteList === void 0 ? [] : _ref$whiteList,
    _ref$filterOrgPath = _ref.filterOrgPath,
    filterOrgPath = _ref$filterOrgPath === void 0 ? '' : _ref$filterOrgPath;
  var problemArr = reportArr.filter(function (item) {
    return !DISABLE_STATUS.includes(item.code_specification_score) || !DISABLE_STATUS.includes(item.code_security_score);
  }).filter(function (item) {
    return !isWhiteProject(whiteList, item.code_url);
  }).filter(function (item) {
    return !filterOrgPath || filterOrgPath === item.org_path;
  }).sort(function (a, b) {
    var aSpec = a.code_specification_score;
    var aSecurity = a.code_security_score;
    var bSpec = b.code_specification_score;
    var bSecurity = b.code_security_score;
    var getScore = function getScore(score) {
      return DISABLE_STATUS.indexOf(score) > -1 ? 100 : score;
    };
    return getScore(aSpec) + getScore(aSecurity) - (getScore(bSpec) + getScore(bSecurity));
  });
  if (!problemArr.length) {
    console.log('[parseOpenSourceReport] Error: 没有开源治理问题数据');
    // 没问题时也返回数据
    // return;
  }
  var list = problemArr.reduce(function (acc, item, index) {
    var temp = [];
    if (!DISABLE_STATUS.includes(item.code_specification_score)) {
      temp.push("\u89C4\u8303: ".concat(parseInt("".concat(item.code_specification_score), 10), "\u5206"));
    }
    if (!DISABLE_STATUS.includes(item.code_security_score)) {
      temp.push("\u5B89\u5168: ".concat(parseInt("".concat(item.code_security_score), 10), "\u5206"));
    }
    var _item$owners = item.owners,
      owners = _item$owners === void 0 ? '' : _item$owners;
    temp.push(owners.split(';').map(function (item) {
      return "<@".concat(item, ">");
    }));
    var showProject = "[".concat(item.project_name, "](").concat(item.codecc_url, ")");
    if (index < maxShowLinkNum) {
      acc.push("".concat(index + 1, ". ").concat(showProject, ": ").concat(temp.join(', ')));
    } else if (index === maxShowLinkNum) {
      acc.push("... \u5DF2\u7701\u7565".concat(problemArr.length - maxShowLinkNum, "\u6761 ..."));
    } else ;
    return acc;
  }, []);
  if (!reportArr.length && !list.length) {
    list.push('抱歉，未拉取到数据～');
  } else if (!list.length) {
    list.push('恭喜，暂未发现问题，请继续保持 🚀');
  }
  var chatContent = [">\u3010".concat((requestInfo === null || requestInfo === void 0 ? void 0 : requestInfo.groupName) || (requestInfo === null || requestInfo === void 0 ? void 0 : requestInfo.centerName) || '', "\u5F00\u6E90\u6CBB\u7406\u95EE\u9898\u3011[").concat(formattedDate, "](").concat(getTechMapWebsiteUrl(date, searchInfo), ")")].concat(_toConsumableArray__default["default"](list));
  return chatContent.join('\n');
}
function getTechMapWebsiteUrl(date) {
  var searchInfo = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
    prefix: ''
  };
  var data = Object.assign({
    org: 0,
    step: 'days',
    period_type: 'date',
    data_date: "".concat(date),
    displayGroup: ['sum'],
    compare: false,
    sort_column: 'code_specification_score',
    sort_direction: 'asc'
  }, searchInfo || {});
  return "".concat(searchInfo.prefix).concat(encodeURIComponent(JSON.stringify(data)));
}

exports.parseOpenSourceReport = parseOpenSourceReport;
