import { bookingListToReservationTableRows } from "./bookingToReservationTables.js";
import { formResourceListToReservationTableRows, mergeResourceRowsWithBookingRows } from "./formResourceToReservationTables.js";
import { isSalesEmbeddedResourceBookingsList, salesEmbeddedResourceListToTableRows } from "./salesEmbeddedResourceToTableRows.js";
//#region src/plus/pisellReservation/data/reservationTablesMerge.ts
/**
* 将宿主 `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) {
		/**
		* 低代码若预置非空 tables，此处会直接返回，**不会**与 floorDay 预约合并。
		* 该提示仅在排查画布无数据时有用，落生产 / 实时推送热路径会成为序列化大对象的瓶颈，
		* 故仅在非生产构建下输出。
		*/
		if (process.env.NODE_ENV !== "production") console.warn("[buildReservationMergedTableRows] 使用 dataSourcesProp 已有 tables，跳过资源+预约合并", {
			gridDataSourceKey,
			fromPropLength: fromProp.length,
			floorDayBookingCount: Array.isArray(floorDayBookingList) ? floorDayBookingList.length : 0,
			hint: "若画布/合并行与接口不一致，请清空或删除引擎里该 dataSource key 的默认 tables，或改为空数组。"
		});
		return fromProp;
	}
	if (isInternalListData) {
		/**
		* 宿主 `getResourceBookingList` 若返回「每行一桌位 + bookings[]」，与扁平预约数组区分，
		* 直接生成 tables（id/main_field 与 OS 一致，预约已挂在该行下）。
		*/
		if (salesResourceBookings != null && isSalesEmbeddedResourceBookingsList(salesResourceBookings)) return salesEmbeddedResourceListToTableRows(salesResourceBookings);
		const resourceRows = formResourceListToReservationTableRows(resourceList);
		const bookingRows = bookingListToReservationTableRows(salesResourceBookings != null ? salesResourceBookings : floorDayBookingList);
		const merged = mergeResourceRowsWithBookingRows(resourceRows, bookingRows);
		/**
		* 该 merge 在 WS 推送链路里每次都会跑；`merged.find(...)` + DevTools 序列化整个 row
		* 单次 100ms+，是「推送 → 画布」延迟的主要来源。仅在非生产下保留排查日志。
		*/
		if (process.env.NODE_ENV !== "production") console.log("[buildReservationMergedTableRows] 已合并 resource + booking", {
			gridDataSourceKey,
			resourceRowCount: resourceRows.length,
			bookingRowCount: bookingRows.length,
			mergedRowCount: merged.length
		});
		return merged;
	}
	return bookingListToReservationTableRows(dataProp !== null && dataProp !== void 0 ? dataProp : []);
}
//#endregion
export { buildReservationMergedTableRows, normalizeSalesResourceBookingListResult };
