import { bookingListToReservationTableRows } from "./bookingToReservationTables.js";
import { filterBookingsVisibleAtTimeCursor } from "./filterBookingsAtTimeCursor.js";
import { formResourceListToReservationTableRows, mergeResourceRowsWithBookingRows } from "./formResourceToReservationTables.js";
import { isSalesEmbeddedResourceBookingsList, salesEmbeddedResourceListToTableRows } from "./salesEmbeddedResourceToTableRows.js";
import dayjs from "dayjs";
//#region src/plus/pisellReservation/data/reservationTablesMerge.ts
/**
* 内置列表模式下：资源台位行（每张桌 id=平面图 dataBinding.recordId） +
* 「当前时刻」预约行（宿主 getResourceBookingList 或本地 filterBookingsVisibleAtTimeCursor）
* → merge 成平面图 dataSources.tables，渲染时无需再查表。
*/
/**
* 将宿主 `sales.getResourceBookingList` 的返回值规范为 booking 项数组（便于走 bookingListToReservationTableRows）
*/
function normalizeSalesResourceBookingListResult(raw) {
	if (raw == null) return [];
	if (Array.isArray(raw)) return raw;
	if (typeof raw === "object") {
		const o = raw;
		if (Array.isArray(o.list)) return o.list;
		if (Array.isArray(o.data)) return o.data;
		if (Array.isArray(o.records)) return o.records;
	}
	return [];
}
function buildReservationMergedTableRows(options) {
	const { dataSourcesProp, gridDataSourceKey, isInternalListData, resourceList, floorDayBookingList, schedule, dataProp, salesResourceBookings } = options;
	const fromProp = dataSourcesProp === null || dataSourcesProp === void 0 ? void 0 : dataSourcesProp[gridDataSourceKey];
	if (Array.isArray(fromProp) && fromProp.length > 0) return fromProp;
	if (isInternalListData) {
		/**
		* 宿主 `getResourceBookingList` 若返回「每行一桌位 + bookings[]」，与扁平预约数组区分，
		* 直接生成 tables（id/main_field 与 OS 一致，预约已挂在该行下）。
		*/
		if (salesResourceBookings != null && isSalesEmbeddedResourceBookingsList(salesResourceBookings)) return salesEmbeddedResourceListToTableRows(salesResourceBookings);
		const resourceRows = formResourceListToReservationTableRows(resourceList);
		const cursorAt = schedule.at != null && dayjs(schedule.at).isValid() ? dayjs(schedule.at) : dayjs();
		return mergeResourceRowsWithBookingRows(resourceRows, bookingListToReservationTableRows(salesResourceBookings != null ? salesResourceBookings : filterBookingsVisibleAtTimeCursor(floorDayBookingList, cursorAt)));
	}
	return bookingListToReservationTableRows(dataProp !== null && dataProp !== void 0 ? dataProp : []);
}
//#endregion
export { buildReservationMergedTableRows, normalizeSalesResourceBookingListResult };
