'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var isUdf = _interopDefault(require('./isUdf'));
var formatToDate = _interopDefault(require('./formatToDate'));
var getDayOfYear = _interopDefault(require('./getDayOfYear'));

/**
 * 获取给定时间的日期在当年的第几个星期
 * @param {Date|number|string} time
 * @returns {number}
 *
 * getWeekOfYear('2021-01-01');  // => 1
 * getWeekOfYear('2021-01-03');  // => 1
 * getWeekOfYear('2021-01-04');  // => 2
 */
function getWeekOfYear(time) {
  if (isUdf(time)) time = new Date();
  var date = formatToDate(time);
  if (!date) return NaN;
  var days = getDayOfYear(date);
  var yearFirstDay = new Date(date.getFullYear(), 0, 1).getDay();
  days += yearFirstDay - 1;
  return Math.ceil(days / 7);
}

module.exports = getWeekOfYear;
