import { SeguimientoValor } from '../seguimiento-tabla.service'; import { TrackingSnapshot } from './tracking-export.models'; export const UNAVAILABLE = 'No disponible'; /** A column as the follow-up table shows it: the value and, under it, its labelled details. */ export interface ExportColumn { key: string; label: string; details: { key: string; label: string }[]; } /** One flat column of a spreadsheet; a detail becomes a column of its own so it can be filtered and sorted. */ export interface FlatColumn { key: string; label: string; numeric: boolean; } export interface FlatTable { columns: FlatColumn[]; rows: SeguimientoValor[][]; } /** The columns the answer announced, in its order. */ export function exportColumns(snapshot: TrackingSnapshot): ExportColumn[] { return snapshot.columnas.map(c => ({ key: c.clave, label: c.etiqueta, details: (c.detalles ?? []).map(d => ({ key: d.clave, label: d.etiqueta })) })); } /** Text of a value as the table shows it, `null` included. */ export function displayText(value: SeguimientoValor | undefined): string { return value === null || value === undefined ? UNAVAILABLE : String(value); } /** Every detail after its value, with a column numeric only when each present value is a number. */ export function flatTable(snapshot: TrackingSnapshot): FlatTable { const columns: FlatColumn[] = []; for (const column of exportColumns(snapshot)) { columns.push({ key: column.key, label: column.label, numeric: false }); for (const detail of column.details) columns.push({ key: detail.key, label: `${column.label}: ${detail.label}`, numeric: false }); } for (const column of columns) { const present = snapshot.items.map(item => item[column.key]).filter(v => v !== null && v !== undefined); column.numeric = present.length > 0 && present.every(v => typeof v === 'number'); } return { columns, rows: snapshot.items.map(item => columns.map(column => item[column.key] ?? null)) }; }