import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { displayRoomTitleFromRow } from "../floorRoomCard/floorRoomCardUtils.js";
import { mapPisellReservationRowToFloorRoomCardData } from "../floorRoomCard/mapRowToFloorRoomCardData.js";
import { ReservationFloorRoomCard } from "../floorRoomCard/ReservationFloorRoomCard.js";
import { RESERVATION_BOUND_ROUND_TABLE_ELEMENT_KIND, RESERVATION_BOUND_TABLE_ELEMENT_KIND } from "./reservationMock.js";
import React from "react";
import { locales } from "@pisell/utils";
import { FLOOR_MAP_BUILTIN_SHAPE_ELEMENT_KIND, FLOOR_MAP_IMAGE_ELEMENT_KIND, findFloorMapRowByDataBinding, getFloorMapBindingPlaceholderReason, renderFloorMapFallbackPlaceholder, resolveSceneElementDisplayTitle } from "@pisell/materials";
import classNames from "classnames";
import "./ReservationCards.less";
//#region src/plus/pisellReservation/floorMap/reservationCards.tsx
const I18N_BOUND = "pisell-reservation.room-kind-bound";
const I18N_CARD_HINT_SELECT_ROW = "pisell-reservation.card-placeholder.hint-select-row";
const I18N_CARD_HINT_BIND_DATASOURCE = "pisell-reservation.card-placeholder.hint-bind-datasource";
const I18N_CARD_HINT_NEW_SLOT = "pisell-reservation.card-placeholder.hint-new-slot";
const I18N_CARD_HINT_ROW_NOT_FOUND = "pisell-reservation.card-placeholder.hint-row-not-found";
function findReservationRow(item, dataSources) {
	return findFloorMapRowByDataBinding(item, dataSources);
}
function getReservationPlaceholderCopy(item, dataSources) {
	const el = item._sceneElement;
	const kindLabel = locales.getText(I18N_BOUND);
	const title = ((el === null || el === void 0 ? void 0 : el.name) != null ? String(el.name).trim() : "") || kindLabel;
	const reason = getFloorMapBindingPlaceholderReason(item, dataSources);
	let hint;
	switch (reason) {
		case "missing_data_source_key":
			hint = locales.getText(I18N_CARD_HINT_BIND_DATASOURCE);
			break;
		case "missing_record_id":
			hint = locales.getText(I18N_CARD_HINT_SELECT_ROW);
			break;
		case "new_unsaved_slot":
			hint = locales.getText(I18N_CARD_HINT_NEW_SLOT);
			break;
		default: hint = locales.getText(I18N_CARD_HINT_ROW_NOT_FOUND);
	}
	const shape = item.elementKind === "reservationBoundRoundTable" || item.shape === "circle" ? "circle" : "rect";
	return {
		title,
		hint,
		shape
	};
}
function ReservationNoDataPlaceholder(props) {
	const { title, hint, shape } = props;
	return /* @__PURE__ */ React.createElement("div", { className: classNames("pisell-reservation-card-placeholder", shape === "circle" && "pisell-reservation-card-placeholder--circle") }, /* @__PURE__ */ React.createElement("div", { className: "pisell-reservation-card-placeholder-title" }, title), hint ? /* @__PURE__ */ React.createElement("div", { className: "pisell-reservation-card-placeholder-hint" }, hint) : null);
}
function boundTableCardShape(item) {
	return item.elementKind === "reservationBoundRoundTable" || item.shape === "circle" ? "circle" : "rect";
}
function renderReservationBoundRoomCardInner(item, dataSources, options) {
	var _item$_sceneElement, _row$completeBookingC;
	const row = findReservationRow(item, dataSources);
	const cardShape = boundTableCardShape(item);
	if (!row) {
		const ph = getReservationPlaceholderCopy(item, dataSources);
		return /* @__PURE__ */ React.createElement(ReservationNoDataPlaceholder, {
			title: ph.title,
			hint: ph.hint,
			shape: ph.shape
		});
	}
	/**
	* 「按业务字段算出来的房间标题」与历史一致：先 main_field、再 code、最后 sceneName 兜底。
	* 不动 displayRoomTitleFromRow 本身，避免下游（bookingDetailModal 等）被联动改坏。
	*/
	const recordTitle = displayRoomTitleFromRow(row, ((_item$_sceneElement = item._sceneElement) === null || _item$_sceneElement === void 0 ? void 0 : _item$_sceneElement.name) != null ? String(item._sceneElement.name).trim() : void 0);
	const data = mapPisellReservationRowToFloorRoomCardData(row, resolveSceneElementDisplayTitle({
		sceneElement: item._sceneElement,
		recordTitle,
		fallback: row.code || locales.getText(I18N_BOUND)
	}));
	/**
	* 资源卡标题旁拼「接口返回的订单计数」：`资源名 (N/M)`。
	* - N：`row.completeBookingCount`（已完成数），缺失按 0 展示；
	* - M：`row.bookingCount`（总数），来自 `salesEmbeddedResourceListToTableRows`
	*   从 `sales.getResourceBookingList` 资源行顶层 `bookingCount`；
	* - 仅在 `M > 0` 时整体渲染，避免空态卡出现 `(0/0)`；
	* - 不再用 `data.bookings.length` 自行换算（语义上「订单数」可能 ≠ 平面图当前
	*   时刻可见的 bookings 数，例如已结账 / 未付款也算单但不渲染卡）；
	* - **写法上不拼到 `main_field` 末尾**：标题字符串在大卡 header 里会随
	*   宽度 ellipsis，先拼会导致长资源名（如 `小包 01（10 人）`）把 `(N/M)`
	*   一起裁掉；改为通过结构化字段 `data.bookingCount` /
	*   `data.completeBookingCount` 透传，由 {@link RoomCardHeaderView}
	*   渲染为独立 `flex-shrink: 0` 节点，保证计数常驻可见；
	* - 入口集中：所有 floor 资源卡（方桌 / 圆桌 / partyBench / barSeat 以及
	*   通用 `bound` kind）都从 {@link renderReservationBoundRoomCardInner} 走；
	* - 不回写 row 或 sceneElement，`displayRoomTitleFromRow` / detailModal
	*   等用到的原始标题不受影响。
	*/
	const dataWithCount = row.bookingCount != null && row.bookingCount > 0 ? _objectSpread2(_objectSpread2({}, data), {}, {
		bookingCount: row.bookingCount,
		completeBookingCount: (_row$completeBookingC = row.completeBookingCount) !== null && _row$completeBookingC !== void 0 ? _row$completeBookingC : 0
	}) : data;
	const layoutH = item.height != null && Number.isFinite(item.height) && item.height > 0 ? item.height : item.width;
	return /* @__PURE__ */ React.createElement(ReservationFloorRoomCard, {
		data: dataWithCount,
		shellShape: cardShape,
		hideHeaderCapacity: true,
		elementLayoutHeightPx: layoutH != null && Number.isFinite(layoutH) && layoutH > 0 && cardShape === "circle" ? layoutH : void 0,
		viewportScale: options === null || options === void 0 ? void 0 : options.viewportScale
	});
}
function getReservationRenderItemByKind(dataSources) {
	const bound = (item, _index, options) => renderReservationBoundRoomCardInner(item, dataSources, options);
	return {
		[FLOOR_MAP_IMAGE_ELEMENT_KIND]: (item, _index, _options) => renderFloorMapFallbackPlaceholder(item),
		[FLOOR_MAP_BUILTIN_SHAPE_ELEMENT_KIND]: (item, _index, _options) => renderFloorMapFallbackPlaceholder(item),
		[RESERVATION_BOUND_TABLE_ELEMENT_KIND]: bound,
		[RESERVATION_BOUND_ROUND_TABLE_ELEMENT_KIND]: bound,
		reservationTable: bound,
		reservationPartyBench: bound,
		reservationBarSeat: bound
	};
}
//#endregion
export { findReservationRow, getReservationRenderItemByKind };
