import { useNavigate } from 'react-router-dom';
import { useState } from 'react';
import type { Product, TranslatableString } from '../types';
import { getCategoryImage, formatPrice } from '../types';
import { Heart, ShoppingBag } from 'lucide-react';
import { useWishlistStore } from '../store/wishlistStore';
import { useCartStore } from '../store/cartStore';
import { useTranslation } from 'react-i18next';
function resolveStr(v: TranslatableString | null | undefined, lang: string): string {
if (!v) return ''
if (typeof v === 'string') return v
return v[lang] ?? v[lang.slice(0, 2)] ?? v['th'] ?? v['en'] ?? Object.values(v)[0] ?? ''
}
interface ProductCardProps {
product: Product;
enableQuickShop?: boolean;
}
export default function ProductCard({ product, enableQuickShop = false }: ProductCardProps) {
const navigate = useNavigate();
const { addItem, removeItem, isInWishlist } = useWishlistStore();
const { addToCart } = useCartStore();
const { i18n, t } = useTranslation();
const isWished = isInWishlist(product.id);
const [selectedVariant, setSelectedVariant] = useState(product.variants?.[0] || null);
const displayName = resolveStr(product.name, i18n.language) || product.scent.name;
const handleWishlistClick = (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
if (isWished) {
removeItem(product.id);
} else {
addItem(product);
}
};
const handleCardClick = () => {
navigate(`/products/${product.id}`);
};
return (
{/* Image Area */}

{
(e.target as HTMLImageElement).style.display = 'none';
}}
/>
{/* Category badge */}
{product.category.name}
{/* Wishlist Button */}
{/* Content Area */}
{displayName}
{product.category.name}
{/* Quick Shop Variant Swatches */}
{enableQuickShop && product.variants && product.variants.length > 0 && (
{product.variants.map((variant) => {
const isSelected = selectedVariant?.id === variant.id;
return (
);
})}
)}
{/* Bottom Area */}
{formatPrice(selectedVariant?.price ?? product.variants[0]?.price)}
{!enableQuickShop && (
{t('product.viewDetails')}
)}
{enableQuickShop && selectedVariant && (
)}
);
}