// 时间对象格式为文本格式
export var formatToString = function formatToString(date, type) {
  if (!date) {
    return null;
  }

  var _date = new Date(date);

  var y = _date.getFullYear();

  var m = _date.getMonth() + 1;
  m = m < 10 ? '0' + m : m;

  var d = _date.getDate();

  d = d < 10 ? '0' + d : d;
  return type === 'month' ? y + "/" + m : y + "/" + m + "/" + d;
};
export var formatToDate = function formatToDate(date) {
  if (!date) {
    return null;
  }

  if (typeof date === 'string') {
    date = date.replace(/-/g, "/");
    return new Date(date);
  } else {
    return new Date(date);
  }
};
export var foramtToMonth = function foramtToMonth(date) {
  if (!date) {
    return null;
  }

  var _date = null;

  if (typeof date === 'string') {
    var arr = date.replace(/-/g, "/").split('/');
    _date = new Date(arr[0] + "/" + arr[1] + "/01");
  } else {
    var d = new Date(date);
    _date = new Date(d.getFullYear() + "/" + (d.getMonth() + 1) + "/01");
  }

  return _date;
};
/**
 * 生成年的区间
 * @param {*} arr 
 */

export var getRangeYears = function getRangeYears(arr) {
  var num = arr[1];
  var years = [];

  while (num >= arr[0]) {
    years.unshift(num);
    num = num - 1;
  }

  return years;
};
/**
 * 生成所有年的星期
 * @param {*} arr 
 * @param {*} firstDayOfWeek 
 */

export var getYearsAndWeeks = function getYearsAndWeeks(arr, firstDayOfWeek) {
  var years = getRangeYears(arr);

  var getAYearWeeks = function getAYearWeeks(year) {
    var weeks = [];

    var _date = new Date(year, 11, 31);

    var _day = _date.getDay() - firstDayOfWeek + 1;

    var _start = new Date(year, 11, 31 - _day);

    var _end = new Date(year + 1, 0, 6 - _day);

    var f = function f(date) {
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      var d = date.getDate();
      return [y + "/" + m + "/" + d, (String(m).length > 1 ? m : '0' + m) + "/" + (String(d).length > 1 ? d : '0' + d)];
    };

    while (_end.getFullYear() >= year) {
      var _startArr = f(_start);

      var _endArr = f(_end);

      weeks.unshift({
        startDay: new Date(_startArr[0]),
        startDayStr: _startArr[1],
        endDay: new Date(_endArr[0]),
        endDayStr: _endArr[1]
      });

      _start.setDate(_start.getDate() - 7);

      _end.setDate(_end.getDate() - 7);
    }

    return weeks;
  };

  return years.map(function (year) {
    return {
      year: year,
      weeks: getAYearWeeks(year)
    };
  });
};
export var typeToSelectotMap = {
  'date': 'DateSelect',
  'daterange': 'DateRangeSelect',
  'month': 'MonthSelect',
  'monthrange': 'MonthRangeSelect',
  'week': 'WeekSelect',
  'quarter': 'QuarterSelect',
  'dateweekmonth': 'DateWeekMonth'
};