const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
//#region src/plus/pisellReservation/data/formResourceToReservationTables.ts
function normalizeMainField(raw) {
	var _first$trim;
	if (raw == null || typeof raw !== "string") return "";
	const first = raw.split(" ## ")[0];
	return (_first$trim = first === null || first === void 0 ? void 0 : first.trim()) !== null && _first$trim !== void 0 ? _first$trim : "";
}
/** 解析 form/resource/page 项的主键，与预约 resources[].relation_id 对齐 */
function normalizeFormResourceRecordId(item) {
	const raw = item.id;
	if (typeof raw === "number" && Number.isFinite(raw)) {
		const n = Math.trunc(raw);
		if (n > 0) return String(n);
		return null;
	}
	if (typeof raw === "string") {
		const t = raw.trim();
		if (!t || t === "0") return null;
		const n = Number(t);
		if (Number.isFinite(n) && n > 0) return String(Math.trunc(n));
		if (/^\d+$/.test(t)) return t;
	}
	return null;
}
/**
* 单条资源记录 → 桌位行（平面图 dataBinding.recordId 与 id 对齐）
*/
function formResourceItemToReservationRow(item) {
	if (item == null || typeof item !== "object") return null;
	const id = normalizeFormResourceRecordId(item);
	if (id == null) return null;
	return {
		id,
		code: normalizeMainField(item.main_field) || id,
		main_field: typeof item.main_field === "string" ? item.main_field : void 0,
		cardStatus: "available",
		area: typeof item.form_code === "string" && item.form_code.trim() ? item.form_code : void 0,
		capacityLabel: item.capacity != null && String(item.capacity).trim() !== "" ? String(item.capacity) : void 0
	};
}
/**
* 资源列表 → 桌位数据源
*/
function formResourceListToReservationTableRows(list) {
	if (!Array.isArray(list) || list.length === 0) return [];
	const rows = [];
	for (const raw of list) {
		const row = formResourceItemToReservationRow(raw);
		if (row) rows.push(row);
	}
	return rows;
}
/**
* 将资源行与「按 resource id 聚合」的预约行合并：保留资源侧 id / code / main_field / area / capacity，
* 覆盖预约卡展示的 cardStatus、客人、时段、slots 等（平面图 dataBinding.recordId 与 id 对齐）。
*/
function mergeResourceRowsWithBookingRows(resourceRows, bookingRows) {
	if (resourceRows.length === 0) return [];
	const byId = /* @__PURE__ */ new Map();
	for (const r of bookingRows) byId.set(r.id, r);
	return resourceRows.map((base) => {
		const b = byId.get(base.id);
		if (!b) return base;
		return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, base), {}, {
			cardStatus: b.cardStatus,
			guestName: b.guestName,
			guestPhone: b.guestPhone,
			time: b.time,
			timeEnd: b.timeEnd,
			partySize: b.partySize,
			startsInMinutes: b.startsInMinutes,
			priceDisplay: b.priceDisplay,
			isPaid: b.isPaid,
			delayMinutes: b.delayMinutes,
			lateMinutes: b.lateMinutes,
			elapsedLabel: b.elapsedLabel,
			progress: b.progress,
			progressTone: b.progressTone,
			topAccentOrange: b.topAccentOrange,
			nextGuest: b.nextGuest,
			nextTime: b.nextTime,
			slots: b.slots,
			moreCount: b.moreCount,
			barSlots: b.barSlots,
			barRemainingLabel: b.barRemainingLabel,
			floorMapSourceBookings: b.floorMapSourceBookings,
			floorMapReferenceDay: b.floorMapReferenceDay,
			hostReservedStatus: b.hostReservedStatus,
			hostIsTimeout: b.hostIsTimeout
		});
	});
}
//#endregion
exports.formResourceItemToReservationRow = formResourceItemToReservationRow;
exports.formResourceListToReservationTableRows = formResourceListToReservationTableRows;
exports.mergeResourceRowsWithBookingRows = mergeResourceRowsWithBookingRows;
