function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
/**
 * 非 comfortable 极简态：独立 DOM（`.rc-tooltip-time-root`），不使用 `.rc-body` / `.rc-progress`。
 * 时间用 antd `Tooltip` 的 `title` 挂在桌名上，`open` 常驻以保持原先「始终可见」的小卡信息密度。
 */
import React from 'react';
import { Tooltip } from 'antd';
import classNames from 'classnames';
import { locales } from '@pisell/utils';
import { FLOOR_ROOM_CARD_DISC_EDGE_RING_ENABLED } from "../floorRoomCardDiscEdge";
import { paymentBadgeClassName } from "../buildCardBodyViewModel";
import { resolveFloorRoomCardProgressPercent } from "../floorRoomCardUtils";
import { floorRoomCardPaymentUiFromOrderStatus } from "../floorRoomCardPaymentUi";
var I18N_PAID = 'pisell-reservation.room-paid';
var I18N_PAYMENT_NEEDED = 'pisell-reservation.room-payment-needed';
var I18N_START = 'pisell-reservation.room-time-start';
var I18N_END = 'pisell-reservation.room-time-end';
function extractHmFromTimeField(raw) {
  var s = String(raw !== null && raw !== void 0 ? raw : '').trim();
  if (!s) return '';
  var parts = s.split(':');
  if (parts.length >= 2) {
    var h = parseInt(parts[0], 10);
    var minDigits = parts[1].replace(/\D/g, '').slice(0, 2);
    var mm = minDigits.length >= 2 ? minDigits.padStart(2, '0') : "".concat(minDigits, "0").slice(0, 2);
    if (Number.isFinite(h)) return "".concat(h, ":").concat(mm);
  }
  return s;
}

/**
 * 在标题尾部按 `(N/M)` 形态拼接订单计数：
 * - 仅在 `bookingCount > 0` 时拼；
 * - N = `completeBookingCount ?? 0`（缺失按 0 渲染，与 RoomCardHeaderView 行为一致）；
 * - M = `bookingCount`。
 * 拼到 base 字符串末尾。同源策略复用于多处资源标题：
 * - 小卡 tooltip 标题（`--overflow` 修饰允许越出卡片宽度显示）；
 * - 预约详情 Modal 标题（`ReservationBookingDetailModal`）。
 *
 * 入参用结构化 `BookingCountSource` 而非具体 `FloorRoomCardData`，让不同上游
 * （平面图卡片数据 / 表格行 `PisellReservationTableRow` 等）都能直接复用，
 * 避免在多处重复实现导致格式漂移。
 */

