'use strict';

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

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../tslib.es6-0d92ef81.js');
var time_time = require('../time/time.js');
var wecomRobot_batchSend = require('../wecom-robot/batch-send.js');
var rainbowHistoryMode_api = require('./api.js');
require('../wecom-robot/base.js');
require('../wecom-robot/helper.js');
require('axios');
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');
require('@babel/runtime/helpers/defineProperty');
require('@babel/runtime/helpers/toConsumableArray');
require('../rainbow-to-cos/api.js');
require('../rainbow/rainbow-admin.js');
require('../rainbow/helper/helper.js');
require('../rainbow/helper/rainbow-base-request.js');
require('../rainbow/helper/rainbow-signature.js');
require('../rainbow/rainbow-user.js');
require('../rainbow/sdk.js');
require('../json/json-parse.js');
require('../list/flat.js');
require('../rainbow-to-cos/helper/helper.js');
require('../tencent-cloud/cos/link.js');
require('../rainbow-to-cos/helper/value-type.js');
require('../string/string.js');
require('../fs/fs-util.js');
require('../fs/fs.js');
require('../nodejs/os.js');

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

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

function getHistoryModeConfigDiffAndSendRobot(_ref) {
  var secretInfo = _ref.secretInfo,
    appName = _ref.appName,
    key = _ref.key,
    chatId = _ref.chatId,
    webhookUrl = _ref.webhookUrl,
    mentions = _ref.mentions,
    fetchRainbowConfigOptions = _ref.fetchRainbowConfigOptions,
    _ref$heartbeat = _ref.heartbeat,
    heartbeat = _ref$heartbeat === void 0 ? false : _ref$heartbeat;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var res, equal, addedMap, deletedMap, parsed, message;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 1;
          return rainbowHistoryMode_api.getHistoryModeConfigDiff({
            secretInfo: secretInfo,
            appName: appName,
            key: key,
            fetchRainbowConfigOptions: fetchRainbowConfigOptions
          });
        case 1:
          res = _context.sent;
          equal = res.equal, addedMap = res.addedMap, deletedMap = res.deletedMap, parsed = res.parsed;
          if (!(equal && !heartbeat)) {
            _context.next = 2;
            break;
          }
          return _context.abrupt("return", {
            message: '',
            equal: equal,
            addedMap: addedMap,
            deletedMap: deletedMap
          });
        case 2:
          message = '';
          if (heartbeat) {
            message = getHeartbeatMessage({
              parsed: parsed,
              mentions: mentions
            });
          } else {
            message = genMessage({
              addedMap: addedMap,
              deletedMap: deletedMap,
              mentions: mentions,
              parsed: parsed
            });
          }
          try {
            if (chatId && webhookUrl) {
              wecomRobot_batchSend.batchSendWxRobotMarkdown({
                chatId: chatId,
                content: message,
                webhookUrl: webhookUrl
              });
            }
          } catch (err) {
            console.log('[sendRainbowInfoToRobot] err: \n', err);
          }
          return _context.abrupt("return", {
            message: message,
            equal: equal,
            addedMap: addedMap,
            deletedMap: deletedMap
          });
        case 3:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
function getHeartbeatMessage(_ref2) {
  var parsed = _ref2.parsed,
    mentions = _ref2.mentions;
  var _a;
  var mentionStr = ((_a = mentions === null || mentions === void 0 ? void 0 : mentions.map(function (item) {
    return "<@".concat(item, ">");
  })) === null || _a === void 0 ? void 0 : _a.join('')) || '';
  var result = ["\u3010History \u6A21\u5F0F\u5B50\u5DE5\u7A0B\u7EDF\u8BA1\u3011".concat(time_time.timeStampFormat(new Date().getTime(), 'yyyy-MM-dd')).concat(mentionStr)];
  result.push("- ".concat(getStatisticsMessage({
    parsed: parsed
  })));
  return result.join('\n');
}
function getStatisticsMessage(_ref3) {
  var parsed = _ref3.parsed;
  var validDomainLength = Object.keys(parsed).filter(function (domain) {
    return parsed[domain].length;
  }).length;
  var totalSubProjects = Object.keys(parsed).reduce(function (acc, item) {
    return acc + (parsed[item].length || 0);
  }, 0);
  return "\u5F53\u524D\u4F7F\u7528`history`\u6A21\u5F0F\u7684\u57DF\u540D\u5171`".concat(validDomainLength, "`\u4E2A\uFF0C\u5B50\u5DE5\u7A0B\u5171`").concat(totalSubProjects, "`\u4E2A");
}
function genMessage(_ref4) {
  var addedMap = _ref4.addedMap,
    deletedMap = _ref4.deletedMap,
    mentions = _ref4.mentions,
    parsed = _ref4.parsed;
  var _a;
  var mentionStr = ((_a = mentions === null || mentions === void 0 ? void 0 : mentions.map(function (item) {
    return "<@".concat(item, ">");
  })) === null || _a === void 0 ? void 0 : _a.join('')) || '';
  var result = ["> \u26A0\uFE0F \u3010History \u6A21\u5F0F\u5B50\u5DE5\u7A0B\u66F4\u65B0\u3011".concat(mentionStr)];
  Object.keys(addedMap).forEach(function (domain) {
    var list = addedMap[domain].map(function (item) {
      return "`".concat(item, "`");
    });
    if (list.length) {
      result.push("- `".concat(domain, "`\u65B0\u589E: ").concat(list.join('、')));
    }
  });
  Object.keys(deletedMap).forEach(function (domain) {
    var list = deletedMap[domain].map(function (item) {
      return "`".concat(item, "`");
    });
    if (list.length) {
      result.push("- `".concat(domain, "`\u5220\u9664: ").concat(list.join('、')));
    }
  });
  result.push(getStatisticsMessage({
    parsed: parsed
  }));
  return result.join('\n');
}

exports.getHistoryModeConfigDiffAndSendRobot = getHistoryModeConfigDiffAndSendRobot;
