const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_calendarItem = require("./calendarItem.js");
let dayjs = require("dayjs");
dayjs = require_runtime.__toESM(dayjs);
let react = require("react");
react = require_runtime.__toESM(react);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/components/eventBooking/components/scheduleCalendar/index.tsx
const EventScheduleCalendar = ({ data, showScheduleName = true, showScheduleTime = true, className, style, onChange, type = "calendarView" }) => {
	const utils = require_useEngineContext.default().appHelper.utils || {};
	const [selectedYear, setSelectedYear] = (0, react.useState)((0, dayjs.default)().year());
	const [currentMonth, setCurrentMonth] = (0, react.useState)((/* @__PURE__ */ new Date()).toISOString().slice(0, 7));
	const [selectedDate, setSelectedDate] = (0, react.useState)("");
	const [selectedTimes, setSelectedTimes] = (0, react.useState)([]);
	/**
	* @title: 可用的日期数据
	* @description:
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-19 22:52
	*/
	(0, react.useEffect)(() => {
		if (data === null || data === void 0 ? void 0 : data.length) {
			const today = /* @__PURE__ */ new Date();
			today.setHours(0, 0, 0, 0);
			const firstAvailableDate = data.find((item) => {
				return new Date(item.date) >= today;
			});
			if (firstAvailableDate && (firstAvailableDate === null || firstAvailableDate === void 0 ? void 0 : firstAvailableDate.date)) {
				const availableDate = firstAvailableDate.date;
				const availableTimes = (firstAvailableDate === null || firstAvailableDate === void 0 ? void 0 : firstAvailableDate.times) || [];
				setSelectedYear((0, dayjs.default)(availableDate).year());
				setCurrentMonth(new Date(availableDate).toISOString().slice(0, 7));
				setSelectedDate(availableDate);
				setSelectedTimes(availableTimes || []);
			}
		}
	}, [data]);
	/**
	* @title: 选中当前日期
	* @description:
	* @param {string} date
	* @param {CalendarDataItem} item
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-18 09:53
	*/
	const handleSelectDate = (date, item) => {
		const dateData = data === null || data === void 0 ? void 0 : data.find((d) => d.date === date);
		if (dateData) {
			setSelectedDate(date);
			setSelectedTimes(dateData.times || []);
		}
	};
	/**
	* @title: 格式化时间段
	* @description:
	* @param {any} time
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-21 16:54
	*/
	const formatDateStr = (time) => {
		try {
			var _utils$storage, _locale$toLowerCase;
			const locale = (utils === null || utils === void 0 || (_utils$storage = utils.storage) === null || _utils$storage === void 0 ? void 0 : _utils$storage.get("umi_locale")) || "en";
			const hour24 = (0, dayjs.default)(time).format("HH");
			const minutes = (0, dayjs.default)(time).format("mm");
			if ((_locale$toLowerCase = locale.toLowerCase()) === null || _locale$toLowerCase === void 0 ? void 0 : _locale$toLowerCase.includes("zh")) return `${Number(hour24) < 12 ? "上午" : "下午"} ${Number(hour24) % 12 || 12}:${minutes}`;
			return (0, dayjs.default)(time).format("hh:mm a").toLowerCase();
		} catch (error) {}
	};
	(0, react.useEffect)(() => {
		onChange === null || onChange === void 0 || onChange(selectedYear);
	}, [selectedYear]);
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("schedule-calendar-wrap", className),
		style
	}, /* @__PURE__ */ react.default.createElement("div", { className: `month-item-box` }, /* @__PURE__ */ react.default.createElement(require_calendarItem.default, {
		isEdit: false,
		month: currentMonth,
		schedulesData: data || [],
		selectDate: selectedDate,
		onSelect: handleSelectDate,
		onChange: (e, type) => {
			setCurrentMonth(e);
			(0, dayjs.default)().year();
			setSelectedYear((0, dayjs.default)(e).year());
		},
		type
	})), type !== "textList" ? /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("category-list") }, selectedTimes.map((d, i) => {
		return /* @__PURE__ */ react.default.createElement("div", {
			className: "category-item",
			key: i
		}, showScheduleName ? /* @__PURE__ */ react.default.createElement("div", { className: "category-title" }, /* @__PURE__ */ react.default.createElement("div", { className: "category-icon" }, /* @__PURE__ */ react.default.createElement("span", null)), /* @__PURE__ */ react.default.createElement("span", null, d.name)) : null, showScheduleTime ? /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("time") }, formatDateStr(d.start_at), " - ", formatDateStr(d.end_at)) : null);
	})) : null);
};
//#endregion
exports.default = EventScheduleCalendar;