export function appendBookingCountSuffix(baseTitle, source) {
  var total = source.bookingCount;
  if (typeof total !== 'number' || !Number.isFinite(total) || total <= 0) {
    return baseTitle;
  }
  var complete = typeof source.completeBookingCount === 'number' && Number.isFinite(source.completeBookingCount) && source.completeBookingCount >= 0 ? source.completeBookingCount : 0;
  return "".concat(baseTitle, " (").concat(complete, "/").concat(total, ")");
}
function TooltipTimeProgressBar(props) {
  var raw = Number(props.percent);
  var safe = Number.isFinite(raw) ? raw : 0;
  var clamped = Math.min(100, Math.max(0, safe));
  return /*#__PURE__*/React.createElement("div", {
    className: "rc-tooltip-time-bar",
    "aria-hidden": true
  }, /*#__PURE__*/React.createElement("div", {
    className: "rc-tooltip-time-bar__track"
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('rc-tooltip-time-bar__fill', props.isTimeout && 'rc-tooltip-time-bar__fill--warn'),
    style: {
      width: "".concat(clamped, "%")
    }
  })));
}
export function RoomCardTooltipTimeLayout(props) {
  var _data$main_field;
  var data = props.data,
    booking = props.booking,
    cardType = props.cardType,
    _props$shellShape = props.shellShape,
    shellShape = _props$shellShape === void 0 ? 'rect' : _props$shellShape,
    _props$hideInnerLinea = props.hideInnerLinearProgress,
    hideInnerLinearProgress = _props$hideInnerLinea === void 0 ? false : _props$hideInnerLinea,
    headerMainFieldOverride = props.headerMainFieldOverride,
    scrollSurfaceHandlers = props.scrollSurfaceHandlers;
  var baseTitle = (headerMainFieldOverride === null || headerMainFieldOverride === void 0 ? void 0 : headerMainFieldOverride.trim()) || String((_data$main_field = data.main_field) !== null && _data$main_field !== void 0 ? _data$main_field : '').trim();
  /**
   * 小卡标题溢出场景下，直接把 `(N/M)` 拼到字符串末尾即可：tooltip-time 路径
   * 的 `--overflow` 修饰已允许文本越出卡片边界整体展示，不会因卡宽不够而被裁。
   * N = `completeBookingCount ?? 0`，M = `bookingCount`；仅 M > 0 时拼接。
   */
  var title = appendBookingCountSuffix(baseTitle, data);
  var isOccupied = cardType === 'payed' || cardType === 'payment_required' || cardType === 'timeout';
  var timeHmRaw = isOccupied ? extractHmFromTimeField(booking.end_time) || extractHmFromTimeField(booking.start_time) : extractHmFromTimeField(booking.start_time);
  var timeHm = timeHmRaw || '—';
  var progressPercent = resolveFloorRoomCardProgressPercent(booking);
  var isTimeout = cardType === 'timeout';
  var showLinearProgress = isOccupied && !hideInnerLinearProgress && !(shellShape === 'circle' && FLOOR_ROOM_CARD_DISC_EDGE_RING_ENABLED);
  var paymentKind = floorRoomCardPaymentUiFromOrderStatus(booking.order_payment_status);
  var paymentBadgeClass = paymentBadgeClassName(paymentKind);
  var paymentText = locales.getText(paymentKind === 'paid' ? I18N_PAID : I18N_PAYMENT_NEEDED);
  var tooltipBg = '#000000';
  var timeHintSuffix = locales.getText(isOccupied ? I18N_END : I18N_START);
  var timeTooltipText = "".concat(timeHm, " ").concat(timeHintSuffix);
  return /*#__PURE__*/React.createElement("div", _extends({
    className: "rc-tooltip-time-root",
    "data-pisell-floor-room-scroll-surface": ""
  }, scrollSurfaceHandlers), /*#__PURE__*/React.createElement("div", {
    className: "rc-tooltip-time-stack"
  }, isOccupied && paymentKind === 'unpaid' ? /*#__PURE__*/React.createElement("span", {
    className: classNames('rc-tooltip-time-pay-badge', 'rc-compact__badge', paymentBadgeClass),
    "aria-hidden": true
  }, paymentText) : null, showLinearProgress ? /*#__PURE__*/React.createElement("div", {
    className: "rc-tooltip-time-progress"
  }, /*#__PURE__*/React.createElement(TooltipTimeProgressBar, {
    percent: progressPercent,
    isTimeout: isTimeout
  })) : null, /*#__PURE__*/React.createElement("div", {
    className: "rc-tooltip-time-title-anchor rc-tooltip-time-title-anchor--overflow"
  }, /*#__PURE__*/React.createElement(Tooltip, {
    title: timeTooltipText,
    placement: "bottom",
    open: true,
    color: tooltipBg,
    autoAdjustOverflow: false,
    getPopupContainer: function getPopupContainer(node) {
      var _ref, _node$closest;
      return (
        /**
         * tooltip 必须挂到会被 viewportScale 缩放的容器里（__viewport-compensate），
         * 否则浮层字体/尺寸不会随图元一起缩放。
         */
        (_ref = (_node$closest = node.closest('.pisell-res-floor-room-card__viewport-compensate')) !== null && _node$closest !== void 0 ? _node$closest : node.closest('.pisell-res-floor-room-card')) !== null && _ref !== void 0 ? _ref : document.body
      );
    },
    overlayClassName: classNames('rc-tooltip-time-ant-overlay', !isOccupied && 'rc-tooltip-time-ant-overlay--reserved'),
    arrow: true
  }, /*#__PURE__*/React.createElement("span", {
    className: "rc-tooltip-time-title rc-tooltip-time-title--overflow"
  }, title)))));
}

/**
 * 空态小卡（方桌 / 圆桌共用）：只有桌名、无时间。
 *
 * 与 {@link RoomCardTooltipTimeLayout} 共用 `rc-tooltip-time-*` DOM 结构，但额外加
 * `--title-only` 修饰类：让 `.rc-tooltip-time-title` 取消 ellipsis 裁切，并允许文本
 * 以 anchor 中心为参考向左右**溢出**卡片宽度（卡内仍以居中点为锚，不挤占周围卡片排版）。
 *
 * 该卡片祖先链上的 `data-tooltip-time='on'` 已把 `pisell-res-floor-room-card`、
 * `__viewport-compensate`、`.rc--tooltip-time`、`.rc-tooltip-time-main` 等全部设为
 * `overflow: visible`，因此 title 文本可直接突破小卡边框显示。
 */
export function RoomCardTooltipTitleOnlyLayout(props) {
  var _data$main_field2;
  var data = props.data,
    headerMainFieldOverride = props.headerMainFieldOverride,
    scrollSurfaceHandlers = props.scrollSurfaceHandlers;
  var baseTitle = (headerMainFieldOverride === null || headerMainFieldOverride === void 0 ? void 0 : headerMainFieldOverride.trim()) || String((_data$main_field2 = data.main_field) !== null && _data$main_field2 !== void 0 ? _data$main_field2 : '').trim();
  var titleWithCount = appendBookingCountSuffix(baseTitle, data);
  var titleSafe = titleWithCount || '—';
  return /*#__PURE__*/React.createElement("div", _extends({
    className: "rc-tooltip-time-root",
    "data-pisell-floor-room-scroll-surface": ""
  }, scrollSurfaceHandlers), /*#__PURE__*/React.createElement("div", {
    className: "rc-tooltip-time-stack rc-tooltip-time-stack--title-only"
  }, /*#__PURE__*/React.createElement("div", {
    className: "rc-tooltip-time-title-anchor rc-tooltip-time-title-anchor--overflow"
  }, /*#__PURE__*/React.createElement("span", {
    className: "rc-tooltip-time-title rc-tooltip-time-title--overflow"
  }, titleSafe))));
}