import {
DailyBestSellersWidget,
type DailyBestSellerProduct,
type DailyBestSellersSnapshot,
} from "@lumo/widgets";
type StoreSaleLine = {
productId: string;
title: string;
quantity: number;
imageUrl?: string;
productPath?: string;
};
type StorefrontProps = {
storeDate: string;
// The host has selected one completed store-local day.
// Pass null if the report for that day is unavailable.
sales: readonly StoreSaleLine[] | null;
leaders?: DailyBestSellersSnapshot["priorLeaders"];
};
export function StorefrontBestSellers({ storeDate, sales, leaders }: StorefrontProps) {
if (sales === null) return <DailyBestSellersWidget snapshot={null} />;
const products = new Map<string, DailyBestSellerProduct>();
for (const line of sales) {
const { productId, title, quantity, imageUrl, productPath } = line;
const units = (products.get(productId)?.units ?? 0) + quantity;
products.set(productId, { id: productId, name: title, units, image: imageUrl, href: productPath });
}
const snapshot: DailyBestSellersSnapshot = {
date: storeDate,
products: Array.from(products.values()),
priorLeaders: leaders,
};
return <DailyBestSellersWidget snapshot={snapshot} />;
}