"use client"; import { useState, type KeyboardEvent } from "react"; import { Button } from "@/app/components/ui/button"; import { Input } from "@/app/components/ui/input"; import type { MemberCheckoutDiscountLine, MemberCheckoutPreviewResult } from "@/app/lib/types"; import { useMoney } from "@/app/pos/currency"; import { useT } from "@/app/lib/i18n/MessagesProvider"; import type { Translate } from "@/app/lib/i18n/t"; interface PosPromoPanelProps { quote: MemberCheckoutPreviewResult | null; loading: boolean; error: string | null; code: string; appliedCode: string | null; onApply: (code: string) => void; onRemove: () => void; /** Re-prices the same cart after a failed quote. */ onRetry: () => void; /** Accept a special-price gift the quote offers (add-promotion-reward-lines). */ onAcceptReward?: (campaignId: number) => void; /** Gifts the operator accepted; each gets a Remove button. */ acceptedRewardCampaignIds?: number[]; /** Drop an accepted gift. */ onRemoveReward?: (campaignId: number) => void; /** Catalogue name for a product code, when the server sent none. */ nameFor?: (productCode: string) => string | undefined; } /** * Map a promo rejection or notice code to an operator-readable message. * The caller passes `quote.promoRejection ?? quote.promoNotice ?? null` as the first argument. * Returns null when there is nothing to display. */ function promoMessage( code: string | null, shortfall: number | null | undefined, money: (amount: number) => string, t: Translate, ): string | null { if (!code) return null; switch (code) { case "NOT_FOUND": return t("PosPromoNotFound"); case "NOT_STARTED": return t("PosPromoNotStarted"); case "EXPIRED": return t("PosPromoExpired"); case "NOT_VALID_FOR_CHANNEL": return t("PosPromoNotForChannel"); case "LOGIN_REQUIRED": return t("PosPromoLoginRequired"); case "NOT_FOR_YOUR_RANK": return t("PosPromoNotForRank"); case "NOT_VALID_FOR_ACCOUNT": return t("PosPromoOtherAccount"); case "USAGE_LIMIT_REACHED": return t("PosPromoFullyRedeemed"); case "MIN_SPEND_NOT_MET": return t("PosPromoMinSpend", { amount: money(shortfall ?? 0) }); case "MIN_QUANTITY_NOT_MET": { const n = shortfall ?? 0; return t(n === 1 ? "PosPromoMinQtyOne" : "PosPromoMinQtyMany", { count: n }); } case "NOT_VALID_FOR_ITEMS": return t("PosPromoNotForItems"); case "CODE_USED": return t("PosPromoCodeUsed"); case "NOT_ACTIVE": return t("PosPromoNotActive"); case "BETTER_OFFER_APPLIED": return t("PosPromoBetterOffer"); default: return null; } } /** One row per campaign label (amounts summed, first-seen order) — the cart summary and the receipt. */ export function discountRows( lines: MemberCheckoutDiscountLine[] | undefined, ): Array<{ label: string; amount: number }> { const byLabel = new Map(); for (const d of lines ?? []) { byLabel.set(d.campaignLabel, (byLabel.get(d.campaignLabel) ?? 0) + d.amount); } return [...byLabel].map(([label, amount]) => ({ label, amount })); } /** * The operator's promo code entry and the cart's discount summary. It sits inside the POS sale *
, so it has no form of its own and Enter applies the code instead of placing the sale. */ export function PosPromoPanel({ quote, loading, error, code, appliedCode, onApply, onRemove, onRetry, onAcceptReward, acceptedRewardCampaignIds = [], onRemoveReward, nameFor, }: PosPromoPanelProps) { const money = useMoney(); const t = useT(); const [draft, setDraft] = useState(code); const message = quote && code ? promoMessage(quote.promoRejection ?? quote.promoNotice ?? null, quote.promoShortfall, money, t) : null; const rows = discountRows(quote?.discountLines); const rewards = quote?.rewardLines ?? []; const nameOf = (r: { productCode: string; productName?: string | null }) => r.productName || nameFor?.(r.productCode) || r.productCode; function handleKeyDown(e: KeyboardEvent) { if (e.key === "Enter") { e.preventDefault(); if (draft.trim()) onApply(draft); } } return (
{appliedCode ? (
{t("PosPromoApplied", { code: appliedCode })}
) : (
setDraft(e.target.value)} onKeyDown={handleKeyDown} />
)} {message && (

{message}

)} {loading &&

{t("PosPricing")}

} {error && (

{t("PosPriceFailed", { error: t(error) })}

)} {rewards.length > 0 && (
    {rewards.map((r) => (
  • {nameOf(r)} × {r.qty} {r.campaignLabel} {r.status === "NEEDS_ACCEPTANCE" ? ( ) : ( {r.netUnitPrice === 0 ? t("PosFree") : money(r.netUnitPrice)} {r.status === "APPLIED" && onRemoveReward && acceptedRewardCampaignIds.includes(r.campaignId) && ( )} )}
  • ))}
)} {(quote?.rewardOutOfStock ?? []).map((code) => (

{t("PosRewardOutOfStock", { name: nameFor?.(code) || code })}

))} {quote && rows.length > 0 && (
{t("PosSubtotal")}
{money(quote.grossTotal ?? quote.orderTotal)}
{rows.map(({ label, amount }) => (
{t("PosDiscountRow", { label })}
-{money(amount)}
))}
)}
); }