/** * Shared helpers for the `mycards` collection: * record typing and Cardmarket price resolution via the cached TCGdex client. */ import { getCard } from "./tcgdex-cache"; import type { MyCard } from "./directus"; export type MyCardRecord = MyCard; export type PricedCard = MyCard & { price: number; totalPrice: number }; /** Collection variant label → Cardmarket avg price field. */ export const variantPriceMap: Record = { Base: "avg", Holo: "avg-holo", Reverse: "avg-reverse", "First Edition": "avg-firstEdition", Promo: "avg-wPromo", }; /** * Resolve the Cardmarket unit price for each collection card. * Card details come from the 24h in-memory cache, so repeated cards and * repeated page loads don't hit the TCGdex API again. Cards without a * Cardmarket price get 0 for consistent display (0,00 €). */ export async function priceCollectionCards( cards: MyCard[], ): Promise { return Promise.all( cards.map(async (card) => { let price = 0; const detail = await getCard(card.card_id); const cardmarket = detail?.pricing?.cardmarket; if (cardmarket) { const priceKey = variantPriceMap[card.variant ?? ""] ?? "avg"; price = cardmarket[priceKey] ?? cardmarket.avg ?? 0; } return { ...card, price, totalPrice: price }; }), ); }