'use client'; import { formatPrice } from 'brainerce'; import { useTranslations } from '@/core/lib/translations'; import { useCart } from '@/core/providers/store-provider'; import { TaxEstimateLine } from '@/ui/cart/tax-estimate-line'; import { useCurrency } from '@/core/lib/use-currency'; import { Separator } from '@/components/ui/separator'; import { cn } from '@/core/lib/utils'; interface CartSummaryProps { className?: string; } export function CartSummary({ className }: CartSummaryProps) { const t = useTranslations('cart'); const tc = useTranslations('common'); const { totals, cart } = useCart(); const currency = useCurrency(); const rules = cart?.appliedDiscounts; const ruleAmt = cart?.ruleDiscountAmount ? parseFloat(cart.ruleDiscountAmount) : 0; const couponAmt = totals.discount - ruleAmt; return (

{t('orderSummary')}

{/* Subtotal */}
{tc('subtotal')} {formatPrice(totals.subtotal, { currency }) as string}
{/* Rule discounts - show each rule by name */} {rules && rules.length > 0 ? rules.map((rule) => (
{rule.ruleName} -{formatPrice(parseFloat(rule.discountAmount), { currency }) as string}
)) : ruleAmt > 0 && (
{tc('generalDiscount')} -{formatPrice(ruleAmt, { currency }) as string}
)} {/* Coupon discount */} {cart?.couponCode && couponAmt > 0 && (
{tc('couponDiscount')} ({cart.couponCode}) -{formatPrice(couponAmt, { currency }) as string}
)} {/* Fallback: generic discount when no breakdown available */} {totals.discount > 0 && ruleAmt <= 0 && !cart?.couponCode && (!rules || rules.length === 0) && (
{tc('discount')} -{formatPrice(totals.discount, { currency }) as string}
)} {/* Shipping */} {totals.shipping > 0 && (
{tc('shipping')} {formatPrice(totals.shipping, { currency }) as string}
)} {/* Tax: a real estimate when the store has a rate for the buyer's country, and the original "calculated at checkout" row otherwise. */} {/* Divider */}
{tc('total')} {formatPrice(totals.total, { currency }) as string}
); }