import { type CardProduct, productAvailability, priceRange } from "@/lib/catalog";
import { taka } from "@/lib/money";
import ProductCardView, { type CardData } from "./ProductCardView";

export function cardData(p: CardProduct, saved = false): CardData {
  const av = productAvailability(p);
  const { min, max } = priceRange(p);
  const image = p.images[0]?.url ?? null;
  const hover = p.images[1]?.url ?? null;
  return {
    id: p.id,
    slug: p.slug,
    name: p.name,
    price: min === max ? taka(min) : `from ${taka(min)}`,
    image,
    hoverImage: hover,
    status: av.mode === "PRE_ORDER" && av.purchasable ? "Pre-order" : av.label,
    tone: av.mode === "PRE_ORDER" && av.purchasable ? "preorder" : av.purchasable ? "ready" : "muted",
    saved,
  };
}

export default function ProductCard({ p, saved, priority }: { p: CardProduct; saved?: boolean; priority?: boolean }) {
  return <ProductCardView p={cardData(p, saved)} priority={priority} />;
}
