import dayjs from "dayjs";
//#region src/components/eventBooking/components/bookingAvailabilityCalendar/components/CalendarDate/state.ts
/**
* @title: 日历星期多语言
* @description:
* @param {*}
* @return {*}
* @Author: 严文强
* @Date: 2024-02-01 18:42
*/
const weekListText = [
	"Su",
	"Mo",
	"Tu",
	"We",
	"Th",
	"Fr",
	"Sa"
];
/**
* @title: 不同状态颜色值
* @description:
* @param {*}
* @return {*}
* @Author: xiangxiang.shao
* @Date: 2024-02-19 11:35
*/
const statusColor = {
	call_to_book: "#444CE7",
	lots_of_space: "#099250",
	filling_up_fast: "#EAAA08",
	sold_out: "#D92D20",
	unavailable: "rgba(68, 76, 231, 0.00)"
};
/**
* @title: 根据预约量数据判断当前状态
* @description:
* @param {*}
* @return {*}
* @Author: xiangxiang.shao
* @Date: 2024-02-19 11:35
*/
const getStatus = (dateLimit, currentDate, cutTime) => {
	let status = "";
	let disabled = false;
	const dataItem = dateLimit[currentDate];
	console.log(currentDate);
	const startTime = dayjs(cutTime === null || cutTime === void 0 ? void 0 : cutTime.shop_time).format("YYYY-MM-DD HH:mm");
	const startDate = dayjs(cutTime === null || cutTime === void 0 ? void 0 : cutTime.shop_time).format("YYYY-MM-DD");
	dayjs(startTime).add(cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit, cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit_type).format("YYYY-MM-DD");
	try {
		if (!dataItem) return {
			status,
			disabled: true
		};
	} catch (err) {
		return {
			status,
			disabled: true
		};
	}
	if (!Object.keys(dateLimit).length) return {
		status,
		disabled: true
	};
	const times = dataItem.times || [];
	if (!times.length) disabled = true;
	else {
		let useable = null;
		if (cutTime.type === "ongoing" && currentDate === startDate) {
			var _cutTime$ongoing, _cutTime$ongoing2;
			if ((cutTime === null || cutTime === void 0 || (_cutTime$ongoing = cutTime["ongoing"]) === null || _cutTime$ongoing === void 0 ? void 0 : _cutTime$ongoing.type) === "before_end") useable = times.find((d) => {
				return startTime >= d.start_at && startTime < d.end_at || d.start_at >= startTime;
			});
			if ((cutTime === null || cutTime === void 0 || (_cutTime$ongoing2 = cutTime["ongoing"]) === null || _cutTime$ongoing2 === void 0 ? void 0 : _cutTime$ongoing2.type) === "after_start") useable = times.find((d) => {
				var _cutTime$ongoing3;
				const _endTime = dayjs(d.start_at).add(cutTime === null || cutTime === void 0 || (_cutTime$ongoing3 = cutTime["ongoing"]) === null || _cutTime$ongoing3 === void 0 ? void 0 : _cutTime$ongoing3.unit, "minutes").format("YYYY-MM-DD HH:mm");
				return startTime >= d.start_at && startTime <= _endTime || d.start_at >= startTime;
			});
		} else useable = times.find((d) => {
			return startTime < d.start_at;
		});
		if (!useable) return {
			status,
			disabled: true
		};
		if (times.find((item) => item.limit === 0)) status = "lots_of_space";
		else {
			const allLimit = times.reduce((prev, next) => {
				return prev + next.limit;
			}, 0);
			const percent = (allLimit - times.reduce((prev, next) => {
				return prev + next.order_count;
			}, 0)) / allLimit;
			if (percent > .5) status = "lots_of_space";
			else if (percent <= .5 && percent > 0) status = "filling_up_fast";
			else if (percent <= 0) status = "sold_out";
		}
	}
	return {
		status,
		disabled
	};
};
/**
* @title: 根据当前日期以设定判断当前是否是call_to_book状态
* @description:
* @param {*}
* @return {*}
* @Author: xiangxiang.shao
* @Date: 2024-02-21 10:10
*/
const getCallToBookStatus = (currentDate, dateLimitData, cutTime) => {
	var _dateLimitData$curren;
	const timeList = ((_dateLimitData$curren = dateLimitData[currentDate]) === null || _dateLimitData$curren === void 0 ? void 0 : _dateLimitData$curren.times) || [];
	let isShowCallToBook = false;
	if (!currentDate || !(cutTime === null || cutTime === void 0 ? void 0 : cutTime.shop_time) || !(cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit) || !(cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit_type)) {
		isShowCallToBook = false;
		return isShowCallToBook;
	}
	if ((cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit_type) === "hours" || (cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit_type) === "minutes" || (cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit_type) === "days") {
		const endTime = dayjs(dayjs().format("YYYY-MM-DD HH:mm:ss")).add(cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit, cutTime === null || cutTime === void 0 ? void 0 : cutTime.unit_type).format("YYYY-MM-DD HH:mm:ss");
		if (dayjs(currentDate).isBefore(dayjs(endTime.split(" ")[0]))) isShowCallToBook = true;
		else if (currentDate === endTime.split(" ")[0]) {
			var _timeList$find;
			if (timeList === null || timeList === void 0 || (_timeList$find = timeList.find) === null || _timeList$find === void 0 ? void 0 : _timeList$find.call(timeList, (item) => {
				if (dayjs(`${item.date} ${item.end_at}`).valueOf() > dayjs(endTime).valueOf()) return true;
			})) isShowCallToBook = false;
			else isShowCallToBook = true;
		} else isShowCallToBook = false;
	}
	return isShowCallToBook;
};
//#endregion
export { getCallToBookStatus, getStatus, statusColor, weekListText };
