import useEngineContext from "../../../../hooks/useEngineContext.js";
import CalendarItem from "./calendarItem.js";
import dayjs from "dayjs";
import React, { useEffect, useState } from "react";
import classNames from "classnames";
import "./index.less";
//#region src/components/eventBooking/components/scheduleCalendar/index.tsx
const EventScheduleCalendar = ({ data, showScheduleName = true, showScheduleTime = true, className, style, onChange, type = "calendarView" }) => {
	const utils = useEngineContext().appHelper.utils || {};
	const [selectedYear, setSelectedYear] = useState(dayjs().year());
	const [currentMonth, setCurrentMonth] = useState((/* @__PURE__ */ new Date()).toISOString().slice(0, 7));
	const [selectedDate, setSelectedDate] = useState("");
	const [selectedTimes, setSelectedTimes] = useState([]);
	/**
	* @title: 可用的日期数据
	* @description:
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-01-19 22:52
	*/
	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(dayjs(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 = dayjs(time).format("HH");
			const minutes = dayjs(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 dayjs(time).format("hh:mm a").toLowerCase();
		} catch (error) {}
	};
	useEffect(() => {
		onChange === null || onChange === void 0 || onChange(selectedYear);
	}, [selectedYear]);
	return /* @__PURE__ */ React.createElement("div", {
		className: classNames("schedule-calendar-wrap", className),
		style
	}, /* @__PURE__ */ React.createElement("div", { className: `month-item-box` }, /* @__PURE__ */ React.createElement(CalendarItem, {
		isEdit: false,
		month: currentMonth,
		schedulesData: data || [],
		selectDate: selectedDate,
		onSelect: handleSelectDate,
		onChange: (e, type) => {
			setCurrentMonth(e);
			dayjs().year();
			setSelectedYear(dayjs(e).year());
		},
		type
	})), type !== "textList" ? /* @__PURE__ */ React.createElement("div", { className: classNames("category-list") }, selectedTimes.map((d, i) => {
		return /* @__PURE__ */ React.createElement("div", {
			className: "category-item",
			key: i
		}, showScheduleName ? /* @__PURE__ */ React.createElement("div", { className: "category-title" }, /* @__PURE__ */ React.createElement("div", { className: "category-icon" }, /* @__PURE__ */ React.createElement("span", null)), /* @__PURE__ */ React.createElement("span", null, d.name)) : null, showScheduleTime ? /* @__PURE__ */ React.createElement("div", { className: classNames("time") }, formatDateStr(d.start_at), " - ", formatDateStr(d.end_at)) : null);
	})) : null);
};
//#endregion
export { EventScheduleCalendar as default };
