"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.testDateDiff = exports.judgeTimeSpaceFromNow = void 0;

var _constant = require("./constant");

var _getLocale = require("./getLocale");

// 精确到日期的检测 date为2021/8/26这样的类型，返回距现在几天
var testDateDiff = function testDateDiff(date) {
  var handleDate = date.length == 5 ? "".concat(new Date().getFullYear(), "/").concat(date) : date;
  var testTime = Date.parse(handleDate.replace(/-/g, "/").split("T").join(" ").split(".")[0]);
  var now = Date.now();
  var diffDay = Math.floor((now - testTime) / (1000 * 60 * 60 * 24));
  return diffDay;
}; // 精确到天的检测


exports.testDateDiff = testDateDiff;

var judgeTimeSpaceFromNow = function judgeTimeSpaceFromNow(date) {
  var withHM = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  if (!date) return (0, _getLocale.getLocale)("OKR_MyO_KRP_Notupdated");
  var months = (0, _constant.getMonth)(); // 兼容safari，safari不支持yy-mm-dd格式，支持yy/mm/dd格式

  var handleDate = date.length === 5 ? "".concat(new Date().getFullYear(), "/").concat(date) : date.replace(/-/g, "/").split("T").join(" ").split(".")[0];
  var timeDateObj = new Date(handleDate);
  var timeMilliSecond = timeDateObj.getTime();
  var timeYear = timeDateObj.getFullYear();
  var timeMonth = timeDateObj.getMonth() + 1;
  var timeDate = timeDateObj.getDate();
  var timeHours = timeDateObj.getHours();
  var timeMinutes = timeDateObj.getMinutes();
  var nowTime = new Date();
  var nowMilliSecond = nowTime.getTime();
  var nowYear = nowTime.getFullYear();
  var nowDate = nowTime.getDate();
  var secondDiff = Math.floor((nowMilliSecond - timeMilliSecond) / 1000);

  if (secondDiff <= 3) {
    return (0, _getLocale.getLocale)("Pro_page_Int_Justnow");
  }

  if (secondDiff < 60) {
    return "".concat(secondDiff).concat((0, _getLocale.getLocale)("Mood_secago"));
  }

  if (secondDiff < 60 * 60) {
    return "".concat(Math.floor(secondDiff / 60)).concat((0, _getLocale.getLocale)("Tasks_Tab_MyS_minuteago"));
  }

  if (secondDiff < 60 * 60 * 24) {
    return "".concat(Math.floor(secondDiff / (60 * 60))).concat((0, _getLocale.getLocale)("Mood_hoursago"));
  }

  if (secondDiff < 60 * 60 * 24 * 2 && nowDate - 1 === timeDate) {
    return "".concat((0, _getLocale.getLocale)("OKR_MyO_Pr_Text_Yesterday")).concat(withHM ? " ".concat("".concat(timeHours < 10 ? "0".concat(timeHours) : timeHours, ":").concat(timeMinutes < 10 ? "0".concat(timeMinutes) : timeMinutes)) : "");
  }

  if (secondDiff < 60 * 60 * 24 * 3 && nowDate - 2 === timeDate) {
    return "".concat((0, _getLocale.getLocale)("Mood_2daysago")).concat(withHM ? " ".concat("".concat(timeHours < 10 ? "0".concat(timeHours) : timeHours, ":").concat(timeMinutes < 10 ? "0".concat(timeMinutes) : timeMinutes)) : "");
  }

  if (timeYear === nowYear) {
    var _dateStr = months[timeMonth - 1] + (_getLocale.isEn ? " ".concat(timeDate, " ") : "".concat(timeDate, "\u65E5"));

    return "".concat(_dateStr).concat(withHM ? " ".concat("".concat(timeHours < 10 ? "0".concat(timeHours) : timeHours, ":").concat(timeMinutes < 10 ? "0".concat(timeMinutes) : timeMinutes)) : "");
  }

  var dateStr = (_getLocale.isEn ? "" : "".concat(timeYear, "\u5E74")) + months[timeMonth - 1] + (_getLocale.isEn ? " ".concat(timeDate) : "".concat(timeDate, "\u65E5")) + (_getLocale.isEn ? ",".concat(timeYear) : "");
  return "".concat(dateStr).concat(withHM ? " ".concat("".concat(timeHours < 10 ? "0".concat(timeHours) : timeHours, ":").concat(timeMinutes < 10 ? "0".concat(timeMinutes) : timeMinutes)) : "");
};

exports.judgeTimeSpaceFromNow = judgeTimeSpaceFromNow;