var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_CalendarDay = __toESM(require("./CalendarDay"));
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_utils = require("./utils");
var import_utils2 = require("../utils");
var import_get_rect = require("../hooks/get-rect");
const CalendarMonth = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("calendar", prefixCls);
  const [visible, setVisible] = (0, import_react.useState)(false);
  const {
    showMonthTitle,
    date,
    showMark,
    firstDayOfWeek
  } = props;
  const daysRef = (0, import_react.useRef)(null);
  const [monthRef, setMonthRef] = (0, import_react.useState)();
  const height = (0, import_react.useRef)(0);
  (0, import_react.useEffect)(() => {
    if (monthRef) {
      height.current = (0, import_get_rect.getRect)(monthRef).height;
    }
  }, [monthRef]);
  const title = (0, import_react.useMemo)(() => {
    const formatterFn = props.formatMonthTitle || import_utils.formatMonthTitle;
    return formatterFn(date);
  }, [date]);
  const rowHeight = (0, import_react.useMemo)(() => (0, import_utils2.addUnit)(props.rowHeight), [props.rowHeight]);
  const offset = (0, import_react.useMemo)(() => {
    const realDay = date.getDay();
    if (firstDayOfWeek) {
      return (realDay + 7 - firstDayOfWeek) % 7;
    }
    return realDay;
  }, [date, firstDayOfWeek]);
  const totalDay = (0, import_react.useMemo)(() => {
    return (0, import_utils.getMonthEndDay)(date.getFullYear(), date.getMonth() + 1);
  }, [date]);
  const shouldRender = (0, import_react.useMemo)(() => visible || !props.lazyRender, [visible, props.lazyRender]);
  const getTitle = () => title;
  const onClick = (item) => {
    var _a;
    (_a = props.onClick) == null ? void 0 : _a.call(props, item);
  };
  const getMultipleDayType = (day) => {
    const isSelected = (date2) => props.currentDate.some((item) => (0, import_utils.compareDay)(item, date2) === 0);
    if (isSelected(day)) {
      const prevDay = (0, import_utils.getPrevDay)(day);
      const nextDay = (0, import_utils.getNextDay)(day);
      const prevSelected = isSelected(prevDay);
      const nextSelected = isSelected(nextDay);
      if (prevSelected && nextSelected) {
        return "multiple-middle";
      }
      if (prevSelected) {
        return "end";
      }
      if (nextSelected) {
        return "start";
      }
      return "multiple-selected";
    }
    return "";
  };
  const getRangeDayType = (day) => {
    const [startDay, endDay] = props.currentDate;
    if (!startDay) {
      return "";
    }
    const compareToStart = (0, import_utils.compareDay)(day, startDay);
    if (!endDay) {
      return compareToStart === 0 ? "start" : "";
    }
    const compareToEnd = (0, import_utils.compareDay)(day, endDay);
    if (props.allowSameDay && compareToStart === 0 && compareToEnd === 0) {
      return "start-end";
    }
    if (compareToStart === 0) {
      return "start";
    }
    if (compareToEnd === 0) {
      return "end";
    }
    if (compareToStart > 0 && compareToEnd < 0) {
      return "middle";
    }
    return "";
  };
  const getDayType = (day) => {
    const {
      type,
      minDate,
      maxDate,
      currentDate
    } = props;
    if ((0, import_utils.compareDay)(day, minDate) < 0 || (0, import_utils.compareDay)(day, maxDate) > 0) {
      return "disabled";
    }
    if (Array.isArray(currentDate)) {
      if (type === "multiple") {
        return getMultipleDayType(day);
      }
      if (type === "range") {
        return getRangeDayType(day);
      }
    } else if (type === "single") {
      return (0, import_utils.compareDay)(day, currentDate) === 0 ? "selected" : "";
    }
    return "";
  };
  const getBottomInfo = (dayType) => {
    if (props.type === "range") {
      if (dayType === "start" || dayType === "end") {
        return (0, import_utils.t)(dayType);
      }
      if (dayType === "start-end") {
        return (0, import_utils.t)("startEnd");
      }
    }
    return "";
  };
  const renderTitle = () => {
    return showMonthTitle ? /* @__PURE__ */ import_react.default.createElement("div", {
      className: (0, import_classnames.default)(bem("month-title"))
    }, title) : null;
  };
  const renderMark = () => {
    return showMark && shouldRender ? /* @__PURE__ */ import_react.default.createElement("div", {
      className: (0, import_classnames.default)(bem("month-mark"))
    }, props.date.getMonth() + 1) : null;
  };
  const placeholders = (0, import_react.useMemo)(() => {
    const count = Math.ceil((totalDay + offset) / 7);
    return Array(count).fill({
      type: "placeholder"
    });
  }, [totalDay, offset]);
  const daysList = (0, import_react.useMemo)(() => {
    const days = [];
    const year = props.date.getFullYear();
    const month = props.date.getMonth();
    for (let day = 1; day <= totalDay; day++) {
      const date2 = new Date(year, month, day);
      const type = getDayType(date2);
      let config = {
        date: date2,
        type,
        text: day,
        bottomInfo: getBottomInfo(type)
      };
      if (props.formatter) {
        config = props.formatter(config);
      }
      days.push(config);
    }
    return days;
  }, [props.currentDate, props.date, props.formatter]);
  const disabledDays = (0, import_react.useMemo)(() => daysList.filter((day) => day.type === "disabled"), [daysList]);
  const scrollToDate = (body, targetDate) => {
    if (daysRef.current) {
      const daysRect = (0, import_get_rect.getRect)(daysRef.current);
      const totalRows = placeholders.length;
      const currentRow = Math.ceil((targetDate.getDate() + offset) / 7);
      const rowOffset = (currentRow - 1) * daysRect.height / totalRows;
      (0, import_utils2.setScrollTop)(body, daysRect.top + rowOffset + body.scrollTop - (0, import_get_rect.getRect)(body).top);
    }
  };
  const renderDay = (item, index) => {
    return /* @__PURE__ */ import_react.default.createElement(import_CalendarDay.default, {
      key: String(index),
      item,
      index,
      color: props.color,
      offset,
      rowHeight,
      onClick
    });
  };
  const renderDays = () => {
    return /* @__PURE__ */ import_react.default.createElement("div", {
      ref: daysRef,
      role: "grid",
      className: (0, import_classnames.default)(bem("days"))
    }, renderMark(), (shouldRender ? daysList : placeholders).map(renderDay));
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    getTitle,
    getHeight: () => height.current,
    setVisible,
    scrollToDate,
    disabledDays
  }));
  return /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem("month")),
    ref: setMonthRef
  }, renderTitle(), renderDays());
});
CalendarMonth.defaultProps = {};
var stdin_default = CalendarMonth;
