/* eslint-disable @next/next/no-img-element -- External card image URLs; next/image migration is out of scope. */
import { useEffect, useRef, useState } from 'react';
import { formatProcessedDestination, VOCAB } from '../../lib/collection-vocabulary.js';
import GlassSurface from '../ui/GlassSurface.js';
const CONDITION_OPTIONS = ['NM', 'LP', 'MP', 'HP', 'DMG'];
function confidencePercent(confidence) {
if (confidence == null || Number.isNaN(confidence)) return null;
return confidence <= 1 ? Math.round(confidence * 100) : Math.round(confidence);
}
function confidenceRingColor(pct) {
if (pct == null) return 'var(--border)';
if (pct < 70) return 'var(--accent-ember)';
if (pct < 85) return 'var(--color-warning)';
return 'var(--color-success)';
}
export default function ReviewCardItem({
card,
collections,
decks,
onIncrement,
onDecrement,
onUpdateMetadata,
onRemove,
onUndo,
isAdding = false,
sessionDestination,
selected = false,
onToggleSelect,
showCheckbox = false,
confidence,
}) {
const [menuOpen, setMenuOpen] = useState(false);
const menuRef = useRef(null);
const resolvedConfidence = confidence ?? card.confidence;
const confidencePct = confidencePercent(resolvedConfidence);
const destinationLabel = card.processedAction
? formatProcessedDestination(card.processedAction)
: sessionDestination?.label ?? VOCAB.MY_COLLECTION;
useEffect(() => {
if (!menuOpen) return undefined;
const handlePointerDown = (event) => {
if (menuRef.current && !menuRef.current.contains(event.target)) {
setMenuOpen(false);
}
};
document.addEventListener('mousedown', handlePointerDown);
return () => document.removeEventListener('mousedown', handlePointerDown);
}, [menuOpen]);
const checkboxAriaLabel =
confidencePct != null
? `Select ${card.name}, ${confidencePct}% confidence`
: `Select ${card.name}`;
return (
{showCheckbox && !card.processed && (
)}
{card.image_url ? (

) : (
)}
{!showCheckbox && card.processed ? (
) : !showCheckbox && !card.processed ? (
) : null}
{card.name}
{[card.set, card.cardNumber].filter(Boolean).join(' · ')}
{showCheckbox && confidencePct != null && (
{confidencePct}% match
)}
{showCheckbox && !card.processed ? (
{menuOpen && (
)}
) : !card.processed ? (
) : null}
{card.processed ? (
Added to {destinationLabel}
{onUndo && (
)}
) : (
<>
{!showCheckbox && (
{card.condition || 'NM'} · {card.isFoil ? 'Foil' : 'Not Foil'} · Qty: {card.quantity || 1}
)}
{!showCheckbox && (
)}
{card.quantity || 1}
{!showCheckbox && collections?.length > 0 && (
)}
>
)}
);
}