'use client' import { useState } from 'react' import type { CatalogItem } from '@/data/mock-catalog' import ColorPicker from './ColorPicker' import { fmt } from '@/lib/format' interface Props { item: CatalogItem } function maxColorsFor(name: string): number | null { const n = name.toLowerCase() if (/arch|column/.test(n)) return 4 if (/\b11["''″]|\b11[- ]?inch/.test(n)) return 1 if (/number.{0,10}sculpt|sculpt.{0,10}number/.test(n)) return 4 if (/ultimate/.test(n)) return 4 return null } /** Lowest stock count across tracked variations. null = nothing tracked. */ function lowestTrackedInventory(item: CatalogItem): number | null { const tracked = item.variations.filter((v) => v.inventory !== null) if (!tracked.length) return null return Math.min(...tracked.map((v) => v.inventory as number)) } const LOW_STOCK_THRESHOLD = 5 export default function ProductCard({ item }: Props) { const [showPicker, setShowPicker] = useState(false) // Prefer admin-set colorMax; fall back to name-based heuristic const maxColors = item.showColors ? (item.colorMax !== null && item.colorMax !== undefined ? item.colorMax : maxColorsFor(item.name)) : null const stock = lowestTrackedInventory(item) const soldOut = stock !== null && stock <= 0 const lowStock = stock !== null && stock > 0 && stock <= LOW_STOCK_THRESHOLD const priceDisplay = item.price ? `From ${fmt(item.price)}` : 'Custom quote' return ( <>
{item.name}
{priceDisplay}
{lowStock && (Only {stock} left
)}{item.description}
{item.tags.length > 0 && (