const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_utils = require("../../utils.js");
const require_context = require("../../context.js");
const require_hooks = require("../../hooks.js");
const require_state = require("./state.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 classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let _ant_design_icons = require("@ant-design/icons");
let dayjs_plugin_isSameOrBefore = require("dayjs/plugin/isSameOrBefore");
dayjs_plugin_isSameOrBefore = require_runtime.__toESM(dayjs_plugin_isSameOrBefore);
require("./index.less");
//#region src/components/appointmentBooking/components/Date/index.tsx
dayjs.default.extend(dayjs_plugin_isSameOrBefore.default);
const Date = (0, react.forwardRef)((props, ref) => {
	var _context$engine2, _Dayjs$locale;
	const isFetchingDates = (0, react.useRef)(false);
	const [loading, setLoading] = (0, react.useState)(false);
	const [currentDate, setCurrentDate] = (0, react.useState)();
	const [dateMonthList, setDateMonthList] = (0, react.useState)([]);
	const [currentMonth, setCurrentMonth] = (0, react.useState)();
	const { appointmentBooking } = require_context.useAppointmentBookingContext();
	const { isDateSelectedFirst, customPageId, channel } = require_hooks.useAppointmentBookingSetting();
	const { nextStep } = require_hooks.useAppointmentBookingStep();
	const context = require_useEngineContext.default();
	const { isMobile } = require_hooks.useConfig();
	(0, react.useImperativeHandle)(ref, () => ({ next: handleNext }));
	/**
	* @title: 处理进入下一步的逻辑
	* @description:
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-05-13 15:36
	*/
	const handleNext = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			const data = yield appointmentBooking.getDateRange();
			if (!(data === null || data === void 0 ? void 0 : data.length)) return require_utils.Toast(_pisell_utils.locales.getText("pisell2.appointment.unselect.date"));
			nextStep();
		});
		return function handleNext() {
			return _ref.apply(this, arguments);
		};
	}();
	const weekListText = [
		_pisell_utils.locales.getText("pisell2.appointment.sunday"),
		_pisell_utils.locales.getText("pisell2.appointment.monday"),
		_pisell_utils.locales.getText("pisell2.appointment.tuesday"),
		_pisell_utils.locales.getText("pisell2.appointment.wednesday"),
		_pisell_utils.locales.getText("pisell2.appointment.thursday"),
		_pisell_utils.locales.getText("pisell2.appointment.friday"),
		_pisell_utils.locales.getText("pisell2.appointment.saturday")
	];
	const startingColumn = 0;
	const columns = Array.from({ length: 7 }, (_, index) => (index + 7 - startingColumn) % 7);
	(0, react.useEffect)(() => {
		(function() {
			var _ref2 = require_asyncToGenerator._asyncToGenerator(function* () {
				var _data$;
				const data = yield appointmentBooking.getDateRange();
				setCurrentMonth((0, dayjs.default)(data === null || data === void 0 || (_data$ = data[0]) === null || _data$ === void 0 ? void 0 : _data$.date) || (0, dayjs.default)());
			});
			return function getCurrentMonthData() {
				return _ref2.apply(this, arguments);
			};
		})()();
	}, []);
	const locale = (0, react.useMemo)(() => {
		var _context$engine;
		return require_state.localeTypes[context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale] || "en";
	}, [context === null || context === void 0 || (_context$engine2 = context.engine) === null || _context$engine2 === void 0 || (_context$engine2 = _context$engine2.props) === null || _context$engine2 === void 0 ? void 0 : _context$engine2.locale]);
	/**
	* @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 === null || currentMonth === void 0 ? void 0 : currentMonth.format("YYYY-MM")).subtract(1, "month").isBefore((0, dayjs.default)(), "month");
	};
	/**
	* @title: 获取日期数据
	* @description:
	* @param {*} params
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-04-24 18:31
	*/
	const getDateData = function() {
		var _ref3 = require_asyncToGenerator._asyncToGenerator(function* (params) {
			console.log(isDateSelectedFirst, customPageId, params, channel, "wh7868");
			if (isDateSelectedFirst) return appointmentBooking.loadScheduleAvailableDate(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, params), {}, {
				custom_page_id: customPageId,
				channel
			}));
			return yield appointmentBooking.getAvailableDate(params);
		});
		return function getDateData(_x) {
			return _ref3.apply(this, arguments);
		};
	}();
	const getDates = function() {
		var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (date) {
			try {
				const data = yield getDateData({
					startDate: (0, dayjs.default)(date).startOf("month").format("YYYY-MM-DD"),
					endDate: (0, dayjs.default)(date).endOf("month").format("YYYY-MM-DD")
				});
				const firstAvailableDate = (data || []).find((d) => d.status === "available");
				if (!firstAvailableDate && !isFetchingDates.current) {
					isFetchingDates.current = true;
					setCurrentMonth((0, dayjs.default)(date).add(1, "month"));
					setLoading(true);
					return;
				}
				const selected = yield appointmentBooking.getDateRange();
				console.log(data, selected, customPageId, "getDates");
				if ((selected === null || selected === void 0 ? void 0 : selected.length) > 0) {
					var _selected$;
					setCurrentDate(selected === null || selected === void 0 || (_selected$ = selected[0]) === null || _selected$ === void 0 ? void 0 : _selected$.date);
				} else if (!isDateSelectedFirst) {
					if (date.format("YYYY-MM") === (0, dayjs.default)().format("YYYY-MM") || isFetchingDates.current) {
						if (firstAvailableDate) {
							setCurrentDate(firstAvailableDate === null || firstAvailableDate === void 0 ? void 0 : firstAvailableDate.date);
							appointmentBooking.setDateRange([firstAvailableDate]);
						}
					}
				}
				const updatedDates = getMonthData(date.format("YYYY-MM")).map((day) => {
					const matchedDay = data.find((d) => d.date === day.date);
					if (matchedDay) return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, day), matchedDay), {}, { disabled: matchedDay.status === "unavailable" });
					return day;
				});
				setLoading(false);
				setDateMonthList(updatedDates);
			} catch (error) {
				setLoading(false);
				setDateMonthList([]);
			}
		});
		return function getDates(_x2) {
			return _ref4.apply(this, arguments);
		};
	}();
	(0, react.useEffect)(() => {
		if (!currentMonth) return;
		getDates(currentMonth);
	}, [currentMonth]);
	/**
	* @title: 切换上个月
	* @description:
	* @param {*}
	* @return {*}
	* @Author: 严文强
	* @Date: 2024-02-01 19:15
	*/
	const lastMonth = () => {
		setCurrentMonth((0, dayjs.default)(currentMonth === null || currentMonth === void 0 ? void 0 : currentMonth.format("YYYY-MM")).subtract(1, "month"));
	};
	/**
	* @title: 切换下个月
	* @description:
	* @param {*}
	* @return {*}
	* @Author: 严文强
	* @Date: 2024-02-01 19:15
	*/
	const nextMonth = () => {
		setCurrentMonth((0, dayjs.default)(currentMonth === null || currentMonth === void 0 ? void 0 : currentMonth.format("YYYY-MM")).add(1, "month"));
		setLoading(true);
	};
	/**
	* @title: 点击某一天日期
	* @description:
	* @param {any} day
	* @param {any} date
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-04-24 18:36
	*/
	const handleClick = (day, date) => {
		if (day === null || day === void 0 ? void 0 : day.disabled) return;
		setCurrentDate(date);
		appointmentBooking.setDateRange([day]);
		if (isDateSelectedFirst) handleNext();
	};
	return /* @__PURE__ */ react.default.createElement("div", { className: `calendar-date-box ${isMobile ? "calendar-date-mobile-box" : ""}` }, !dateMonthList.length || !currentMonth ? /* @__PURE__ */ react.default.createElement(antd.Skeleton, {
		loading: true,
		active: true,
		style: { paddingTop: "16px" }
	}) : /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { className: "calendar-header" }, /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("calendar-last-month", { ["last-month-disabled"]: lastMonthDisable() }),
		onClick: () => {
			if (lastMonthDisable()) return;
			lastMonth();
		}
	}, /* @__PURE__ */ react.default.createElement(_ant_design_icons.CaretLeftOutlined, null)), /* @__PURE__ */ react.default.createElement("div", { className: "calendar-current-month" }, (_Dayjs$locale = (0, dayjs.default)(currentMonth).locale(locale)) === null || _Dayjs$locale === void 0 ? void 0 : _Dayjs$locale.format("MMMM YYYY")), /* @__PURE__ */ react.default.createElement("div", {
		className: "calendar-next-month",
		onClick: nextMonth
	}, /* @__PURE__ */ react.default.createElement(_ant_design_icons.CaretRightOutlined, null))), /* @__PURE__ */ react.default.createElement("div", { className: "calendar-content" }, /* @__PURE__ */ react.default.createElement("div", { className: "calendar-weeks" }, columns.map((col) => /* @__PURE__ */ react.default.createElement("div", {
		key: col,
		className: "calendar-weeks-items"
	}, weekListText[Number(col)]))), /* @__PURE__ */ react.default.createElement("div", { className: "calendar-days" }, dateMonthList.map((day, index) => {
		const date = (0, dayjs.default)(day.date).format("YYYY-MM-DD");
		const isCurrentMonth = (0, dayjs.default)(day.date).isSame(currentMonth, "month");
		return /* @__PURE__ */ react.default.createElement("div", {
			key: index,
			className: (0, classnames.default)("calendar-days-items", {
				["disabledDate"]: day === null || day === void 0 ? void 0 : day.disabled,
				["selectDate"]: currentDate === date,
				["other-month-days"]: !isCurrentMonth,
				["loading-item"]: loading
			}),
			onClick: () => handleClick(day, date)
		}, isCurrentMonth && /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("day-text") }, (0, dayjs.default)(day.date).format("D")));
	})))));
});
//#endregion
exports.default = Date;
