import useEngineContext from "../../../../hooks/useEngineContext.js";
import { formatBackgroundColors, formatMonthDisplay } from "../../utils.js";
import ScheduleTimes from "./components/ScheduleTimes/index.js";
import dayjs from "dayjs";
import React, { useMemo } from "react";
import { Calendar } from "antd";
import { Iconfont } from "@pisell/materials";
import zhCN from "antd/es/date-picker/locale/zh_CN";
import enUS from "antd/es/date-picker/locale/en_US";
//#region src/components/eventBooking/components/scheduleCalendar/calendarItem.tsx
const CalendarItem = (props) => {
	var _utils$storage;
	const { month, schedulesData, selectDate, onSelect, isEdit, onChange, type } = props;
	const utils = useEngineContext().appHelper.utils || {};
	const locale = (utils === null || utils === void 0 || (_utils$storage = utils.storage) === null || _utils$storage === void 0 || (_utils$storage = _utils$storage.get("umi_locale")) === null || _utils$storage === void 0 ? void 0 : _utils$storage.includes("zh")) ? zhCN : enUS;
	const scheduleMap = useMemo(() => new Map(schedulesData.map((schedule) => [schedule.date, schedule])), [schedulesData]);
	/**
	* @title: 月份切换
	* @description:
	* @param {string} type
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-12 11:48
	*/
	const handleMonth = (type) => {
		const date = /* @__PURE__ */ new Date(month + "-01");
		if (type === "prev") date.setMonth(date.getMonth() - 1);
		else date.setMonth(date.getMonth() + 1);
		onChange(date.toISOString().slice(0, 7), type);
	};
	return /* @__PURE__ */ React.createElement("div", { className: "month-item" }, /* @__PURE__ */ React.createElement("div", { className: "calendar-month" }, /* @__PURE__ */ React.createElement("div", {
		onClick: () => handleMonth("prev"),
		className: "calendar-month-icon"
	}, /* @__PURE__ */ React.createElement(Iconfont, { type: "pisell2-chevron-left" })), /* @__PURE__ */ React.createElement("span", { className: "calendar-month-title" }, formatMonthDisplay(month)), /* @__PURE__ */ React.createElement("div", {
		onClick: () => handleMonth("next"),
		className: "calendar-month-icon"
	}, /* @__PURE__ */ React.createElement(Iconfont, { type: "pisell2-chevron-right" }))), type === "textList" ? /* @__PURE__ */ React.createElement(ScheduleTimes, {
		month,
		schedulesData
	}) : /* @__PURE__ */ React.createElement(Calendar, {
		locale,
		headerRender: () => null,
		fullscreen: false,
		value: dayjs(month),
		disabledDate: (currentDate) => {
			return !schedulesData.some((item) => currentDate.isSame(item.date)) || currentDate.format("YYYY-MM") !== month;
		},
		fullCellRender: (e) => {
			const isDisabled = !schedulesData.some((item) => e.isSame(item.date)) || e.format("YYYY-MM") !== month;
			const currentDayText = e.format("YYYY-MM-DD");
			const currentDayData = scheduleMap.get(currentDayText);
			const isCurrentMonth = e.format("YYYY-MM") === month;
			const isSelectDate = currentDayText === selectDate;
			const isExcluded = currentDayData === null || currentDayData === void 0 ? void 0 : currentDayData.isExcluded;
			let style = { color: isCurrentMonth ? "#101828" : "inherit" };
			const colors = !isEdit ? ["#7F56D9"] : (currentDayData === null || currentDayData === void 0 ? void 0 : currentDayData.color) || [];
			if (isDisabled) return /* @__PURE__ */ React.createElement("div", { style }, e.date());
			style = { background: formatBackgroundColors(colors) };
			return /* @__PURE__ */ React.createElement("div", {
				className: `caleddar-date-cell ${isSelectDate ? "select" : ""} ${isExcluded ? "excluded" : ""}`,
				onClick: () => onSelect(currentDayText, scheduleMap.get(currentDayText)),
				style: {
					position: "absolute",
					left: "50%",
					top: "50%",
					transform: "translate(-50%, -50%)",
					display: "flex",
					alignItems: "center",
					justifyContent: "center"
				}
			}, /* @__PURE__ */ React.createElement("div", {
				className: `caleddar-date ${colors.filter(Boolean).length ? "has-status" : ""} ${isCurrentMonth ? "" : "disabled-date"}`,
				style
			}, e.date()));
		}
	}));
};
//#endregion
export { CalendarItem as default };
