import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../../tslib.es6-848120af.js';
import '@babel/runtime/helpers/toConsumableArray';
import { compareTwoList, getMaxAndMinIdx } from '../../list/parse-list.mjs';
import { createCanvasTable } from '../../canvas/table.mjs';
import { timeStampFormat } from '../../time/time.mjs';
import '@babel/runtime/helpers/typeof';
import { getSomeDayStartTimeStamp, getSomeDayEndTimeStamp } from '../../date/timeStamp.mjs';
import { batchSendWxRobotBase64Img } from '../../wecom-robot/batch-send.mjs';
import { getMultiCustomEventData } from '../api/custom-event.mjs';
import { getTableHeaders } from '../parse/header.mjs';
import { parseMultiCustomEvent } from '../parse/custom-event.mjs';
import '../../number/number.mjs';
import '../../list/list.mjs';
import '../../canvas/get-canvas.mjs';
import '../../third-party/canvas.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 '../api/credential.mjs';
import '../../fs/fs-util.mjs';
import '../../fs/fs.mjs';
import '../../nodejs/os.mjs';

/**
 * 获取自定义事件图片
 * @ignore
 * @param {object} options 配置信息
 * @returns {string} 图片url
 * @example
 *
 * const requestMultiImgDate = Date.now() - 1 * 24 * 60 * 60 * 1000;
 *
 * const tamGroupIdList = [1, 2, 3];
 *
 * const eventProjectMap = {
 *   62659: {
 *     name: 'aaaaa',
 *   },
 *   57706: {
 *     name: 'bbbbb',
 *     extraProjectId: 66379,
 *   },
 * };
 *
 * const eventMap = {
 *   WX_SUC: {
 *     // 总和
 *     type: 'SUMMARY',
 *     target: ['ENTER_GAME_WX_SUC', 'LAUNCH_GAME_SUC_WX'],
 *   },
 *   WX_FAIL: {
 *     // 总和
 *     type: 'SUMMARY',
 *     target: ['ENTER_GAME_WX_FAIL', 'LAUNCH_GAME_FAIL_WX'],
 *   },
 * };
 *
 * const eventTableHeaderMap = {
 *   ProjectName: {
 *     name: '项目名称',
 *     tableWidth: 95,
 *   },
 *   ALL_SUMMARY: {
 *     name: '拉起总数',
 *     tableWidth: 65,
 *   },
 * };
 *
 * genCustomEventImg({
 *   date: requestLaunchGameDate,
 *   secretInfo: {
 *     getPwdCode,
 *     encrypt,
 *     apiKey: process.env.AEGIS_APP_KEY,
 *     loginName: 'lee',
 *   },
 *   projectIdMap: eventProjectMap,
 *   eventMap,
 *   tableHeaderMap: eventTableHeaderMap,
 * });
 *
 */
