/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var dayjs = require('dayjs');
var components_calendar_const = require('./const.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);

const getDay = dt => {
  let day = dayjs__default["default"](dt).day();
  if (day === 0) day = 7;
  return day;
};
const getDayCn = num => {
  let re = "";
  const numStr = num.toString();
  if (numStr in components_calendar_const.DAY_CN_MAP) re = components_calendar_const.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 = components_calendar_const.FIRST_MONTH_OF_YEAR; num <= components_calendar_const.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__default["default"](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__default["default"](`${year}-${month}`).startOf("month").toDate();
  const end = dayjs__default["default"](dayjs__default["default"](`${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__default["default"](date).format(format),
      filterDate: null,
      formattedFilterDate: null,
      isShowWeekend: true
    };
  };
  const judgeIsCurrent = date => {
    const isCurrent = multiple ? !!curDateList.find(item => item.isSame(dayjs__default["default"](date))) : curDate.isSame(dayjs__default["default"](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;
};

exports.addDate = addDate;
exports.createMonthCellsData = createMonthCellsData;
exports.createYearCellsData = createYearCellsData;
exports.getCellColIndex = getCellColIndex;
exports.getDayCn = getDayCn;
//# sourceMappingURL=utils.js.map
