'use strict';

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

var fs_fsUtil = require('../fs/fs-util.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../fs/fs.js');
require('../nodejs/crypto.js');
require('../nodejs/fs.js');
require('../nodejs/os.js');
require('../nodejs/path.js');
require('../time/time.js');

/**
 * 组装发送给机器人的消息
 * @private
 * @param {Array<object>} usefulData 处理后的数据
 * @returns {string} 发送给机器人的markdown消息
 * @example
 * ```ts
 * composeRobotContent([]);
 * // '当前深圳预警已全部解除'
 *
 * composeRobotContent([
 *   { date: '2023-09-01 12:00', alarmArea: '南山区', alarmType: '暴雨', alarmColor: '黄色' },
 * ]);
 * // '>【深圳当前生效的预警】\n1. 暴雨黄色预警·南山区，发布时间：2023-09-01 12:00\n[深圳市气象局](http://...)'
 * ```
 */
function composeRobotContent() {
  var usefulData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  if (!usefulData.length) {
    return '当前深圳预警已全部解除';
  }
  var content = ['>【深圳当前生效的预警】'];
  var WEB_LINK = 'http://weather.sz.gov.cn/qixiangfuwu/yujingfuwu/tufashijianyujing/';
  var tempList = usefulData.map(function (item, index) {
    var date = item.date,
      alarmArea = item.alarmArea,
      alarmType = item.alarmType,
      alarmColor = item.alarmColor;
    var list = ["".concat(index + 1, ". ").concat(alarmType).concat(alarmColor || '', "\u9884\u8B66\xB7").concat(alarmArea, "\uFF0C\u53D1\u5E03\u65F6\u95F4\uFF1A").concat(date) // `${str}`,
    ];
    return list.join('\n');
  });
  content.push(tempList.join('\n'));
  content.push("[\u6DF1\u5733\u5E02\u6C14\u8C61\u5C40](".concat(WEB_LINK, ")"));
  return content.join('\n');
}
/**
 * 对比数据
 * @private
 * @param {Array<object>} usefulData 处理后的数据
 * @returns {boolean} 是否和以前数据一样
 * @example
 * ```ts
 * compareData([{ alarmType: '暴雨' }]);
 * // true 或 false（取决于上次保存的 alarm.json）
 * ```
 */
function compareData() {
  var usefulData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var FILE_NAME = 'alarm.json';
  var preData = {};
  try {
    preData = JSON.parse(fs_fsUtil.readJsonLog(FILE_NAME));
  } catch (e) {
    console.log('[compareData] err: ', e);
  }
  var isSame = JSON.stringify(preData) === JSON.stringify(usefulData);
  fs_fsUtil.saveJsonToLog(usefulData, FILE_NAME);
  return isSame;
}
/**
 * 处理weather数据
 * @private
 * @param {Array<object>} subAlarm 天气数据
 * @returns 处理后的数据
 * @example
 * ```ts
 * const { content, isSame } = parseWeatherData(subAlarm);
 * if (!isSame) sendRobot(content);
 * ```
 */
function parseWeatherData() {
  var subAlarm = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var usefulData = subAlarm.map(function (item) {
    var date = item.date,
      alarmArea = item.alarmArea,
      alarmType = item.alarmType,
      alarmColor = item.alarmColor,
      str = item.str;
    return {
      date: date,
      alarmArea: alarmArea,
      alarmType: alarmType,
      alarmColor: alarmColor,
      str: str
    };
  });
  var isSame = compareData(usefulData);
  var content = composeRobotContent(usefulData);
  return {
    content: content,
    isSame: isSame
  };
}

exports.compareData = compareData;
exports.composeRobotContent = composeRobotContent;
exports.parseWeatherData = parseWeatherData;
