import _typeof from '@babel/runtime/helpers/typeof';

function beautiTime(time) {
  if (time >= 10) return "".concat(time);
  return "0".concat(time);
}
/**
 * 功能和上面的dateFormat/timeStampFormat类型，只是参数time可以接收多种类型，且参数cFormat用的是{y}形式
 * @param {(Object|string|number)} time 输入日期
 * @param {string} cFormat 时间格式
 * @returns {string | null} 格式化后的日期字符串
 * @example
 *
 * const date = new Date('2020-11-27 8:23:24');
 *
 * const res = parseTime(date, 'yyyy-MM-dd hh:mm:ss')
 *
 * // 2020-11-27 08:23:24
 */
function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null;
  }
  var format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
  var date;
  if (_typeof(time) === 'object') {
    date = time;
  } else {
    if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
      time = parseInt(time, 10);
    }
    if (typeof time === 'number' && time.toString().length === 10) {
      time *= 1000;
    }
    date = new Date(time);
  }
  var formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  };
  var timeStr = format.replace(/{([ymdhisa])+}/g, function (result, key) {
    var value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value];
    }
    return value.toString().padStart(2, '0');
  });
  return timeStr;
}
/**
 * 获取某个时间戳距离今天的时间
 * @param {number} timestamp
 * @returns {string} 距离今天的时间描述
 * @example
 *
 * const date = new Date('2020-11-27 8:23:24').getTime();
 * getTimeAgo(date);
 * // 1个月前
 *
 * const date2 = new Date('2021-11-27 8:23:24').getTime();
 * getTimeAgo(date2);
 * // 10个月后
  */
function getTimeAgo(timestamp) {
  var mistiming = Math.round(Date.now() / 1000) - timestamp;
  var postfix = mistiming > 0 ? '前' : '后';
  mistiming = Math.abs(mistiming);
  var arrr = ['年', '个月', '星期', '天', '小时', '分钟', '秒'];
  var arrn = [31536000, 2592000, 604800, 86400, 3600, 60, 1];
  for (var i = 0; i < 7; i++) {
    var inm = Math.floor(mistiming / arrn[i]);
    if (inm !== 0) {
      return inm + arrr[i] + postfix;
    }
  }
  return '';
}
/**
 * 功能：获取多久之前，若间隔超过一天，返回时刻描述
 * @param {number} timestamp 时间戳
 * @param {string} format 时间格式
 * @returns {string} 距离今天的时间描述或者时刻描述
 * @example
 *
 * getTimeAgoOrDate(Date.now() - 60 * 60 * 24 * 1 * 1000);
 * // 1天前
 *
 * const date = new Date('2018-07-13 17:54:01').getTime();
 * getTimeAgoOrDate(date);
 * // 7月13日17时54分
 */
function getTimeAgoOrDate(time, format) {
  if ("".concat(time).length === 10) {
    time = parseInt("".concat(time), 10) * 1000;
  } else {
    time = +time;
  }
  var d = new Date(time);
  var now = Date.now();
  var diff = (now - d.getTime()) / 1000;
  if (diff < 30) {
    return '刚刚';
  }
  if (diff < 3600) {
    // less 1 hour
    return "".concat(Math.ceil(diff / 60), "\u5206\u949F\u524D");
  }
  if (diff < 3600 * 24) {
    return "".concat(Math.ceil(diff / 3600), "\u5C0F\u65F6\u524D");
  }
  if (diff < 3600 * 24 * 2) {
    return '1天前';
  }
  if (format) {
    return parseTime(time, format);
  }
  return "".concat(d.getMonth() + 1, "\u6708").concat(d.getDate(), "\u65E5").concat(d.getHours(), "\u65F6").concat(d.getMinutes(), "\u5206");
}
/**
 * 倒计时（eg:距开赛1天）
 * @param {string} time  剩余时间
 * @param {SECOND | MINUTE | HOUR | DAY } [maxUnit]  最大单位
 * @returns {object} 剩余时间的描述对象
 * @example
 *
 * getCountDownObj(100)
 * // { day: 0, hour: 0, minute: 1, second: 40 }
 *
 * getCountDownObj(1*24*60*60+200)
 * // { day: 1, hour: 0, minute: 3, second: 20 }
 *
 * getCountDownObj(1 * 24 * 60 * 60 + 2 * 60 * 60 + 1 * 60 + 11, 'HOUR')
 * // 结果 =>
 * {
 *   fHour: '26',
 *   fMinute: '01',
 *   fSecond: '11',
 *   hour: 26,
 *   minute: 1,
 *   second: 11,
 * }
 */
function getCountDownObj(time) {
  var maxUnit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'DAY';
  if (!time) {
    return {};
  }
  time = parseInt("".concat(time), 10);
  var second = Math.floor(time % 60);
  // 秒是最大单位
  if (maxUnit === 'SECOND') {
    return {
      second: time,
      fSecond: beautiTime(second)
    };
  }
  var minute = Math.floor(time / 60 % 60);
  // 分钟是最大单位
  if (maxUnit === 'MINUTE') {
    minute = Math.floor(time / 60);
    return {
      minute: minute,
      second: second,
      fMinute: beautiTime(minute),
      fSecond: beautiTime(second)
    };
  }
  var hour = Math.floor(time / 60 / 60 % 24);
  // 小时为最大单位
  if (maxUnit === 'HOUR') {
    hour = Math.floor(time / 60 / 60);
    return {
      hour: hour,
      minute: minute,
      second: second,
      fHour: beautiTime(hour),
      fMinute: beautiTime(minute),
      fSecond: beautiTime(second)
    };
  }
  // 天为最大单位
  if (maxUnit === 'DAY') {
    var day = Math.floor(time / 60 / 60 / 24);
    return {
      day: day,
      hour: hour,
      minute: minute,
      second: second,
      fDay: beautiTime(day),
      fHour: beautiTime(hour),
      fMinute: beautiTime(minute),
      fSecond: beautiTime(second)
    };
  }
  return {};
}

export { getCountDownObj, getTimeAgo, getTimeAgoOrDate, parseTime };
