const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_bookingToReservationTables = require("./bookingToReservationTables.js");
const require_formResourceToReservationTables = require("./formResourceToReservationTables.js");
//#region src/plus/pisellReservation/data/salesEmbeddedResourceToTableRows.ts
/**
* `sales.getResourceBookingList` 另一种返回形态：按桌（资源）一行，内含 `bookings[]`。
* 检测到该形态时直接生成平面图 `tables`，无需再与 form/resource/page + 扁平预约做合并。
*/
/** 宿主返回的单条「桌台 + 预约列表」是否像资源行（与扁平 schedule/booking 项区分） */
function osResourceRowLooksValid(o) {
	if (o.schedule_event_id != null && o.form_record_id == null && o.resource_id == null && typeof o.main_field !== "string") return false;
	return o.form_record_id != null || o.resource_id != null || typeof o.main_field === "string" || o.id != null && o.schedule_event_id == null;
}
function salesOsItemToFormResourcePageItem(o) {
	var _ref, _o$form_record_id;
	const idRaw = (_ref = (_o$form_record_id = o.form_record_id) !== null && _o$form_record_id !== void 0 ? _o$form_record_id : o.resource_id) !== null && _ref !== void 0 ? _ref : o.id;
	if (idRaw == null) return null;
	const fid = o.form_id;
	const cap = o.capacity;
	return {
		id: typeof idRaw === "number" ? idRaw : typeof idRaw === "string" ? idRaw : String(idRaw),
		form_id: typeof fid === "number" ? fid : fid != null && fid !== "" ? Number(fid) : void 0,
		main_field: typeof o.main_field === "string" ? o.main_field : void 0,
		form_code: typeof o.form_code === "string" ? o.form_code : void 0,
		capacity: typeof cap === "number" ? cap : cap != null && String(cap).trim() !== "" ? Number(cap) : void 0
	};
}
/**
* 是否为「资源行 + bookings」列表（与扁平预约项数组区分：后者一般无 `bookings` 数组字段）。
*/
function isSalesEmbeddedResourceBookingsList(items) {
	if (!Array.isArray(items) || items.length === 0) return false;
	const first = items[0];
	if (!first || typeof first !== "object") return false;
	const r = first;
	if (!Array.isArray(r.bookings)) return false;
	return osResourceRowLooksValid(r);
}
/** 从宿主 booking 项上摘平面图展示用补丁（payment / status 信号等） */
function patchRowFromHostBooking(row, hostBookings) {
	if (!hostBookings.length) return row;
	const b0 = hostBookings[0];
	const next = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, row), require_bookingToReservationTables.hostBookingUiPatchFromRecord(b0));
	const ord = b0.order;
	if ((ord === null || ord === void 0 ? void 0 : ord.payment_status) === "paid") next.isPaid = true;
	if (next.hostReservedStatus !== "late" && String(b0.reserved_status) === "late" && String(b0.status) !== "reserved") {
		next.hostReservedStatus = "late";
		if (next.lateMinutes == null) next.lateMinutes = 1;
	}
	return next;
}
/**
* `getResourceBookingList` 返回的带 `bookings` 的资源列表 → `PisellReservationTableRow[]`
*/
function salesEmbeddedResourceListToTableRows(items) {
	const rows = [];
	for (const raw of items) {
		if (!raw || typeof raw !== "object") continue;
		const o = raw;
		const pageItem = salesOsItemToFormResourcePageItem(o);
		if (!pageItem) continue;
		const base = require_formResourceToReservationTables.formResourceItemToReservationRow(pageItem);
		if (!base) continue;
		const rawBookings = Array.isArray(o.bookings) ? o.bookings : [];
		const bookingRow = require_bookingToReservationTables.mergeBookingsForResourceRecord(base.id, rawBookings);
		if (bookingRow) {
			const merged = require_formResourceToReservationTables.mergeResourceRowsWithBookingRows([base], [bookingRow])[0];
			rows.push(patchRowFromHostBooking(merged, rawBookings));
		} else rows.push(require_bookingToReservationTables.applyHostResourceShellToBaseRow(base, o));
	}
	return rows;
}
//#endregion
exports.isSalesEmbeddedResourceBookingsList = isSalesEmbeddedResourceBookingsList;
exports.salesEmbeddedResourceListToTableRows = salesEmbeddedResourceListToTableRows;
