const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_utils = require("../../../utils.js");
const require_context = require("../../../context.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
//#region src/components/venueBooking/components/VenueSelection/components/MonthPicker.tsx
/**
* 月历选择器
* 以日历网格形式展示一个月的日期，标记已选日期、今天、已售完日期
* 点击日期后更新锚点日期并关闭选择器
*/
function MonthPicker() {
	const { monthCursor, setMonthCursor, anchorDate, setAnchorDate, setMonthPickerOpen, fetchWeekData, viewModel } = require_context.useVenueBookingContext();
	const days = require_utils.getMonthDays(monthCursor);
	const selectedKey = require_utils.formatDateKey(anchorDate);
	const todayKey = require_utils.formatDateKey((0, dayjs.default)());
	const todayStart = (0, dayjs.default)().startOf("day");
	const knownDateRange = (0, react.useMemo)(() => {
		const knownDates = (viewModel.dateSummary || []).map((item) => (0, dayjs.default)(String((item === null || item === void 0 ? void 0 : item.date) || ""))).filter((date) => date.isValid()).sort((a, b) => a.valueOf() - b.valueOf());
		if (!knownDates.length) return null;
		return {
			minDate: knownDates[0].startOf("day"),
			maxDate: knownDates[knownDates.length - 1].startOf("day")
		};
	}, [viewModel.dateSummary]);
	/** 选中日期后：更新锚点 + 拉取该周数据 */
	function handleSelect(_x) {
		return _handleSelect.apply(this, arguments);
	}
	function _handleSelect() {
		_handleSelect = require_asyncToGenerator._asyncToGenerator(function* (day) {
			setAnchorDate(day);
			setMonthCursor(day.startOf("month"));
			setMonthPickerOpen(false);
			const weekDays = require_utils.getWeekDays(day);
			yield fetchWeekData(require_utils.formatDateKey(weekDays[0]), require_utils.formatDateKey(weekDays[weekDays.length - 1]));
		});
		return _handleSelect.apply(this, arguments);
	}
	return /* @__PURE__ */ react.default.createElement("div", { className: "absolute right-0 top-[calc(100%+12px)] z-30 w-[320px] rounded-[24px] border border-black/6 bg-white p-4 shadow-[0_20px_60px_rgba(20,20,20,0.14)]" }, /* @__PURE__ */ react.default.createElement("div", { className: "mb-4 flex items-center justify-between" }, /* @__PURE__ */ react.default.createElement("button", {
		className: "flex h-10 w-10 items-center justify-center rounded-2xl bg-[#fafafa] ring-1 ring-black/8 hover:bg-zinc-50",
		onClick: () => setMonthCursor(monthCursor.subtract(1, "month"))
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
		type: "pisell2-chevron-left",
		style: { fontSize: 16 }
	})), /* @__PURE__ */ react.default.createElement("div", { className: "text-sm font-bold text-zinc-900" }, require_utils.getMonthLabel(monthCursor)), /* @__PURE__ */ react.default.createElement("button", {
		className: "flex h-10 w-10 items-center justify-center rounded-2xl bg-[#fafafa] ring-1 ring-black/8 hover:bg-zinc-50",
		onClick: () => setMonthCursor(monthCursor.add(1, "month"))
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
		type: "pisell2-chevron-right",
		style: { fontSize: 16 }
	}))), /* @__PURE__ */ react.default.createElement("div", { className: "mb-2 grid grid-cols-7 gap-2 px-1 text-center text-[11px] font-bold uppercase tracking-wide text-zinc-400" }, [
		_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.mon") || "周一",
		_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.tue") || "周二",
		_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.wed") || "周三",
		_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.thu") || "周四",
		_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.fri") || "周五",
		_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.sat") || "周六",
		_pisell_utils.locales.getText("pisell2.venue.calendar.weekday.short.sun") || "周日"
	].map((d) => /* @__PURE__ */ react.default.createElement("div", { key: d }, d))), /* @__PURE__ */ react.default.createElement("div", { className: "grid grid-cols-7 gap-2" }, days.map((day, index) => {
		if (!day) return /* @__PURE__ */ react.default.createElement("div", {
			key: `empty-${index}`,
			className: "h-11"
		});
		const key = require_utils.formatDateKey(day);
		const meta = require_utils.getDateMetaFromSummary(day, viewModel.dateSummary);
		const selected = key === selectedKey;
		const isToday = key === todayKey;
		const isPastDate = day.isBefore(todayStart, "day");
		const isDisabledBySummary = !(!knownDateRange ? true : day.isBefore(knownDateRange.minDate, "day") || day.isAfter(knownDateRange.maxDate, "day")) && meta.soldOut;
		const isDisabled = isPastDate || isDisabledBySummary;
		const cellClass = isDisabled ? "bg-zinc-50 text-zinc-400 ring-1 ring-zinc-200" : selected ? "text-white shadow-sm" : isToday ? "" : "bg-[#fafafa] text-zinc-700 ring-1 ring-black/6";
		const cellStyle = selected && !meta.soldOut ? { backgroundColor: "var(--vb-primary)" } : isToday && !meta.soldOut ? {
			backgroundColor: "var(--vb-primary-light)",
			color: "var(--vb-primary)"
		} : void 0;
		return /* @__PURE__ */ react.default.createElement("button", {
			key,
			onClick: () => {
				if (isDisabled) return;
				handleSelect(day);
			},
			disabled: isDisabled,
			className: `h-11 rounded-2xl text-sm font-semibold transition ${cellClass}`,
			style: cellStyle
		}, /* @__PURE__ */ react.default.createElement("span", { className: isDisabledBySummary ? "line-through text-zinc-400" : "" }, day.date()));
	})));
}
//#endregion
exports.default = MonthPicker;
