'use strict';

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

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../../tslib.es6-0d92ef81.js');
var canvas_table = require('../../canvas/table.js');
var time_time = require('../../time/time.js');
var wecomRobot_batchSend = require('../../wecom-robot/batch-send.js');
var tam_api_customEvent = require('../api/custom-event.js');
var tam_parse_customEvent = require('../parse/custom-event.js');
var list_parseList = require('../../list/parse-list.js');
var tam_parse_header = require('../parse/header.js');
var date_timeStamp = require('../../date/timeStamp.js');
require('../../canvas/get-canvas.js');
require('../../third-party/canvas.js');
require('../../list/list.js');
require('@babel/runtime/helpers/toConsumableArray');
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('../api/credential.js');
require('../../fs/fs-util.js');
require('../../fs/fs.js');
require('../../nodejs/os.js');
require('../../number/number.js');

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

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

/**
 * 获取自定义事件图片
 * @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 tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var startTime, endTime, preStartTime, preEndTime, headerDate, sortKeyList, data, preData, parsedData, parsedPreData, tableData, img;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          startTime = date_timeStamp.getSomeDayStartTimeStamp(date);
          endTime = date_timeStamp.getSomeDayEndTimeStamp(date);
          preStartTime = date_timeStamp.getSomeDayStartTimeStamp(new Date(date).getTime() - 1 * 24 * 60 * 60 * 1000);
          preEndTime = date_timeStamp.getSomeDayEndTimeStamp(new Date(date).getTime() - 1 * 24 * 60 * 60 * 1000);
          headerDate = time_time.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 tam_api_customEvent.getMultiCustomEventData({
            startTime: startTime,
            endTime: endTime,
            projectIdMap: projectIdMap,
            env: env,
            secretInfo: secretInfo
          });
        case 2:
          data = _context.sent;
          _context.next = 3;
          return tam_api_customEvent.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 = tam_parse_customEvent.parseMultiCustomEvent({
            eventDataMap: data,
            eventMap: eventMap,
            projectIdMap: projectIdMap,
            sortKeyList: sortKeyList
          });
          parsedPreData = tam_parse_customEvent.parseMultiCustomEvent({
            eventDataMap: preData,
            eventMap: eventMap,
            projectIdMap: projectIdMap,
            sortKeyList: sortKeyList
          });
          list_parseList.compareTwoList(parsedData, parsedPreData, 'ProjectName');
          tableData = list_parseList.getMaxAndMinIdx(parsedData);
          img = '';
          try {
            img = canvas_table.createCanvasTable({
              data: tableData,
              headers: tam_parse_header.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 tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
    var img;
    return _regeneratorRuntime__default["default"].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 wecomRobot_batchSend.batchSendWxRobotBase64Img({
            img: img,
            webhookUrl: webhookUrl,
            chatId: chatId
          });
        case 3:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
}

exports.genCustomEventImg = genCustomEventImg;
exports.genCustomEventImgAndSendRobot = genCustomEventImgAndSendRobot;
