import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import { timeStampFormat } from '../time/time.mjs';
import { batchSendWxRobotMarkdown } from '../wecom-robot/batch-send.mjs';
import { getHistoryModeConfigDiff } from './api.mjs';
import '../wecom-robot/base.mjs';
import '../wecom-robot/helper.mjs';
import 'axios';
import '../wecom-robot/send-img.mjs';
import '../node-img/img.mjs';
import '../nodejs/crypto.mjs';
import '../nodejs/fs.mjs';
import '../nodejs/request.mjs';
import '../nodejs/path.mjs';
import '@babel/runtime/helpers/defineProperty';
import '@babel/runtime/helpers/toConsumableArray';
import '../rainbow-to-cos/api.mjs';
import '../fs/fs-util.mjs';
import '../fs/fs.mjs';
import '../nodejs/os.mjs';
import '../rainbow/rainbow-admin.mjs';
import '../rainbow/helper/helper.mjs';
import '../rainbow/helper/rainbow-base-request.mjs';
import '../rainbow/helper/rainbow-signature.mjs';
import '../rainbow/rainbow-user.mjs';
import '../list/flat.mjs';
import '@babel/runtime/helpers/typeof';
import '../rainbow/sdk.mjs';
import '../json/json-parse.mjs';
import '../rainbow-to-cos/helper/helper.mjs';
import '../string/string.mjs';
import '../tencent-cloud/cos/link.mjs';
import '../rainbow-to-cos/helper/value-type.mjs';

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 __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var res, equal, addedMap, deletedMap, parsed, message;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 1;
          return 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) {
              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(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');
}

export { getHistoryModeConfigDiffAndSendRobot };
