function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React from 'react';
import classNames from 'classnames';
import { FLOOR_MAP_BUILTIN_SHAPE_ELEMENT_KIND, FLOOR_MAP_IMAGE_ELEMENT_KIND, findFloorMapRowByDataBinding, getFloorMapBindingPlaceholderReason, renderFloorMapFallbackPlaceholder, resolveSceneElementDisplayTitle } from '@pisell/materials';
import { locales } from '@pisell/utils';
import { RESERVATION_BOUND_ROUND_TABLE_ELEMENT_KIND, RESERVATION_BOUND_TABLE_ELEMENT_KIND } from "./reservationMock";
import { displayRoomTitleFromRow, mapPisellReservationRowToFloorRoomCardData, ReservationFloorRoomCard } from "../floorRoomCard";
import "./ReservationCards.less";
var I18N_BOUND = 'pisell-reservation.room-kind-bound';
var I18N_CARD_HINT_SELECT_ROW = 'pisell-reservation.card-placeholder.hint-select-row';
var I18N_CARD_HINT_BIND_DATASOURCE = 'pisell-reservation.card-placeholder.hint-bind-datasource';
var I18N_CARD_HINT_NEW_SLOT = 'pisell-reservation.card-placeholder.hint-new-slot';
var I18N_CARD_HINT_ROW_NOT_FOUND = 'pisell-reservation.card-placeholder.hint-row-not-found';
export function findReservationRow(item, dataSources) {
  return findFloorMapRowByDataBinding(item, dataSources);
}
function getReservationPlaceholderCopy(item, dataSources) {
  var el = item._sceneElement;
  var kindLabel = locales.getText(I18N_BOUND);
  var nameText = (el === null || el === void 0 ? void 0 : el.name) != null ? String(el.name).trim() : '';
  var title = nameText || kindLabel;
  var reason = getFloorMapBindingPlaceholderReason(item, dataSources);
  var 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);
  }
  var shape = item.elementKind === RESERVATION_BOUND_ROUND_TABLE_ELEMENT_KIND || item.shape === 'circle' ? 'circle' : 'rect';
  return {
    title: title,
    hint: hint,
    shape: shape
  };
}
function ReservationNoDataPlaceholder(props) {
  var title = props.title,
    hint = props.hint,
    shape = props.shape;
  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 === RESERVATION_BOUND_ROUND_TABLE_ELEMENT_KIND || item.shape === 'circle' ? 'circle' : 'rect';
}
function renderReservationBoundRoomCardInner(item, dataSources, options) {
  var _item$_sceneElement, _row$completeBookingC;
  var row = findReservationRow(item, dataSources);
  var cardShape = boundTableCardShape(item);
  if (!row) {
    var ph = getReservationPlaceholderCopy(item, dataSources);
    return /*#__PURE__*/React.createElement(ReservationNoDataPlaceholder, {
      title: ph.title,
      hint: ph.hint,
      shape: ph.shape
    });
  }
  var sceneName = ((_item$_sceneElement = item._sceneElement) === null || _item$_sceneElement === void 0 ? void 0 : _item$_sceneElement.name) != null ? String(item._sceneElement.name).trim() : undefined;
  /**
   * 「按业务字段算出来的房间标题」与历史一致：先 main_field、再 code、最后 sceneName 兜底。
   * 不动 displayRoomTitleFromRow 本身，避免下游（bookingDetailModal 等）被联动改坏。
   */
  var recordTitle = displayRoomTitleFromRow(row, sceneName);
  /**
   * 若图元上勾选了「将图元名作为资源标题」，由 resolveSceneElementDisplayTitle 统一覆盖；
   * 否则 fallback 链为 recordTitle → row.code → I18N_BOUND，与历史行为等价。
   */
  var cardTitle = resolveSceneElementDisplayTitle({
    sceneElement: item._sceneElement,
    recordTitle: recordTitle,
    fallback: row.code || locales.getText(I18N_BOUND)
  });
  var data = mapPisellReservationRowToFloorRoomCardData(row, cardTitle);
  /**
   * 资源卡标题旁拼「接口返回的订单计数」：`资源名 (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
   *   等用到的原始标题不受影响。
   */
  var dataWithCount = row.bookingCount != null && row.bookingCount > 0 ? _objectSpread(_objectSpread({}, data), {}, {
    bookingCount: row.bookingCount,
    completeBookingCount: (_row$completeBookingC = row.completeBookingCount) !== null && _row$completeBookingC !== void 0 ? _row$completeBookingC : 0
  }) : data;
  var 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 : undefined,
    viewportScale: options === null || options === void 0 ? void 0 : options.viewportScale
  });
}
export function renderReservationTableCard(item, dataSources) {
  return renderReservationBoundRoomCardInner(item, dataSources);
}
export function renderReservationPartyCard(item, dataSources) {
  return renderReservationBoundRoomCardInner(item, dataSources);
}
export function renderReservationBarCard(item, dataSources) {
  return renderReservationBoundRoomCardInner(item, dataSources);
}
export function getReservationRenderItemByKind(dataSources) {
  var bound = function bound(item, _index, options) {
    return renderReservationBoundRoomCardInner(item, dataSources, options);
  };
  return _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, FLOOR_MAP_IMAGE_ELEMENT_KIND, function (item, _index, _options) {
    return renderFloorMapFallbackPlaceholder(item);
  }), FLOOR_MAP_BUILTIN_SHAPE_ELEMENT_KIND, function (item, _index, _options) {
    return renderFloorMapFallbackPlaceholder(item);
  }), RESERVATION_BOUND_TABLE_ELEMENT_KIND, bound), RESERVATION_BOUND_ROUND_TABLE_ELEMENT_KIND, bound), "reservationTable", bound), "reservationPartyBench", bound), "reservationBarSeat", bound);
}