1import { categoryFamily } from '@rareindex/taxonomy';2import { normalizeForMatch, type AssetAttributes } from '@rareindex/shared';34/**5 * Deterministic canonical key per category family (§112). Two descriptions of the same object6 * from different sources must produce the same key when their structured attributes agree.7 * Unknown parts are encoded as '' so partial records still collide with fuller ones only when8 * the discriminating fields match.9 */10const n = (s: string | null | undefined): string => (s ? normalizeForMatch(s).replace(/\s+/g, ' ') : '');11const num = (s: string | null | undefined): string => (s ? s.toLowerCase().replace(/^#/, '').replace(/\s+/g, '') : '');1213/** Identifier keys considered deterministic enough to match on their own. */14export const DETERMINISTIC_IDS = [15 'scryfall_id', 'oracle_id', 'tcgplayer_id', 'cardmarket_id', 'pokemontcg_id', 'ygo_id', 'ygo_set_code', 'psa_spec_id', 'psa_item_id',16 'style_code', 'sku', 'upc', 'ean', 'isbn', 'jan', 'lego_set_number', 'bricklink_id', 'brickset_id', 'pricecharting_id', 'reference', 'goldin_item_id', 'stockx_id', 'chrono24_id', 'comic_id', 'gocollect_id', 'hobbydb_id', 'funko_id',17 'tcgdex_id', 'lorcast_id', 'mtggoldfish_uuid', 'gatcg_edition_id', 'swudb_id', 'digimoncard_id', 'sorcery_printing_id', 'discogs_release_id', 'pcgs_number', 'mfc_id', 'comics_org_id', 'ebay_epid', 'asin', 'vin', 'bgg_id', 'amiami_gcode', 'fossilera_item', 'ppg_id',18] as const;1920export function deterministicIdentifiers(ids: Record<string, string> | undefined): Record<string, string> {21 const out: Record<string, string> = {};22 if (!ids) return out;23 for (const k of DETERMINISTIC_IDS) {24 const v = ids[k];25 if (v && String(v).trim()) out[k] = String(v).trim();26 }27 return out;28}2930export function buildCanonicalKey(a: AssetAttributes): string {31 const family = categoryFamily(a.categorySlug);32 const cat = a.categorySlug;33 switch (family) {34 case 'trading_cards':35 case 'sports_cards':36 case 'non_sport_cards': {37 const set = n(a.setCode) || n(a.set);38 return [cat, set, num(a.number), n(a.name), n(a.variant), n(a.language) || 'en'].join('|');39 }40 case 'watches':41 return [cat, n(a.brand), n(a.reference) || n(a.model), n(a.variant)].join('|');42 case 'sneakers': {43 const style = a.identifiers?.style_code ?? a.identifiers?.sku;44 return style ? [cat, n(style)].join('|') : [cat, n(a.brand), n(a.model) || n(a.name), n(a.color), a.year ?? ''].join('|');45 }46 case 'lego': {47 const setNo = a.identifiers?.lego_set_number ?? a.number ?? a.model;48 return [cat, setNo ? num(setNo) : n(a.name), n(a.variant)].join('|');49 }50 case 'video_games':51 case 'gaming_hardware':52 return [cat, n(a.identifiers?.platform ?? a.series ?? a.brand), n(a.name), n(a.region) || n(a.language), n(a.variant)].join('|');53 case 'comics':54 case 'manga':55 return [cat, n(a.brand), n(a.series) || n(a.name), num(a.number), n(a.variant), a.year ?? ''].join('|');56 case 'funko':57 case 'designer_toys':58 case 'action_figures':59 case 'vintage_toys':60 return [cat, n(a.brand), n(a.series) || n(a.franchise), num(a.number), n(a.name), n(a.variant)].join('|');61 case 'coins':62 case 'banknotes':63 return [cat, n(a.country), a.year ?? '', n(a.name), n(a.variant) || n(a.identifiers?.mint_mark)].join('|');64 case 'wine':65 case 'whisky':66 case 'rum':67 case 'cognac':68 return [cat, n(a.brand), n(a.name), a.year ?? '', n(a.size)].join('|');69 default:70 return [cat, n(a.brand), n(a.name), a.year ?? '', n(a.variant), n(a.reference) || num(a.number)].join('|');71 }72}7374/** Human title used for slugs, search and display. Deterministic from attributes. */75export function composeTitle(a: AssetAttributes): string {76 const family = categoryFamily(a.categorySlug);77 const parts: string[] = [];78 switch (family) {79 case 'trading_cards':80 case 'sports_cards':81 case 'non_sport_cards': {82 let head = a.name;83 if (a.number) head += ` #${a.number.replace(/^#/, '')}`;84 const tail = [a.set, a.edition, a.variant].filter(Boolean).join(' ');85 parts.push(head);86 if (tail) parts.push(tail);87 if (a.year) parts.push(`(${a.year})`);88 break;89 }90 case 'watches':91 parts.push([a.brand, a.model ?? a.name].filter(Boolean).join(' '));92 if (a.reference) parts.push(a.reference);93 if (a.variant) parts.push(a.variant);94 break;95 case 'sneakers':96 parts.push(a.name);97 if (a.color && !a.name.toLowerCase().includes(a.color.toLowerCase())) parts.push(a.color);98 if (a.identifiers?.style_code) parts.push(a.identifiers.style_code);99 break;100 case 'lego': {101 const setNo = a.identifiers?.lego_set_number ?? a.number;102 parts.push([setNo ? `LEGO ${setNo}` : 'LEGO', a.name].filter(Boolean).join(' '));103 if (a.series) parts.push(a.series);104 if (a.year) parts.push(`(${a.year})`);105 break;106 }107 case 'video_games':108 case 'gaming_hardware':109 parts.push(a.name);110 if (a.identifiers?.platform ?? a.series) parts.push(a.identifiers?.platform ?? a.series ?? '');111 if (a.region) parts.push(a.region);112 break;113 case 'comics':114 case 'manga':115 parts.push([a.series ?? a.name, a.number ? `#${a.number}` : null].filter(Boolean).join(' '));116 if (a.variant) parts.push(a.variant);117 if (a.year) parts.push(`(${a.year})`);118 break;119 default:120 parts.push([a.brand, a.name].filter(Boolean).join(' '));121 if (a.variant) parts.push(a.variant);122 if (a.year) parts.push(`(${a.year})`);123 }124 return parts.filter(Boolean).join(' · ').replace(/\s+/g, ' ').trim();125}126