/**
 * 将业务 booking / 卡型 / 布局模式转为 {@link RoomCardBodyView} 所需纯展示字段（含 i18n 解析）。
 */
import { locales } from '@pisell/utils';
import { floorRoomCardBookingCheckoutDomKey } from "./floorRoomCardDom";
import { floorRoomCardPaymentUiFromOrderStatus } from "./floorRoomCardPaymentUi";
import { formatFloorRoomCardDurationMinutes, getFloorRoomCardExplicitNextBooking, getFloorRoomCardNextAfterPrimaryReserved, getFloorRoomCardNextAfterSingleOccupiedBooking, getFloorRoomCardNextNotStartedBooking, maskPisellReservationPhoneTail, resolveFloorRoomCardProgressPercent, resolveFloorRoomCardReservedCountdownMinutes, resolveFloorRoomCardReservedCountdownMinutesForBooking } from "./floorRoomCardUtils";
var I18N_END = 'pisell-reservation.room-time-end';
var I18N_START = 'pisell-reservation.room-time-start';
var I18N_IN = 'pisell-reservation.room-in-minutes';
var I18N_NEXT = 'pisell-reservation.room-next-prefix';
var I18N_LATE = 'pisell-reservation.late';
var I18N_PAID = 'pisell-reservation.room-paid';
var I18N_PAYMENT_NEEDED = 'pisell-reservation.room-payment-needed';
var I18N_ITEMS_TOTAL = 'pisell-reservation.room-items-total';
function i18nArrivalCountdown(totalMin) {
  var d = formatFloorRoomCardDurationMinutes(Math.round(totalMin));
  return locales.getText(I18N_IN).replace(/\{d\}/g, d);
}
function guestDisplayLine(holder, showPhone) {
  var _holder$name;
  var n = holder === null || holder === void 0 || (_holder$name = holder.name) === null || _holder$name === void 0 ? void 0 : _holder$name.trim();
  if (n) return n;
  if (showPhone && holder !== null && holder !== void 0 && holder.phone) {
    var m = maskPisellReservationPhoneTail(holder.phone);
    if (m) return m;
  }
  return '';
}
function paymentAmountClassName(kind) {
  if (kind === 'paid') return 'rc-highlight--paid';
  return 'rc-highlight--unpaid';
}
function paymentBadgeClassName(kind) {
  if (kind === 'paid') return 'rc-compact__badge--paid';
  return 'rc-compact__badge--unpaid';
}
function paymentLabelKey(kind) {
  if (kind === 'paid') return I18N_PAID;
  return I18N_PAYMENT_NEEDED;
}
export function buildRoomCardBodyViewModel(params) {
  var _params$shellShape, _params$nowMs, _ref, _ref2, _getFloorRoomCardExpl, _holder$name$trim, _holder$name2, _booking$product_quan;
  var cardType = params.cardType,
    booking = params.booking,
    data = params.data,
    compact = params.compact;
  var shellShape = (_params$shellShape = params.shellShape) !== null && _params$shellShape !== void 0 ? _params$shellShape : 'rect';
  var nowMs = (_params$nowMs = params.nowMs) !== null && _params$nowMs !== void 0 ? _params$nowMs : Date.now();
  var isReserved = cardType === 'not_arrived' || cardType === 'late';
  var isOccupied = cardType === 'payed' || cardType === 'payment_required' || cardType === 'timeout';
  var showPhone = isReserved;
  var timeLabel = isReserved ? "".concat(booking.start_time, " ").concat(locales.getText(I18N_START)) : "".concat(booking.end_time, " ").concat(locales.getText(I18N_END));
  var arrivalMin = compact && isReserved ? resolveFloorRoomCardReservedCountdownMinutesForBooking(booking) : resolveFloorRoomCardReservedCountdownMinutes(data);
  var countdown = cardType === 'not_arrived' && arrivalMin != null && arrivalMin > 0 ? i18nArrivalCountdown(arrivalMin) : null;
  var lateDurationLabel = booking.lateTime != null && Number.isFinite(booking.lateTime) && booking.lateTime > 0 ? formatFloorRoomCardDurationMinutes(booking.lateTime) : null;
  var reservedLateOverrun = cardType === 'late' && lateDurationLabel ? "".concat(locales.getText(I18N_LATE), " +").concat(lateDurationLabel) : null;
  var timeoutDurationLabel = booking.timeoutTime != null && Number.isFinite(booking.timeoutTime) && booking.timeoutTime > 0 ? formatFloorRoomCardDurationMinutes(booking.timeoutTime) : null;
  var occupiedTimeoutOverrun = cardType === 'timeout' && timeoutDurationLabel ? "+".concat(timeoutDurationLabel) : null;
  var paymentKind = floorRoomCardPaymentUiFromOrderStatus(booking.order_payment_status);
  var isTimeout = cardType === 'timeout';
  var nextBooking = (_ref = (_ref2 = (_getFloorRoomCardExpl = getFloorRoomCardExplicitNextBooking(data)) !== null && _getFloorRoomCardExpl !== void 0 ? _getFloorRoomCardExpl : getFloorRoomCardNextAfterSingleOccupiedBooking(data)) !== null && _ref2 !== void 0 ? _ref2 : getFloorRoomCardNextAfterPrimaryReserved(data)) !== null && _ref !== void 0 ? _ref : getFloorRoomCardNextNotStartedBooking(data, nowMs);
  var holder = booking.holder;
  var name = (_holder$name$trim = holder === null || holder === void 0 || (_holder$name2 = holder.name) === null || _holder$name2 === void 0 ? void 0 : _holder$name2.trim()) !== null && _holder$name$trim !== void 0 ? _holder$name$trim : '';
  var maskedPhone = showPhone && holder !== null && holder !== void 0 && holder.phone ? maskPisellReservationPhoneTail(holder.phone) : null;
  var guestNameCompact = guestDisplayLine(booking.holder, showPhone);
  var nextGuestLine = null;
  var nextTimeRange = null;
  if (nextBooking) {
    nextGuestLine = guestDisplayLine(nextBooking.holder, true) || null;
    nextTimeRange = "".concat(nextBooking.start_time, " - ").concat(nextBooking.end_time);
  }
  var progressPercent = resolveFloorRoomCardProgressPercent(booking);
  var productQty = (_booking$product_quan = booking.product_quantity) !== null && _booking$product_quan !== void 0 ? _booking$product_quan : 0;
  var itemsCountLabel = locales.getText(I18N_ITEMS_TOTAL).replace(/\{n\}/g, String(productQty));

  /** 预约中（预留）：单条/多条均不展示金额、付款、件数 */
  var showLargeAmountRow = !compact && isOccupied;
  var showInlinePaymentBadge = shellShape === 'circle' && isOccupied && !compact;
  var showAmountInlineBadge = !compact && shellShape === 'circle' && showInlinePaymentBadge;
  /** 方桌单条占用：大圆印章；圆桌单条占用：行内徽章故无印章 */
  var showPaymentStamp = !compact && isOccupied && (shellShape === 'rect' || !showInlinePaymentBadge);

  /** 多条 compact：仅占用行展示金额/徽章/件数；件数与时刻同轨 */
  var showCompactCommerceRow = compact && isOccupied;
  var itemsCountInsideScroll = showCompactCommerceRow;
  return {
    layout: compact ? 'compact' : 'full',
    checkoutDomKey: floorRoomCardBookingCheckoutDomKey(booking),
    cardType: cardType,
    contactName: name,
    contactPhoneMasked: maskedPhone,
    showContactPhone: showPhone,
    guestNameCompact: guestNameCompact,
    timeLabel: timeLabel,
    partySize: booking.number,
    amountDisplay: booking.amount,
    countdown: countdown,
    reservedLateOverrun: reservedLateOverrun,
    occupiedTimeoutOverrun: occupiedTimeoutOverrun,
    paymentAmountClass: paymentAmountClassName(paymentKind),
    paymentBadgeClass: paymentBadgeClassName(paymentKind),
    paymentBadgeText: locales.getText(paymentLabelKey(paymentKind)),
    paymentStampText: locales.getText(paymentLabelKey(paymentKind)),
    paymentIsPaid: paymentKind === 'paid',
    paymentStampVariant: paymentKind,
    showLargeAmountRow: showLargeAmountRow,
    showProgress: isOccupied,
    showPaymentStamp: showPaymentStamp,
    showInlinePaymentBadge: showInlinePaymentBadge,
    showAmountInlineBadge: showAmountInlineBadge,
    showCompactCommerceRow: showCompactCommerceRow,
    itemsCountLabel: itemsCountLabel,
    itemsCountInsideScroll: itemsCountInsideScroll,
    progressPercent: progressPercent,
    progressIsTimeout: isTimeout,
    nextGuestLine: nextGuestLine,
    nextTimeRange: nextTimeRange
  };
}