import { ChevronDown, ChevronRight, Sparkles } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import type { CalibrationProfile, PAProfileSectionProps } from './types'; import { isMatchingCalibration } from './utils'; export function PAProfileSection({ formData, printersWithCalibrations, selectedProfiles, setSelectedProfiles, expandedPrinters, setExpandedPrinters, }: PAProfileSectionProps) { const { t } = useTranslation(); const togglePrinterExpanded = (printerId: string) => { setExpandedPrinters((prev) => { const next = new Set(prev); if (next.has(printerId)) next.delete(printerId); else next.add(printerId); return next; }); }; const toggleProfileSelected = (printerId: string, caliIdx: number, extruderId?: number | null) => { const key = `${printerId}:${caliIdx}:${extruderId ?? 'null'}`; const printerNozzleKey = `${printerId}:${extruderId ?? 'null'}`; setSelectedProfiles((prev) => { const next = new Set(prev); if (next.has(key)) { next.delete(key); } else { // Remove existing profile for same printer/nozzle for (const existingKey of Array.from(next)) { const parts = existingKey.split(':'); const existingPrinterNozzle = `${parts[0]}:${parts[2]}`; if (existingPrinterNozzle === printerNozzleKey) { next.delete(existingKey); } } next.add(key); } return next; }); }; // Auto-select best matching profiles const autoSelectProfiles = () => { const newSelection = new Set(); for (const { printer, calibrations } of printersWithCalibrations) { if (!printer.connected) continue; const matchingCals = calibrations.filter(cal => isMatchingCalibration(cal, formData), ); // Group by extruder const byExtruder = new Map(); for (const cal of matchingCals) { const extKey = `${cal.extruder_id ?? 'null'}`; if (!byExtruder.has(extKey)) byExtruder.set(extKey, []); byExtruder.get(extKey)!.push(cal); } // Select best (highest K) for each extruder for (const [extKey, cals] of byExtruder) { if (cals.length > 0) { const sorted = [...cals].sort((a, b) => b.k_value - a.k_value); const best = sorted[0]; newSelection.add(`${printer.id}:${best.cali_idx}:${extKey}`); } } } setSelectedProfiles(newSelection); }; if (!formData.material) { return (

{t('inventory.selectMaterialFirst')}

); } if (printersWithCalibrations.length === 0) { return (

{t('inventory.noPrintersConfigured')}

); } // Count total matching profiles const totalMatching = printersWithCalibrations.reduce((sum, { printer, calibrations }) => { if (!printer.connected) return sum; return sum + calibrations.filter(cal => isMatchingCalibration(cal, formData)).length; }, 0); const renderProfile = (printer: { id: number }, cal: CalibrationProfile) => { const key = `${printer.id}:${cal.cali_idx}:${cal.extruder_id ?? 'null'}`; const isSelected = selectedProfiles.has(key); return ( ); }; return (
{/* Header with auto-select */}

{t('inventory.matchingFilter')}: {formData.brand || t('inventory.anyBrand')} / {formData.material} / {formData.subtype || t('inventory.anyVariant')}

{totalMatching > 0 && ( )}
{/* Printer sections */}
{printersWithCalibrations.map(({ printer, calibrations }) => { const isExpanded = expandedPrinters.has(String(printer.id)); const matchingCals = calibrations.filter(cal => isMatchingCalibration(cal, formData)); const matchingCount = matchingCals.length; // Multi-nozzle grouping const isMultiNozzle = matchingCals.some(cal => cal.extruder_id !== undefined && cal.extruder_id !== null && cal.extruder_id > 0, ); const leftNozzleCals = matchingCals.filter(cal => cal.extruder_id === 1); const rightNozzleCals = matchingCals.filter(cal => cal.extruder_id === 0 || cal.extruder_id === undefined || cal.extruder_id === null, ); return (
{/* Printer Header */} {/* Calibration Profiles */} {isExpanded && (
{!printer.connected ? (

{t('inventory.printerOffline')}

) : matchingCount === 0 ? (

{t('inventory.noKProfilesMatch')}

) : isMultiNozzle ? ( <> {leftNozzleCals.length > 0 && (

{t('inventory.leftNozzle')}

{leftNozzleCals.map(cal => renderProfile(printer, cal))}
)} {rightNozzleCals.length > 0 && (

{t('inventory.rightNozzle')}

{rightNozzleCals.map(cal => renderProfile(printer, cal))}
)} ) : (
{matchingCals.map(cal => renderProfile(printer, cal))}
)}
)}
); })}
{/* Summary */} {selectedProfiles.size > 0 && (

{selectedProfiles.size} {t('inventory.profilesSelected')}

)}
); }