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(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/schedules/calendar/index.tsx
var calendar_exports = {};
__export(calendar_exports, {
  default: () => calendar_default
});
module.exports = __toCommonJS(calendar_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_react = __toESM(require("react"));
var import_calendarItem = __toESM(require("./calendarItem"));
var import_model = require("../model");
var import_utils = require("@pisell/utils");
var import_index = require("./index.less");
var import_ahooks = require("ahooks");
var import_serve = require("./serve");
var import_antd = require("antd");
var import_utils2 = require("./utils");
var MONTH_CARD_WIDTH = 300;
var Calendar = () => {
  const { state, dispatch } = (0, import_react.useContext)(import_model.Context);
  const { calendar, currentSelectDayDetail } = state;
  const { allData, editData, isEdit } = calendar || {};
  const { date: selectDate } = currentSelectDayDetail || {};
  const calenderRef = (0, import_react.useRef)(null);
  const [colCount, setColCount] = (0, import_react.useState)(4);
  const [dateRange] = (0, import_react.useState)([
    (0, import_dayjs.default)().format("YYYY-01"),
    (0, import_dayjs.default)().add(1, "year").format("YYYY-06")
  ]);
  const [monthData, setMonthDate] = (0, import_react.useState)({});
  (0, import_react.useEffect)(() => {
    const startMonth = (0, import_dayjs.default)(dateRange[0]);
    const endMonth = (0, import_dayjs.default)(dateRange[1]);
    const months = {
      [(0, import_dayjs.default)(dateRange[0]).format("YYYY")]: [dateRange[0]]
    };
    let currentMonth = startMonth;
    while (currentMonth.isBefore(endMonth) || currentMonth.isSame(endMonth, "month")) {
      currentMonth = currentMonth.add(1, "month");
      const year = currentMonth.format("YYYY");
      if (!months[year]) {
        months[year] = [];
      }
      months[year].push(currentMonth.format("YYYY-MM"));
    }
    setMonthDate(months);
  }, [dateRange]);
  (0, import_react.useEffect)(() => {
    if (!calenderRef.current) {
      return;
    }
    const resizeObserver = new ResizeObserver(() => {
      var _a;
      const width = ((_a = calenderRef.current) == null ? void 0 : _a.clientWidth) || 0;
      const colCount2 = Math.floor((width - 20) / MONTH_CARD_WIDTH);
      setColCount(colCount2 > 4 ? 4 : colCount2);
    });
    resizeObserver.observe(calenderRef.current);
    return () => {
      resizeObserver.disconnect();
    };
  }, []);
  const { loading, run, refresh } = (0, import_ahooks.useRequest)(import_serve.getScheduleAvailability, {
    manual: true,
    onSuccess: (result, params) => {
      if (result.status) {
        dispatch({
          type: "setCalendar",
          payload: {
            sourceData: result.data,
            allData: (0, import_utils2.formatAvailability)(result.data)
          }
        });
      }
    }
  });
  (0, import_react.useEffect)(() => {
    run({});
  }, []);
  const handleSelectDate = (date, item) => {
    if (isEdit) {
      return;
    }
    if (selectDate === date) {
      dispatch({
        type: "setState",
        payload: {
          currentSelectDayDetail: null
        }
      });
    } else {
      dispatch({
        type: "setState",
        payload: {
          currentSelectDayDetail: {
            date,
            scheduleIds: item == null ? void 0 : item.schedule_ids
          }
        }
      });
    }
    dispatch({
      type: "setCalendar",
      payload: {
        isEdit: false,
        editData: []
      }
    });
  };
  const schedulesData = (0, import_react.useMemo)(() => {
    if (isEdit) {
      return editData || [];
    }
    return allData;
  }, [isEdit, allData, editData]);
  return /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { spinning: loading }, /* @__PURE__ */ import_react.default.createElement("div", { className: "calender-box", ref: calenderRef }, Object.keys(monthData).map((year) => {
    return /* @__PURE__ */ import_react.default.createElement("div", { key: year, className: "calender-year-wrap" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "calender-year" }, year), /* @__PURE__ */ import_react.default.createElement("div", { className: "month-list" }, monthData[year].map((month, index) => {
      return /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: `month-item-box month-item-${colCount}`,
          key: index,
          style: {
            // width: `calc(100% / ${colCount})`,
          }
        },
        /* @__PURE__ */ import_react.default.createElement(
          import_calendarItem.default,
          {
            isEdit,
            month,
            schedulesData,
            selectDate,
            onSelect: handleSelectDate
          }
        )
      );
    }), monthData[year].length % colCount ? (0, import_utils.createArray)(
      colCount - monthData[year].length % colCount,
      (_, i) => {
        return /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            key: i,
            style: { width: MONTH_CARD_WIDTH }
          }
        );
      }
    ) : null));
  })));
};
var calendar_default = Calendar;
