"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const date = require("../../../constants/date.js");
const index = require("../../../hooks/use-locale/index.js");
const utils = require("../../time-picker/src/utils.js");
const dateTable = require("./date-table.js");
const vue = require("vue");
const dayjs_min = require("../../../../../../../_virtual/dayjs.min.js");
const localeData = require("../../../../../../../_virtual/localeData.js");
const useDateTable = (props, emit) => {
  dayjs_min.default.extend(localeData.default);
  const firstDayOfWeek = dayjs_min.default.localeData().firstDayOfWeek();
  const { t, lang } = index.useLocale();
  const now = dayjs_min.default().locale(lang.value);
  const isInRange = vue.computed(() => !!props.range && !!props.range.length);
  const rows = vue.computed(() => {
    let days = [];
    if (isInRange.value) {
      const [start, end] = props.range;
      const currentMonthRange = utils.rangeArr(end.date() - start.date() + 1).map((index2) => ({
        text: start.date() + index2,
        type: "current"
      }));
      let remaining = currentMonthRange.length % 7;
      remaining = remaining === 0 ? 0 : 7 - remaining;
      const nextMonthRange = utils.rangeArr(remaining).map((_, index2) => ({
        text: index2 + 1,
        type: "next"
      }));
      days = currentMonthRange.concat(nextMonthRange);
    } else {
      const firstDay = props.date.startOf("month").day();
      const prevMonthDays = dateTable.getPrevMonthLastDays(props.date, (firstDay - firstDayOfWeek + 7) % 7).map((day) => ({
        text: day,
        type: "prev"
      }));
      const currentMonthDays = dateTable.getMonthDays(props.date).map((day) => ({
        text: day,
        type: "current"
      }));
      days = [...prevMonthDays, ...currentMonthDays];
      const nextMonthDays = utils.rangeArr(7 - (days.length % 7 || 7)).map((_, index2) => ({
        text: index2 + 1,
        type: "next"
      }));
      days = days.concat(nextMonthDays);
    }
    return dateTable.toNestedArr(days);
  });
  const weekDays = vue.computed(() => {
    const start = firstDayOfWeek;
    if (start === 0) return date.WEEK_DAYS.map((_) => t(`el.datepicker.weeks.${_}`));
    else return date.WEEK_DAYS.slice(start).concat(date.WEEK_DAYS.slice(0, start)).map((_) => t(`el.datepicker.weeks.${_}`));
  });
  const getFormattedDate = (day, type) => {
    switch (type) {
      case "prev":
        return props.date.startOf("month").subtract(1, "month").date(day);
      case "next":
        return props.date.startOf("month").add(1, "month").date(day);
      case "current":
        return props.date.date(day);
    }
  };
  const handlePickDay = ({ text, type }) => {
    emit("pick", getFormattedDate(text, type));
  };
  const getSlotData = ({ text, type }) => {
    const day = getFormattedDate(text, type);
    return {
      isSelected: day.isSame(props.selectedDay),
      type: `${type}-month`,
      day: day.format("YYYY-MM-DD"),
      date: day.toDate()
    };
  };
  return {
    now,
    isInRange,
    rows,
    weekDays,
    getFormattedDate,
    handlePickDay,
    getSlotData
  };
};
exports.useDateTable = useDateTable;
