import type { FindingRecord } from '../../domain/finding'; import { toolIdForRecord } from '../../importers/status-normalization'; import { toolProfileDefinitions } from '../../profiles/profile-definition-catalog'; function matches(pattern: RegExp, value: string): boolean { pattern.lastIndex = 0; return pattern.test(value.trim()); } /** Resolves a title with the individual record's profile in mixed project sheets. */ export function recordTitleFor(record: FindingRecord, selectedColumn = '', fallbackToolId?: string): string { const toolId = toolIdForRecord(record, fallbackToolId); const titlePatterns = toolProfileDefinitions.find(definition => definition.id === toolId)?.presentation.titleHeaders ?? []; const profileTitle = titlePatterns .map(pattern => record.fields.find(field => field.value && matches(pattern, field.label))?.value) .find(Boolean); // An empty selected column (e.g. a scanner `Name` on a GPO row of the mixed // project sheet) must fall through to the record's own title. return profileTitle || (selectedColumn && record.fields.find(field => field.label === selectedColumn)?.value) || record.title; }