'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 rainbowToCos_helper_helper = require('./helper/helper.js');
var list_flat = require('../list/flat.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('../tencent-cloud/cos/link.js');
require('./helper/value-type.js');
require('../string/string.js');
require('../fs/fs-util.js');
require('@babel/runtime/helpers/toConsumableArray');
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 isEqual(a, b) {
  var wrapA = a;
  var wrapB = b;
  if (typeof a !== 'string') wrapA = JSON.stringify(a);
  if (typeof a !== 'string') wrapB = JSON.stringify(b);
  return wrapA === wrapB;
}
function getAllCOSFiles(_ref) {
  var flatNow = _ref.flatNow,
    appName = _ref.appName,
    secretInfo = _ref.secretInfo,
    cosInfo = _ref.cosInfo;
  var _ref2 = secretInfo || {},
    groupName = _ref2.groupName,
    envName = _ref2.envName;
  var notifyList = [">\u3010\u5F53\u524D\u4E03\u5F69\u77F3\u914D\u7F6E\u3011".concat(appName, "\xB7").concat(groupName, "\xB7").concat(envName)];
  var keys = Object.keys(flatNow);
  for (var i = 0; i < keys.length; i++) {
    var key = keys[i];
    var params = {
      appName: appName,
      groupName: groupName,
      envName: envName,
      key: key,
      valueType: flatNow[key].valueType,
      cosInfo: cosInfo
    };
    notifyList.push("".concat(i + 1, ". [").concat(key, "](").concat(rainbowToCos_helper_helper.getCOSFilePath(params), ")"));
  }
  return notifyList;
}
function getOnlyChangedCOSFiles(_ref3) {
  var flatNow = _ref3.flatNow,
    flatOrigin = _ref3.flatOrigin,
    appName = _ref3.appName,
    secretInfo = _ref3.secretInfo,
    cosInfo = _ref3.cosInfo;
  var _ref4 = secretInfo || {},
    groupName = _ref4.groupName,
    envName = _ref4.envName;
  var notifyList = [];
  var addList = [];
  var updateList = [];
  var deleteList = [];
  Object.keys(flatNow).forEach(function (key) {
    var params = {
      appName: appName,
      groupName: groupName,
      envName: envName,
      key: key,
      valueType: flatNow[key].valueType,
      cosInfo: cosInfo
    };
    if (!flatOrigin[key]) {
      addList.push("[".concat(key, "](").concat(rainbowToCos_helper_helper.getCOSFilePath(params), ")"));
    } else if (!isEqual(flatOrigin[key].value, flatNow[key].value)) {
      updateList.push("[".concat(key, "](").concat(rainbowToCos_helper_helper.getCOSFilePath(params), ")"));
    }
  });
  Object.keys(flatOrigin).forEach(function (key) {
    if (!flatNow[key]) {
      var params = {
        appName: appName,
        groupName: groupName,
        envName: envName,
        key: key,
        valueType: flatOrigin[key].valueType,
        cosInfo: cosInfo
      };
      deleteList.push("[".concat(key, "](").concat(rainbowToCos_helper_helper.getCOSFilePath(params), ")"));
    }
  });
  if (addList.length) {
    notifyList.push("\u65B0\u589E\u4E86\uFF1A".concat(addList.join('，')));
  }
  if (updateList.length) {
    notifyList.push("\u66F4\u65B0\u4E86\uFF1A".concat(updateList.join('，')));
  }
  if (deleteList.length) {
    notifyList.push("\u5220\u9664\u4E86\uFF1A".concat(deleteList.join('，')));
  }
  if (!notifyList.length) {
    return [];
  }
  notifyList = notifyList.map(function (value, idx) {
    return "".concat(idx + 1, ". ").concat(value);
  });
  notifyList.unshift(">\u3010\u4E03\u5F69\u77F3\u914D\u7F6E\u66F4\u65B0\u3011".concat(appName, "\xB7").concat(groupName, "\xB7").concat(envName));
  notifyList.push("".concat(time_time.timeStampFormat(Date.now(), 'yyyy-MM-dd: hh:mm:ss')));
  return notifyList;
}
function genRobotMsg(_ref5) {
  var newConfig = _ref5.newConfig,
    originConfig = _ref5.originConfig,
    secretInfo = _ref5.secretInfo,
    appName = _ref5.appName,
    cosInfo = _ref5.cosInfo,
    _ref5$isGenAll = _ref5.isGenAll,
    isGenAll = _ref5$isGenAll === void 0 ? false : _ref5$isGenAll;
  var flatNow = list_flat.flatten(newConfig, 'key');
  var flatOrigin = list_flat.flatten(originConfig, 'key');
  var notifyList = [];
  if (isGenAll) {
    notifyList = getAllCOSFiles({
      flatNow: flatNow,
      appName: appName,
      secretInfo: secretInfo,
      cosInfo: cosInfo
    });
  } else {
    notifyList = getOnlyChangedCOSFiles({
      flatNow: flatNow,
      flatOrigin: flatOrigin,
      appName: appName,
      secretInfo: secretInfo,
      cosInfo: cosInfo
    });
  }
  return notifyList.join('\n');
}
function sendRainbowInfoToRobot(_ref6) {
  var newConfig = _ref6.newConfig,
    originConfig = _ref6.originConfig,
    secretInfo = _ref6.secretInfo,
    cosInfo = _ref6.cosInfo,
    appName = _ref6.appName,
    _ref6$isGenAll = _ref6.isGenAll,
    isGenAll = _ref6$isGenAll === void 0 ? false : _ref6$isGenAll,
    chatId = _ref6.chatId,
    webhookUrl = _ref6.webhookUrl;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var chatContent, _t;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          chatContent = genRobotMsg({
            newConfig: newConfig,
            originConfig: originConfig,
            secretInfo: secretInfo,
            appName: appName,
            cosInfo: cosInfo,
            isGenAll: isGenAll
          });
          if (chatContent) {
            _context.next = 1;
            break;
          }
          console.log('[sendRainbowInfoToRobot] Error: chatContent 为空');
          return _context.abrupt("return");
        case 1:
          _context.prev = 1;
          _context.next = 2;
          return wecomRobot_batchSend.batchSendWxRobotMarkdown({
            chatId: chatId,
            content: chatContent,
            webhookUrl: webhookUrl
          });
        case 2:
          _context.next = 4;
          break;
        case 3:
          _context.prev = 3;
          _t = _context["catch"](1);
          console.log('[sendRainbowInfoToRobot] err: \n', _t);
        case 4:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[1, 3]]);
  }));
}

exports.sendRainbowInfoToRobot = sendRainbowInfoToRobot;
