/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 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 = (props, state) => {
  const {
    curSelectedYear: year,
    curDate,
    curDateList
  } = state;
  const {
    format,
    multiple
  } = props;
  const monthsArr = [];
  for (let num = FIRST_MONTH_OF_YEAR; num <= LAST_MONTH_OF_YEAR; num++) {
    const date = new Date(year, num - 1);
    const isCurrent = multiple ? !!curDateList.find(item => item.year() === year && parseInt(item.format("M"), 10) === num) : curDate.year() === year && parseInt(curDate.format("M"), 10) === num;
    monthsArr.push({
      mode: "year",
      isCurrent,
      date,
      formattedDate: dayjs(date).format(format),
      filterDate: null,
      formattedFilterDate: null,
      isShowWeekend: true
    });
  }
  return monthsArr;
};
const createMonthCellsData = (props, state) => {
  const {
    curSelectedYear: year,
    curSelectedMonth: month,
    realFirstDayOfWeek: firstDayOfWeek,
    curDate,
    curDateList
  } = state;
  const {
    format,
    multiple
  } = props;
  const daysArr = [];
  const begin = dayjs(`${year}-${month}`).startOf("month").toDate();
  const end = dayjs(dayjs(`${year}-${month}`).endOf("month").format("YYYY-MM-DD")).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
    };
  };
  const judgeIsCurrent = date => {
    const isCurrent = multiple ? !!curDateList.find(item => item.isSame(dayjs(date))) : curDate.isSame(dayjs(date));
    return isCurrent;
  };
  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);
    const isCurrent = judgeIsCurrent(date);
    arr.push(createCellData(0, isCurrent, 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
