/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import dayjs from 'dayjs';
import { DAY_CN_MAP, LAST_MONTH_OF_YEAR, FIRST_MONTH_OF_YEAR } from './const.js';

const getDay = dt => {
  let day = dayjs(dt).day();
  if (day === 0) day = 7;
  return day;
};
const getDayCn = num => {
  let re = "";
  const numStr = num.toString();
  if (numStr in DAY_CN_MAP) re = DAY_CN_MAP[numStr];
  return re;
};
const getCellColIndex = (firstDayOfWeek, dt) => {
  let re = 0;
  const day = getDay(dt);
  if (day >= firstDayOfWeek) re = day - firstDayOfWeek;else re = 7 - firstDayOfWeek + day;
  return re;
};
const addDate = (dt, days) => {
  const d = new Date(dt);
  d.setDate(d.getDate() + days);
  return d;
};
const createYearCellsData = (year, curDate, format) => {
  const monthsArr = [];
  const isCurYear = curDate.year() === year;
  for (let num = FIRST_MONTH_OF_YEAR; num <= LAST_MONTH_OF_YEAR; num++) {
    const date = new Date(year, num - 1);
    const curDateMon = parseInt(curDate.format("M"), 10);
    const isCurrent = isCurYear && curDateMon === num;
    monthsArr.push({
      mode: "year",
      isCurrent,
      date,
      formattedDate: dayjs(date).format(format),
      filterDate: null,
      formattedFilterDate: null,
      isShowWeekend: true
    });
  }
  return monthsArr;
};
const createMonthCellsData = (year, month, firstDayOfWeek, curDate, format) => {
  const daysArr = [];
  const begin = dayjs(`${year}-${month}`).startOf("month").toDate();
  const end = dayjs(`${year}-${month}`).endOf("month").toDate();
  const days = end.getDate();
  const beginDateColIndex = getCellColIndex(firstDayOfWeek, begin);
  let arr = [];
  let num = 1;
  const createCellData = (belongTo, isCurrent, date, weekOrder) => {
    const day = getDay(date);
    return {
      mode: "month",
      belongTo,
      isCurrent,
      day,
      weekOrder,
      date,
      formattedDate: dayjs(date).format(format),
      filterDate: null,
      formattedFilterDate: null,
      isShowWeekend: true
    };
  };
  for (let i = 0; i < beginDateColIndex; i++) {
    const date = addDate(begin, i - beginDateColIndex);
    arr.push(createCellData(-1, false, date, num));
    if (arr.length === 7) {
      daysArr.push(arr);
      arr = [];
      num += 1;
    }
  }
  for (let i = 0; i < days; i++) {
    const date = addDate(begin, i);
    arr.push(createCellData(0, curDate.isSame(dayjs(date)), date, num));
    if (arr.length === 7) {
      daysArr.push(arr);
      arr = [];
      num += 1;
    }
  }
  if (arr.length) {
    const nextMonthCellNum = 7 - arr.length;
    for (let i = 0; i < nextMonthCellNum; i++) {
      const date = addDate(end, i + 1);
      arr.push(createCellData(1, false, date, num));
    }
    daysArr.push(arr);
  }
  return daysArr;
};

export { addDate, createMonthCellsData, createYearCellsData, getCellColIndex, getDayCn };
//# sourceMappingURL=utils.js.map
