import { getPartRatio } from '../../number/number.mjs';
import { saveJsonToLog } from '../../fs/fs-util.mjs';
import '@babel/runtime/helpers/toConsumableArray';
import '../../fs/fs.mjs';
import '../../nodejs/crypto.mjs';
import '../../nodejs/fs.mjs';
import '../../nodejs/os.mjs';
import '../../nodejs/path.mjs';
import '../../time/time.mjs';

/**
 * 处理事件数据
 * @ignore
 * @param {object} options 配置
 * @example
 *
 * const res = parseEventData({
 *   data: {
 *     LAUNCH_GAME_WX_SUC: 2,
 *     LAUNCH_GAME_WX_FAIL: 1,
 *     ENTER_GAME_WX_SUC: 2,
 *     ENTER_GAME_WX_FAIL: 6,
 *   },
 *   eventMap: {
 *     WX_SUC: {
 *       // 总和
 *       type: 'SUMMARY',
 *       target: ['LAUNCH_GAME_WX_SUC', 'ENTER_GAME_WX_SUC'],
 *     },
 *     WX_FAIL: {
 *       // 总和
 *       type: 'SUMMARY',
 *       target: ['LAUNCH_GAME_WX_FAIL', 'ENTER_GAME_WX_FAIL'],
 *     },
 *     WX_SUMMARY: {
 *       // 总和
 *       type: 'SUMMARY',
 *       target: ['WX_SUC', 'WX_FAIL'],
 *     },
 *     WX_SUC_RATIO: {
 *       // 比例
 *       type: 'RATIO',
 *       target: [
 *         // 分母部分
 *         ['WX_SUMMARY'],
 *         // 分子部分
 *         ['WX_SUC'],
 *       ],
 *     },
 *   },
 * });
 *
 * console.log(res);
 *
 * // 结果如下：
 * {
 *   WX_SUC: 4,
 *   WX_FAIL: 7,
 *   WX_SUMMARY: 11,
 *   WX_SUC_RATIO: 36.36,
 * }
 *
 *
 */
function parseEventData(_ref) {
  var data = _ref.data,
    eventMap = _ref.eventMap;
  var res = {};
  Object.keys(eventMap).forEach(function (event) {
    var info = eventMap[event] || {};
    var target = info.target,
      type = info.type;
    if (type === 'SUMMARY') {
      res[event] = getEventSummary(target, data, res) || 0;
    } else if (type === 'RATIO') {
      var _target$ = target[0],
        summary = _target$ === void 0 ? [] : _target$,
        _target$2 = target[1],
        part = _target$2 === void 0 ? [] : _target$2;
      res[event] = getPartRatio(getEventSummary(summary, data, res), getEventSummary(part, data, res)) || 0;
    }
  });
  res.ALL_SUMMARY = Object.keys(res).filter(function (key) {
    return key.endsWith('_SUMMARY');
  }).reduce(function (acc, item) {
    return acc + res[item];
  }, 0);
  res.ALL_FAIL = Object.keys(res).filter(function (key) {
    return key.endsWith('_FAIL');
  }).reduce(function (acc, item) {
    return acc + res[item];
  }, 0);
  res.ALL_FAIL_RATIO = getPartRatio(res.ALL_SUMMARY, res.ALL_FAIL);
  return res;
}
/**
 * 获取数据综合
 * @ignore
 * @param keyList keyList
 * @param dataMap dataMap
 * @param extraDataMap extraDataMap
 * @returns 数据综合
 */
function getEventSummary(keyList, dataMap, extraDataMap) {
  return keyList.reduce(function (acc, item) {
    return acc + (dataMap[item] || extraDataMap[item] || 0);
  }, 0);
}
/**
 * 解析额外数据
 * @ignore
 * @param eventDataMap 数据map
 * @returns 解析后的数据
 * @example
 * const eventDataMap = {
 * '57706': {
    LAUNCH_GAME_FAIL_GP_HELPER: 2,
    LAUNCH_GAME_FAIL_QQ: 228,
    extraData: {
      JUMP_LIVE_ACCOUNT_CONSOLE_SUC: 27,
      LAUNCH_GAME_FAIL_MP: 11,
      LAUNCH_GAME_SUC_MP: 261,
    }
  },
  '62653': {
 * }
 *
 * const res = parseExtraData(eventDataMap)
 * console.log(res)
 * {
 *   57706: {
 *   LAUNCH_GAME_FAIL_GP_HELPER: 2,
     LAUNCH_GAME_FAIL_QQ: 228,
 *   EXTRA_DATA_JUMP_LIVE_ACCOUNT_CONSOLE_SUC: 27,
     EXTRA_DATA_LAUNCH_GAME_FAIL_MP: 11,
     EXTRA_DATA_LAUNCH_GAME_SUC_MP: 261,
 *   },
     62653: {
      // ...
     }
 * }
 */
function parseExtraData(eventDataMap) {
  var res = {};
  saveJsonToLog(eventDataMap, 'tam.custom-event.event-data-map.json');
  Object.keys(eventDataMap).forEach(function (projectId) {
    var eventData = eventDataMap[projectId];
    if (eventData.extraData) {
      Object.keys(eventData.extraData).forEach(function (key) {
        eventData["EXTRA_DATA_".concat(key)] = eventData.extraData[key];
      });
      delete eventData.extraData;
    }
    res[projectId] = eventData;
  });
  saveJsonToLog(res, 'tam.custom-event.parsed-extra-data.json');
  return res;
}
/**
 * 解析自定义事件数据
 * @ignore
 * @param options 配置
 * @returns 解析后的数据
 */
function parseMultiCustomEvent(_ref2) {
  var eventDataMap = _ref2.eventDataMap,
    eventMap = _ref2.eventMap,
    _ref2$projectIdMap = _ref2.projectIdMap,
    projectIdMap = _ref2$projectIdMap === void 0 ? {} : _ref2$projectIdMap,
    sortKeyList = _ref2.sortKeyList;
  var parsedEventData = parseExtraData(eventDataMap);
  var res = [];
  Object.keys(parsedEventData).forEach(function (projectId) {
    var _a;
    var eventData = eventDataMap[projectId];
    var parsedData = parseEventData({
      data: eventData,
      eventMap: eventMap
    });
    // 统一格式，类似于：[ { ProjectName: { name: 'ProjectName', value: '脚手架' } } ]
    var initial = {
      ProjectName: {
        name: 'ProjectName',
        value: (_a = projectIdMap[projectId]) === null || _a === void 0 ? void 0 : _a.name
      }
    };
    initial = Object.keys(parsedData).filter(function (item) {
      return sortKeyList.includes(item);
    }).sort(function (a, b) {
      return sortKeyList.indexOf(a) - sortKeyList.indexOf(b);
    }).reduce(function (acc, key) {
      acc[key] = {
        name: key,
        value: parsedData[key]
      };
      return acc;
    }, initial);
    res.push(initial);
  });
  saveJsonToLog(res, 'tam.custom-event.parsed-multi-custom-event.json');
  return res;
}

export { parseMultiCustomEvent };
