import moment from 'moment';

//
export function generateWeekList(startYear, endYear, startWeek, endWeek) {
  const weeks = [];
  const start = moment(startYear+ String(startWeek).padStart(2, '0'), "GGGGWW", true).startOf('isoWeek');
  const end = moment(endYear+ String(endWeek).padStart(2, '0'), "GGGGWW", true).startOf('isoWeek');


  let current = start.clone();
  let lastMonth = '';
  let monthIdx = 0;
  while (current.isSameOrBefore(end)) {
    let cur3 = current.clone().add(3, 'days'); //목요일기준 

    if (lastMonth == cur3.format('MM')) {
      monthIdx++;
    } else {
      monthIdx = 0;
    }
    lastMonth = cur3.format('MM');

    weeks.push({
      start: current.format('YYYYMMDD'),
      end: current.clone().endOf('isoWeek').format('YYYYMMDD'),
      week: current.isoWeek(),
      year: current.isoWeekYear(),
      formatted: current.format('GGGG[W]WW'),//`${current.isoWeekYear()}W${current.isoWeek().toString().padStart(2, '0')}`,
      month: `${current.isoWeekYear()}M${cur3.format('MM')}`,
      monthIdx: monthIdx,
      lastMonth: lastMonth,
      quarter: `${current.isoWeekYear()}Q${Math.ceil((cur3.month() + 1) / 3)}`, // Adding 1 because moment months are 0-based
      half: `${current.isoWeekYear()}H${Math.ceil((cur3.month() + 1) / 6)}`,
    });

    current.add(1, 'week');
  }

  return weeks;
}

export default {
  generateWeekList,
};
