/**
 * 将业务 booking / 卡型 / 布局模式转为 {@link RoomCardBodyView} 所需纯展示字段（含 i18n 解析）。
 */
import { locales } from '@pisell/utils';
import { floorRoomCardBookingCheckoutDomKey } from "./floorRoomCardDom";
import { floorRoomCardPaymentUiFromOrderStatus } from "./floorRoomCardPaymentUi";
import { FLOOR_ROOM_CARD_DISC_EDGE_RING_ENABLED } from "./floorRoomCardDiscEdge";
import { FLOOR_ROOM_CARD_GUEST_SEGMENT_SEP, floorRoomCardGuestSegments, formatFloorRoomCardBookingEndLine, formatFloorRoomCardBookingStartLine, formatFloorRoomCardBookingTimeRange, formatFloorRoomCardDurationMinutes, maskPisellReservationPhoneTail, resolveFloorRoomCardNextBookingLikeBodyModel, 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';
var I18N_ITEMS_SUMMARY = 'pisell-reservation.room-items-summary';
var I18N_DUE_LABEL = 'pisell-reservation.booking-detail.strip-due-label';
function i18nArrivalCountdown(totalMin) {
  var d = formatFloorRoomCardDurationMinutes(Math.round(totalMin));
  return locales.getText(I18N_IN).replace(/\{d\}/g, d);
}
function paymentAmountClassName(kind) {
  if (kind === 'paid') return 'rc-highlight--paid';
  return 'rc-highlight--unpaid';
}
export 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, _booking$end_time, _holder$name$trim, _holder$name, _booking$order_displa, _booking$product_quan, _booking$items_summar;
  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();
  /** 与大卡 {@link RoomCardBodyView} 一致：不保留测量档 mini/micro/compact 的异化正文 */
  var floorDensity = 'comfortable';
  var isReserved = cardType === 'not_arrived' || cardType === 'late';
  var isOccupied = cardType === 'payed' || cardType === 'payment_required' || cardType === 'timeout';
  var showPhone = isReserved;
  /** mini/micro：不拼接「开始/结束」后缀；预约态只显示开始时刻，占用态只显示结束时刻 */
  var narrowTimeLabels = floorDensity === 'mini' || floorDensity === 'micro';
  var startSuffix = narrowTimeLabels ? '' : locales.getText(I18N_START);
  var endSuffix = narrowTimeLabels ? '' : locales.getText(I18N_END);
  var reservedStartLine = formatFloorRoomCardBookingStartLine(booking, startSuffix);
  var reservedEndLine = formatFloorRoomCardBookingEndLine(booking, endSuffix);
  var hasReservedEndTime = String((_booking$end_time = booking.end_time) !== null && _booking$end_time !== void 0 ? _booking$end_time : '').trim().length > 0;
  /** 详情（非 compact）且尚未到店/迟到预留：展示起止；平面图紧凑格仅展示开始 */
  var timeLabel = narrowTimeLabels ? isReserved ? reservedStartLine : formatFloorRoomCardBookingEndLine(booking, endSuffix) : isReserved ? !compact && hasReservedEndTime ? "".concat(reservedStartLine, " \xB7 ").concat(reservedEndLine) : reservedStartLine : formatFloorRoomCardBookingEndLine(booking, 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 = resolveFloorRoomCardNextBookingLikeBodyModel(data, nowMs);
  var holder = booking.holder;
  var guestContactSegments = floorRoomCardGuestSegments(booking, showPhone);
  /** mini/micro：正文人展示极简层，不带来联系人分段 */
  if (floorDensity === 'micro' || floorDensity === 'mini') {
    guestContactSegments = [];
  }
  /** 分段展示合并进各层后，不再单独重复尾号行 */
  var useIntegratedContactSegments = guestContactSegments.length > 0;
  var name = (_holder$name$trim = holder === null || holder === void 0 || (_holder$name = holder.name) === null || _holder$name === void 0 ? void 0 : _holder$name.trim()) !== null && _holder$name$trim !== void 0 ? _holder$name$trim : '';
  var maskedPhone = !useIntegratedContactSegments && showPhone && holder !== null && holder !== void 0 && holder.phone ? maskPisellReservationPhoneTail(holder.phone) : null;
  var guestNameCompact = guestContactSegments.join(FLOOR_ROOM_CARD_GUEST_SEGMENT_SEP);
  var nextGuestLine = null;
  var nextGuestContactSegments = [];
  var nextTimeRange = null;
  if (nextBooking) {
    nextGuestContactSegments = floorRoomCardGuestSegments(nextBooking, true);
    nextGuestLine = nextGuestContactSegments.length ? nextGuestContactSegments.join(FLOOR_ROOM_CARD_GUEST_SEGMENT_SEP) : null;
    nextTimeRange = formatFloorRoomCardBookingTimeRange(nextBooking);
  }
  var progressPercent = resolveFloorRoomCardProgressPercent(booking);
  var orderNumberDisplay = ((_booking$order_displa = booking.order_display_number) === null || _booking$order_displa === void 0 ? void 0 : _booking$order_displa.trim()) || null;
  var amountPrefixLabel = isOccupied && paymentKind !== 'paid' ? locales.getText(I18N_DUE_LABEL) : null;
  var productItemCount = (_booking$product_quan = booking.product_quantity) !== null && _booking$product_quan !== void 0 ? _booking$product_quan : 0;
  var productTotalQty = booking.product_total_quantity;
  var baseItemsCountLabel = productTotalQty != null && productTotalQty > 0 && productItemCount > 0 ? locales.getText(I18N_ITEMS_SUMMARY).replace(/\{items\}/g, String(productItemCount)).replace(/\{qty\}/g, String(productTotalQty)) : locales.getText(I18N_ITEMS_TOTAL).replace(/\{n\}/g, String(productItemCount));
  var itemsSummarySuffix = (_booking$items_summar = booking.items_summary_suffix) === null || _booking$items_summar === void 0 ? void 0 : _booking$items_summar.trim();
  var itemsCountLabel = itemsSummarySuffix ? "".concat(baseItemsCountLabel, " \xB7 ").concat(itemsSummarySuffix) : baseItemsCountLabel;

  /** 详情卡金额/商品汇总：预约中与占用中都展示；mini/micro 不展示金额行 */
  var showLargeAmountRow = !compact && (isOccupied || isReserved) && floorDensity !== 'micro' && floorDensity !== 'mini';
  var showInlinePaymentBadge = shellShape === 'circle' && isOccupied && !compact;
  /** 金额行旁支付徽标/chip：仅 comfortable；compact/mini/micro 不再展示 */
  var showAmountInlineBadge = !compact && isOccupied && floorDensity === 'comfortable' && showInlinePaymentBadge;
  /** 方桌单条占用：大圆印章；圆桌 comfortable：行内徽章故无印章 */
  var showPaymentStamp = floorDensity === 'comfortable' && !compact && isOccupied && (shellShape === 'rect' || !showInlinePaymentBadge);

  /** 多条 compact：占用行展示金额等；mini/micro 隐藏商务列 */
  var showCompactCommerceRow = compact && isOccupied && floorDensity !== 'micro' && floorDensity !== 'mini';
  var itemsCountInsideScroll = showCompactCommerceRow;
  var showEdgeProgressRing = FLOOR_ROOM_CARD_DISC_EDGE_RING_ENABLED && shellShape === 'circle' && !compact && isOccupied;
  var showEdgeProgressRectBorder = shellShape === 'rect' && !compact && isOccupied && floorDensity !== 'comfortable';
  return {
    layout: compact ? 'compact' : 'full',
    checkoutDomKey: floorRoomCardBookingCheckoutDomKey(booking),
    cardType: cardType,
    contactName: name,
    contactPhoneMasked: maskedPhone,
    showContactPhone: showPhone,
    guestContactSegments: guestContactSegments,
    guestNameCompact: guestNameCompact,
    timeLabel: timeLabel,
    partySize: booking.number,
    amountDisplay: booking.amount,
    orderNumberDisplay: orderNumberDisplay,
    amountPrefixLabel: amountPrefixLabel,
    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,
    showEdgeProgressRing: showEdgeProgressRing,
    showEdgeProgressRectBorder: showEdgeProgressRectBorder,
    progressPercent: progressPercent,
    progressIsTimeout: isTimeout,
    nextGuestLine: nextGuestLine,
    nextGuestContactSegments: nextGuestContactSegments,
    nextTimeRange: nextTimeRange
  };
}