'use client'; import type { InventoryInfo } from 'brainerce'; import { Badge } from '@/components/ui/badge'; import { cn } from '@/core/lib/utils'; import { useTranslations } from '@/core/lib/translations'; import { useStoreCapabilities } from '@/core/providers/store-provider'; import { resolveLowStockThreshold } from '@/core/lib/capabilities'; import { kitInventory } from '@/core/lib/kit'; interface StockBadgeProps { inventory: InventoryInfo | null | undefined; /** * KIT stock — pass `product.kitAvailable` whenever the product may be a kit. * * ⛔ A KIT has NO `inventory` block, so passing `inventory` alone sent every * kit down the `!inventory` branch below and printed a red "Out of stock" on * every card, category page, search result and product page — while the buy * button beside it stayed enabled. `undefined` here means "not a kit"; * `null` means "unlimited". They are not interchangeable. */ kitAvailable?: number | null; /** * Override the threshold. Leave it unset — the merchant's configured value * comes from the store's capabilities, and a hardcoded number here shows the * wrong badge on every store that chose a different one. */ lowStockThreshold?: number; className?: string; } export function StockBadge({ inventory, kitAvailable, lowStockThreshold, className, }: StockBadgeProps) { const t = useTranslations('productDetail'); // Merchant-configured, from the channel's capabilities. Falls back to the // platform default while the fetch is in flight or if it failed, and resolves // to 0 (nothing is ever low) when the merchant turned low-stock warnings off. const { capabilities } = useStoreCapabilities(); const threshold = lowStockThreshold ?? resolveLowStockThreshold(capabilities); // A kit reports from `kitAvailable`; everything else from its inventory row. const stock = kitAvailable !== undefined ? kitInventory(kitAvailable) : inventory; const label = getStockLabel(stock, threshold, t); const color = getStockColor(stock, threshold); return ( {label} ); } function getStockLabel( inventory: InventoryInfo | null | undefined, lowStockThreshold: number, t: (key: string) => string ): string { // No stock signal at all. A KIT reaches here only when the caller forgot to // pass `kitAvailable` — see the prop comment above. if (!inventory) return t('outOfStock'); const { trackingMode, inStock, available } = inventory; if (trackingMode === 'DISABLED') return t('unavailable'); // Sold out but still purchasable: the merchant allowed backorders. `inStock` // is false at zero even then, so the buy button (canPurchase) is the signal // that this is a backorder, not an out-of-stock item. Never print a count here. if (!inStock) { return inventory.canPurchase === true ? t('availableOnBackorder') : t('outOfStock'); } if (trackingMode === 'UNLIMITED') return t('inStock'); // TRACKED — show actual quantity if (available <= lowStockThreshold) { return t('onlyLeft').replace('{available}', String(available)); } return t('availableInStock').replace('{available}', String(available)); } function getStockColor( inventory: InventoryInfo | null | undefined, lowStockThreshold: number ): string { if (!inventory) return 'bg-red-100 text-red-800'; const { trackingMode, inStock, available } = inventory; if (trackingMode === 'DISABLED') return 'bg-red-100 text-red-800'; if (!inStock) { // Backorder (purchasable at zero stock) is a caution tone, not the red of a sold-out item. return inventory.canPurchase === true ? 'bg-yellow-100 text-yellow-800' : 'bg-red-100 text-red-800'; } if (trackingMode === 'TRACKED' && available <= lowStockThreshold) return 'bg-yellow-100 text-yellow-800'; return 'bg-green-100 text-green-800'; }