"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.endTime = endTime;
exports.getWeeksNum = exports.getWeekNumber = exports.getWeek = exports.getPointYearMonthWeeksRange = exports.getPointYearMonthWeekStartTimeAndEndTime = exports.getDaysOfMonth = void 0;
exports.startTime = startTime;

//获取当前日期在这个月是第几周
var getWeekNumber = function getWeekNumber(date) {
  var currentDate = new Date(date).getDate();
  var day = new Date(date).getDay(); //今天是星期几 星期天返回0

  var week = Math.ceil((currentDate + 6 - day) / 7);
  return week;
}; //获取某年某月有多少天


exports.getWeekNumber = getWeekNumber;

var getDaysOfMonth = function getDaysOfMonth(year, month) {
  switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
      return 31;

    case 4:
    case 6:
    case 9:
    case 11:
      return 30;

    case 2:
      return year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 29 : 28;

    default:
      return 0;
  }
};

exports.getDaysOfMonth = getDaysOfMonth;

function startTime(time) {
  var nowTimeDate = new Date(time);
  return nowTimeDate.setHours(0, 0, 0, 0);
}

function endTime(time) {
  var nowTimeDate = new Date(time);
  return nowTimeDate.setHours(23, 59, 59, 999);
}

var getWeeksNum = function getWeeksNum(year, month) {
  var d = new Date(); // 该月第一天

  d.setFullYear(year, month - 1, 1);
  var w1 = d.getDay();
  if (w1 == 0) w1 = 7; // 该月天数

  d.setFullYear(year, month, 0);
  var dd = d.getDate(); // 第一个周一

  var d1;
  if (w1 != 1) d1 = 7 - w1 + 2;else d1 = 1;
  var week_count = Math.ceil((dd - d1 + 1) / 7);
  return week_count;
};

exports.getWeeksNum = getWeeksNum;

var getPointYearMonthWeekStartTimeAndEndTime = function getPointYearMonthWeekStartTimeAndEndTime(year, month, week) {
  // 获取当月的周期
  var date = new Date("".concat(year, "/").concat(month, "/1"));
  var nowDayOfWeek = date.getDay();
  if (nowDayOfWeek === 0) nowDayOfWeek = 7;
  var dayNum = 1 * 24 * 60 * 60 * 1000;
  var firstDate = new Date(date.valueOf() - (nowDayOfWeek - 1) * dayNum);
  var lastDate = new Date(new Date(firstDate).valueOf() + 6 * dayNum * week);
  var startDate = new Date(startTime(new Date(firstDate).valueOf() + 7 * dayNum * (week - 1)));
  var endDate = new Date(endTime(lastDate));
  var startMonth = startDate.getMonth() + 1 > 9 ? startDate.getMonth() + 1 : '0' + (startDate.getMonth() + 1);
  var startDateValue = startDate.getDate() > 9 ? startDate.getDate() : '0' + startDate.getDate();
  var endMonth = endDate.getMonth() + 1 > 9 ? endDate.getMonth() + 1 : '0' + (endDate.getMonth() + 1);
  var endDateValue = endDate.getDate() > 9 ? endDate.getDate() : '0' + endDate.getDate();
  return {
    startTime: "".concat(startMonth, "/").concat(startDateValue),
    endTime: "".concat(endMonth, "/").concat(endDateValue)
  };
};

exports.getPointYearMonthWeekStartTimeAndEndTime = getPointYearMonthWeekStartTimeAndEndTime;

var getPointYearMonthWeeksRange = function getPointYearMonthWeeksRange(year, month) {
  var startDate = new Date("".concat(year, "/").concat(month, "/1"));
  var calDays = new Date(startDate.valueOf());
  calDays.setMonth(calDays.getMonth() + 1);
  calDays.setDate(0);
  var days = calDays.getDate();
  var dayNum = 1 * 24 * 60 * 60 * 1000;
  var result = [];

  for (var i = 0; i < days; i++) {
    var date = new Date(startDate.valueOf() + i * dayNum);

    if (date.getDay() === 1) {
      var endDate = new Date(date.valueOf() + 6 * dayNum);
      result.push({
        startTime: "".concat(date.getMonth() + 1 >= 10 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1), "/").concat(date.getDate() >= 10 ? date.getDate() : '0' + date.getDate()),
        endTime: "".concat(endDate.getMonth() + 1 >= 10 ? endDate.getMonth() + 1 : '0' + (endDate.getMonth() + 1), "/").concat(endDate.getDate() >= 10 ? endDate.getDate() : '0' + endDate.getDate())
      });
      i += 6;
    }
  }

  return result;
}; //获取某个日期处于的年月周


exports.getPointYearMonthWeeksRange = getPointYearMonthWeeksRange;

var getWeek = function getWeek(date) {
  var isPreMonth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  var currentMonth = new Date(date).getMonth() + 1;
  var currentYear = new Date(date).getFullYear();
  var currentDate = new Date(date).getDate();
  var week;
  var range = getPointYearMonthWeeksRange(currentYear, currentMonth);

  if (new Date(range[0].startTime).getDate() > currentDate) {
    //计入上个月
    currentMonth -= 1;

    var _range = getPointYearMonthWeeksRange(currentYear, currentMonth);

    week = _range.length;
  } else {
    range.forEach(function (item, index) {
      if (new Date(item.startTime).getDate() < currentDate && new Date(item.endTime).getDate() > currentDate || new Date(item.startTime).getDate() === currentDate || new Date(item.endTime).getDate() === currentDate) {
        week = index + 1;
      }
    });
  }

  return [week, currentMonth, currentYear];
};

exports.getWeek = getWeek;