const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_calendarItem = require("./calendarItem.js");
const require_model = require("../model.js");
const require_serve = require("./serve.js");
const require_utils = require("./utils.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/schedules/calendar/index.tsx
const MONTH_CARD_WIDTH = 300;
const Calendar = () => {
	const { state, dispatch } = (0, react.useContext)(require_model.Context);
	const { calendar, currentSelectDayDetail } = state;
	const { allData, editData, isEdit } = calendar || {};
	const { date: selectDate } = currentSelectDayDetail || {};
	const calenderRef = (0, react.useRef)(null);
	const [colCount, setColCount] = (0, react.useState)(4);
	const [dateRange] = (0, react.useState)([(0, dayjs.default)().format("YYYY-01"), (0, dayjs.default)().add(1, "year").format("YYYY-06")]);
	const [monthData, setMonthDate] = (0, react.useState)({});
	(0, react.useEffect)(() => {
		const startMonth = (0, dayjs.default)(dateRange[0]);
		const endMonth = (0, dayjs.default)(dateRange[1]);
		const months = { [(0, 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, react.useEffect)(() => {
		if (!calenderRef.current) return;
		const resizeObserver = new ResizeObserver(() => {
			var _calenderRef$current;
			const width = ((_calenderRef$current = calenderRef.current) === null || _calenderRef$current === void 0 ? void 0 : _calenderRef$current.clientWidth) || 0;
			const colCount = Math.floor((width - 20) / MONTH_CARD_WIDTH);
			setColCount(colCount > 4 ? 4 : colCount);
		});
		resizeObserver.observe(calenderRef.current);
		return () => {
			resizeObserver.disconnect();
		};
	}, []);
	const { loading, run, refresh } = (0, ahooks.useRequest)(require_serve.getScheduleAvailability, {
		manual: true,
		onSuccess: (result, params) => {
			if (result.status) dispatch({
				type: "setCalendar",
				payload: {
					sourceData: result.data,
					allData: require_utils.formatAvailability(result.data)
				}
			});
		}
	});
	(0, 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 || item === void 0 ? void 0 : item.schedule_ids
			} }
		});
		dispatch({
			type: "setCalendar",
			payload: {
				isEdit: false,
				editData: []
			}
		});
	};
	const schedulesData = (0, react.useMemo)(() => {
		if (isEdit) return editData || [];
		return allData;
	}, [
		isEdit,
		allData,
		editData
	]);
	return /* @__PURE__ */ react.default.createElement(antd.Spin, { spinning: loading }, /* @__PURE__ */ react.default.createElement("div", {
		className: "calender-box",
		ref: calenderRef
	}, Object.keys(monthData).map((year) => {
		return /* @__PURE__ */ react.default.createElement("div", {
			key: year,
			className: "calender-year-wrap"
		}, /* @__PURE__ */ react.default.createElement("div", { className: "calender-year" }, year), /* @__PURE__ */ react.default.createElement("div", { className: "month-list" }, monthData[year].map((month, index) => {
			return /* @__PURE__ */ react.default.createElement("div", {
				className: `month-item-box month-item-${colCount}`,
				key: index,
				style: {}
			}, /* @__PURE__ */ react.default.createElement(require_calendarItem.default, {
				isEdit,
				month,
				schedulesData,
				selectDate,
				onSelect: handleSelectDate
			}));
		}), monthData[year].length % colCount ? (0, _pisell_utils.createArray)(colCount - monthData[year].length % colCount, (_, i) => {
			return /* @__PURE__ */ react.default.createElement("div", {
				key: i,
				style: { width: MONTH_CARD_WIDTH }
			});
		}) : null));
	})));
};
//#endregion
exports.default = Calendar;
