const require_runtime = require("../../../../../../_virtual/_rolldown/runtime.js");
const require_index = require("./Status/index.js");
const require_state = require("./state.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let dayjs_plugin_isSameOrBefore = require("dayjs/plugin/isSameOrBefore");
dayjs_plugin_isSameOrBefore = require_runtime.__toESM(dayjs_plugin_isSameOrBefore);
require("./index.less");
//#region src/components/eventBooking/components/bookingAvailabilityCalendar/components/CalendarDate/index.tsx
dayjs.default.extend(dayjs_plugin_isSameOrBefore.default);
const CalendarDate = (props, ref) => {
	const { cutTime, getLimitData, dateLimitData, loading } = props;
	const [productId, setProductId] = (0, react.useState)("");
	const [currentDate, setCurrentDate] = (0, react.useState)("");
	const [dateMonthList, setDateMonthList] = (0, react.useState)([]);
	const [currentMonth, setCurrentMonth] = (0, react.useState)(() => {
		return (0, dayjs.default)();
	});
	const [open, setOpen] = (0, react.useState)(false);
	const startingColumn = 0;
	const columns = Array.from({ length: 7 }, (_, index) => (index + 7 - startingColumn) % 7);
	/**
	* @title: 获取当前月日期
	* @description:
	* @param {*}
	* @return {*}
	* @Author: 严文强
	* @Date: 2024-02-01 20:30
	*/
	const getMonthData = (date) => {
		const firstDay = (0, dayjs.default)(`${date}-01`);
		const startDay = firstDay.day();
		const daysInMonth = firstDay.daysInMonth();
		const monthData = [];
		for (let i = 0; i < startDay; i++) monthData.push({
			day: -1,
			date: (0, dayjs.default)(firstDay).subtract(startDay - i, "day").toDate()
		});
		for (let i = 1; i <= daysInMonth; i++) monthData.push({
			day: i,
			date: firstDay.date(i).format("YYYY-MM-DD")
		});
		return monthData;
	};
	/**
	* @title: 计算是否已经切换到当前月，如果是则禁止继续点击上一个月
	* @description:
	* @param {*}
	* @return {*}
	* @Author: 严文强
	* @Date: 2024-02-01 19:27
	*/
	const lastMonthDisable = () => {
		return (0, dayjs.default)(currentMonth.format("YYYY-MM")).subtract(1, "month").isBefore((0, dayjs.default)(), "month");
	};
	/**
	* @title: 切换上个月
	* @description:
	* @param {*}
	* @return {*}
	* @Author: 严文强
	* @Date: 2024-02-01 19:15
	*/
	const lastMonth = () => {
		let newLastMonth = (0, dayjs.default)(currentMonth.format("YYYY-MM")).subtract(1, "month");
		setCurrentMonth(newLastMonth);
		setDateMonthList(getMonthData(newLastMonth.format("YYYY-MM")));
	};
	/**
	* @title: 切换下个月
	* @description:
	* @param {*}
	* @return {*}
	* @Author: 严文强
	* @Date: 2024-02-01 19:15
	*/
	const nextMonth = () => {
		let newNextMonth = (0, dayjs.default)(currentMonth.format("YYYY-MM")).add(1, "month");
		setCurrentMonth(newNextMonth);
		setDateMonthList(getMonthData(newNextMonth.format("YYYY-MM")));
	};
	/**
	* @title: 根据当前选择的月份剩余量
	* @description:
	* @return {*}
	* @Author: xiangxiang.shao
	* @Date: 2024-02-07 14:13
	*/
	const CalendarDateContent = (0, react.useMemo)(() => {
		return /* @__PURE__ */ React.createElement("div", { className: "calendar-box" }, /* @__PURE__ */ React.createElement("div", { className: "calendar-header" }, /* @__PURE__ */ React.createElement("div", {
			className: (0, classnames.default)("calendar-last-month", { "last-month-disabled": lastMonthDisable() }),
			onClick: () => {
				if (lastMonthDisable()) return;
				lastMonth();
			}
		}, /* @__PURE__ */ React.createElement(_pisell_materials.Iconfont, { type: "pisell2-chevron-left" })), /* @__PURE__ */ React.createElement("div", { className: "calendar-current-month" }, currentMonth.format("MMMM YYYY")), /* @__PURE__ */ React.createElement("div", {
			className: "calendar-next-month",
			onClick: () => {
				nextMonth();
			}
		}, /* @__PURE__ */ React.createElement(_pisell_materials.Iconfont, { type: "pisell2-chevron-right" }))), /* @__PURE__ */ React.createElement("div", { className: "calendar-content" }, /* @__PURE__ */ React.createElement("div", { className: "calendar-weeks" }, columns.map((col) => /* @__PURE__ */ React.createElement("div", {
			key: col,
			className: "calendar-weeks-items"
		}, require_state.weekListText[Number(col)]))), /* @__PURE__ */ React.createElement("div", { className: "calendar-days" }, dateMonthList.map((day, index) => {
			const date = (0, dayjs.default)(day.date).format("YYYY-MM-DD");
			const { status, disabled } = require_state.getStatus(dateLimitData, date, cutTime);
			const currentStatus = require_state.getCallToBookStatus(date, dateLimitData, cutTime) && (status === "lots_of_space" || status === "filling_up_fast") ? "call_to_book" : status;
			return /* @__PURE__ */ React.createElement("div", {
				key: index,
				className: (0, classnames.default)("calendar-days-items", {
					disabledDate: disabled || loading,
					disabledSelect: status === "sold_out",
					selectDate: currentDate === date,
					"other-month-days": (day === null || day === void 0 ? void 0 : day.day) < 1
				}),
				onClick: () => {
					if (loading) return;
					props.dateChange(date);
				}
			}, (day === null || day === void 0 ? void 0 : day.day) >= 1 && /* @__PURE__ */ React.createElement("div", {
				style: { "--lineColor": !loading ? require_state.statusColor[currentStatus] : "" },
				className: (0, classnames.default)("day-text", { "day-line": !disabled })
			}, (day === null || day === void 0 ? void 0 : day.day) < 1 ? "" : day === null || day === void 0 ? void 0 : day.day));
		}))), /* @__PURE__ */ React.createElement(require_index.default, { style: { margin: 0 } }));
	}, [
		currentMonth,
		dateMonthList,
		getLimitData,
		currentDate
	]);
	(0, react.useImperativeHandle)(ref, () => {
		return { open: (date, id) => {
			setOpen(true);
			let daysText = (0, dayjs.default)(date).format("YYYY-MM");
			setCurrentMonth((0, dayjs.default)(date));
			setCurrentDate(date);
			setDateMonthList(getMonthData(daysText));
		} };
	});
	return /* @__PURE__ */ React.createElement(_pisell_materials.PisellModal, {
		open,
		title: "device",
		mobileModalHeight: "auto",
		platform: (0, _pisell_utils.isMobile)() ? "h5" : "pc",
		bodyStyle: { padding: (0, _pisell_utils.isMobile)() ? "16px 24px" : 32 },
		destroyOnClose: true,
		className: "select-holder-modal",
		footer: null,
		onCancel: () => setOpen(false)
	}, CalendarDateContent);
};
var CalendarDate_default = (0, react.forwardRef)(CalendarDate);
//#endregion
exports.default = CalendarDate_default;
