import { resolveResourceIdFromRow } from "./bookingDetailHostActions.js";
import { EMPTY_RESOURCE_BOOKING_ITEM_LISTS } from "./bookingDetailModalConstants.js";
import dayjs from "dayjs";
import { useEffect, useMemo, useRef, useState } from "react";
//#region src/plus/pisellReservation/components/bookingDetailModal/bookingDetailResourceBookingItem.ts
/**
* 把宿主 sales.getResourceBookingItem 任意返回值规范为三段子列表数组。
*/
function normalizeResourceBookingItemLists(raw) {
	if (raw == null || typeof raw !== "object") return EMPTY_RESOURCE_BOOKING_ITEM_LISTS;
	const r = raw;
	const arr = (v) => Array.isArray(v) ? v : [];
	return {
		bookings: arr(r.bookings),
		completedBookings: arr(r.completedBookings),
		unpaidBookings: arr(r.unpaidBookings)
	};
}
/**
* 打开弹窗时拉取 sales.getResourceBookingItem，并按 activeTab 返回当前 Tab 的 bookings 列表。
*/
function useResourceBookingItem(activeTab, options) {
	const { open, raw, tableRow, floorDayBookingList, currentTime, followWallClock, pisellos } = options;
	const [resourceBookingItem, setResourceBookingItem] = useState(null);
	const [resourceBookingItemLoading, setResourceBookingItemLoading] = useState(false);
	const resourceBookingItemReqIdRef = useRef(0);
	useEffect(() => {
		if (!open || raw == null) {
			setResourceBookingItem(null);
			setResourceBookingItemLoading(false);
			resourceBookingItemReqIdRef.current += 1;
		}
	}, [open, raw]);
	useEffect(() => {
		if (!open || raw == null) return;
		const getModule = pisellos === null || pisellos === void 0 ? void 0 : pisellos.getModule;
		if (typeof getModule !== "function") return;
		let salesModule;
		try {
			salesModule = getModule.call(pisellos, "sales");
		} catch (_unused) {
			return;
		}
		const fn = salesModule === null || salesModule === void 0 ? void 0 : salesModule.getResourceBookingItem;
		if (typeof fn !== "function") return;
		const resourceId = resolveResourceIdFromRow(tableRow);
		if (resourceId == null) return;
		const fmt = "YYYY-MM-DD HH:mm";
		const currentTimeStr = currentTime ? dayjs(currentTime).format(fmt) : dayjs().format(fmt);
		/**
		* 与 useReservationSalesHostData 里 getResourceBookingList 的处理对齐：
		* - 跟随墙钟时 `deviceTime` 与 `currentTime` 相同，避免与 `dayjs()` 差一分钟；
		* - 关闭跟随后 `deviceTime` 走实时设备时间，保留「游标(业务) vs 此刻(设备)」双通道语义。
		*/
		const deviceTimeStr = followWallClock ? currentTimeStr : dayjs().format(fmt);
		const bookingList = Array.isArray(floorDayBookingList) ? floorDayBookingList : [];
		const reqId = ++resourceBookingItemReqIdRef.current;
		setResourceBookingItemLoading(true);
		const callParams = {
			resourceId,
			currentTime: currentTimeStr,
			bookingList,
			deviceTime: deviceTimeStr
		};
		const apply = (result) => {
			if (resourceBookingItemReqIdRef.current !== reqId) return;
			setResourceBookingItem(normalizeResourceBookingItemLists(result));
			setResourceBookingItemLoading(false);
		};
		const fail = () => {
			if (resourceBookingItemReqIdRef.current !== reqId) return;
			setResourceBookingItem(EMPTY_RESOURCE_BOOKING_ITEM_LISTS);
			setResourceBookingItemLoading(false);
		};
		try {
			const result = fn.call(salesModule, callParams);
			if (result != null && typeof result.then === "function") result.then(apply).catch(fail);
			else apply(result);
		} catch (_unused2) {
			fail();
		}
	}, [
		open,
		raw,
		tableRow,
		floorDayBookingList,
		currentTime,
		followWallClock,
		pisellos
	]);
	return {
		resourceBookingItem,
		resourceBookingItemLoading,
		activeTabBookings: useMemo(() => {
			if (!resourceBookingItem) return [];
			return (activeTab === "current-occupied" ? resourceBookingItem.bookings : activeTab === "ended" ? resourceBookingItem.completedBookings : resourceBookingItem.unpaidBookings).filter((x) => x != null && typeof x === "object");
		}, [resourceBookingItem, activeTab])
	};
}
//#endregion
export { useResourceBookingItem };