function genCustomEventImg(_ref) {
  var date = _ref.date,
    projectIdMap = _ref.projectIdMap,
    _ref$env = _ref.env,
    env = _ref$env === void 0 ? 'production' : _ref$env,
    secretInfo = _ref.secretInfo,
    eventMap = _ref.eventMap,
    tableHeaderMap = _ref.tableHeaderMap;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var startTime, endTime, preStartTime, preEndTime, headerDate, sortKeyList, data, preData, parsedData, parsedPreData, tableData, img;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          startTime = getSomeDayStartTimeStamp(date);
          endTime = getSomeDayEndTimeStamp(date);
          preStartTime = getSomeDayStartTimeStamp(new Date(date).getTime() - 1 * 24 * 60 * 60 * 1000);
          preEndTime = getSomeDayEndTimeStamp(new Date(date).getTime() - 1 * 24 * 60 * 60 * 1000);
          headerDate = timeStampFormat(new Date(date).getTime(), 'yyyy-MM-dd');
          sortKeyList = Object.keys(tableHeaderMap);
          if (sortKeyList.length) {
            _context.next = 1;
            break;
          }
          return _context.abrupt("return");
        case 1:
          _context.next = 2;
          return getMultiCustomEventData({
            startTime: startTime,
            endTime: endTime,
            projectIdMap: projectIdMap,
            env: env,
            secretInfo: secretInfo
          });
        case 2:
          data = _context.sent;
          _context.next = 3;
          return getMultiCustomEventData({
            startTime: preStartTime,
            endTime: preEndTime,
            projectIdMap: projectIdMap,
            env: env,
            secretInfo: secretInfo
          });
        case 3:
          preData = _context.sent;
          if (data) {
            _context.next = 4;
            break;
          }
          return _context.abrupt("return");
        case 4:
          if (preData) {
            _context.next = 5;
            break;
          }
          return _context.abrupt("return");
        case 5:
          parsedData = parseMultiCustomEvent({
            eventDataMap: data,
            eventMap: eventMap,
            projectIdMap: projectIdMap,
            sortKeyList: sortKeyList
          });
          parsedPreData = parseMultiCustomEvent({
            eventDataMap: preData,
            eventMap: eventMap,
            projectIdMap: projectIdMap,
            sortKeyList: sortKeyList
          });
          compareTwoList(parsedData, parsedPreData, 'ProjectName');
          tableData = getMaxAndMinIdx(parsedData);
          img = '';
          try {
            img = createCanvasTable({
              data: tableData,
              headers: getTableHeaders(tableData, tableHeaderMap),
              title: "\u62C9\u8D77\u6E38\u620F\u6570\u636E\u6C47\u603B ".concat(headerDate),
              cellWidthList: Object.values(tableHeaderMap).map(function (item) {
                return item.tableWidth || 65;
              })
            });
          } catch (err) {
            console.log('[genCustomEventImg.createCanvasTable.error]', err);
          }
          return _context.abrupt("return", img);
        case 6:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
/**
 * 获取自定义事件图片并发送
 * @param {object} options 配置信息
 * @returns {string} 图片url
 * @example
 *
 * const requestMultiImgDate = Date.now() - 1 * 24 * 60 * 60 * 1000;
 *
 * const tamGroupIdList = [1, 2, 3];
 *
 * const eventProjectMap = {
 *   62659: {
 *     name: 'aaaaa',
 *   },
 *   57706: {
 *     name: 'bbbbb',
 *     extraProjectId: 66379,
 *   },
 * };
 *
 * const eventMap = {
 *   WX_SUC: {
 *     // 总和
 *     type: 'SUMMARY',
 *     target: ['ENTER_GAME_WX_SUC', 'LAUNCH_GAME_SUC_WX'],
 *   },
 *   WX_FAIL: {
 *     // 总和
 *     type: 'SUMMARY',
 *     target: ['ENTER_GAME_WX_FAIL', 'LAUNCH_GAME_FAIL_WX'],
 *   },
 * };
 *
 * const eventTableHeaderMap = {
 *   ProjectName: {
 *     name: '项目名称',
 *     tableWidth: 95,
 *   },
 *   ALL_SUMMARY: {
 *     name: '拉起总数',
 *     tableWidth: 65,
 *   },
 * };
 *
 * genCustomEventImgAndSendRobot({
 *   date: requestLaunchGameDate,
 *   secretInfo: {
 *     getPwdCode,
 *     encrypt,
 *     apiKey: process.env.AEGIS_APP_KEY,
 *     loginName: 'lee',
 *   },
 *   projectIdMap: eventProjectMap,
 *   eventMap,
 *   tableHeaderMap: eventTableHeaderMap,
 *   webhookUrl: tamRobotWebhook,
 *   chatId: tamRobotChatId,
 * });
 *
 */
function genCustomEventImgAndSendRobot(_ref2) {
  var date = _ref2.date,
    projectIdMap = _ref2.projectIdMap,
    _ref2$env = _ref2.env,
    env = _ref2$env === void 0 ? 'production' : _ref2$env,
    secretInfo = _ref2.secretInfo,
    eventMap = _ref2.eventMap,
    tableHeaderMap = _ref2.tableHeaderMap,
    webhookUrl = _ref2.webhookUrl,
    chatId = _ref2.chatId;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var img;
    return _regeneratorRuntime.wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 1;
          return genCustomEventImg({
            date: date,
            projectIdMap: projectIdMap,
            env: env,
            secretInfo: secretInfo,
            eventMap: eventMap,
            tableHeaderMap: tableHeaderMap
          });
        case 1:
          img = _context2.sent;
          if (!(!img || !chatId)) {
            _context2.next = 2;
            break;
          }
          return _context2.abrupt("return");
        case 2:
          _context2.next = 3;
          return batchSendWxRobotBase64Img({
            img: img,
            webhookUrl: webhookUrl,
            chatId: chatId
          });
        case 3:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
}

export { genCustomEventImg, genCustomEventImgAndSendRobot };
