export const DOMINIOS = ['tea', 'cacu', 'cama'] as const; export type Dominio = typeof DOMINIOS[number]; export type TipoPeriodo = 'semestre' | 'mes'; export function isDominio(value: unknown): value is Dominio { return typeof value === 'string' && DOMINIOS.some(domain => domain === value); } export function requireDominio(value: unknown): Dominio { if (!isDominio(value)) throw new Error('Dominio no permitido'); return value; } export interface Indicador { id: string; version?: string; titulo: string; contrato: string; figuras: string[]; tipos_periodo: TipoPeriodo[]; filtros?: IndicadorFiltro[]; // Tabs that show it; without this field, all of them. vistas?: string[]; // Interactive donut and bars by one stratum, like CaCu's EU7. escenario?: VistaEscenario; // How the breakdown by stratum opens: counts, or each category's share of its stratum. medida_estrato?: 'conteo' | 'porcentaje'; } // CaMa's stratifiers (brca, estadio, tamano, etapa) break down its quantities of interest. export type DimensionEstrato = 'grupo_edad' | 'estatus_seguimiento' | 'citologia_alto_grado' | 'birads' | 'brca' | 'estadio' | 'tamano' | 'etapa'; export const DIMENSIONES_ESTRATO: readonly DimensionEstrato[] = ['grupo_edad', 'estatus_seguimiento', 'citologia_alto_grado', 'birads', 'brca', 'estadio', 'tamano', 'etapa']; // `etiqueta` names one value of the stratum in lower case, as in «por escenario clínico». export interface VistaEscenario { estrato: DimensionEstrato; etiqueta: string; } export interface IndicadorFiltro { estrato: string; etiqueta: string; valores: { valor: string; etiqueta: string }[]; } export interface DatosScope { estrato?: DimensionEstrato; filtros?: readonly string[]; } export interface EstratoTotal { periodo: string; grupo: string; estrato: string; n: number | null; suprimido: 0 | 1; } export interface IndicadorDatosEstratificados extends IndicadorDatos { estrato: DimensionEstrato; valores_estrato: string[]; etiquetas_estrato: Record; totales_estrato?: EstratoTotal[]; } export interface IndicadorFicha { codigo?: string | null; para?: string | null; unidad?: string | null; pregunta?: string | null; definicion?: string | null; nota?: string | null; } export interface IndicadorFiguraDetalle { figura: string; descripcion: string | null; como_leer: string[] | null; } export interface IndicadorMetadatos { descripcion?: string | null; ficha?: IndicadorFicha | null; referencias?: { tipo: string; etiqueta: string; ref: string | null }[] | null; poblaciones?: { grupo: string; codigo: string; descripcion: string }[] | null; figuras_detalle?: IndicadorFiguraDetalle[] | null; categorias?: Record; [key: string]: unknown; } // Keep the complete HTTP figure, including future attributes and interpretation notes. export interface PlotlyFigure { data: Record[]; layout: Record; frames?: Record[]; config?: Record; [key: string]: unknown; } export type Tema = 'claro' | 'oscuro'; export interface FigureSelection { dominio?: Dominio; tipoPeriodo?: TipoPeriodo; indicador: string; figura: string; tema: Tema; periodos?: string[]; } // A measure may publish more groups next to `group` (TEA's EEG result: one per alteration). The rows without those // of the other published groups; a row of a group nobody published stays, for the caller to refuse. export function filasDelGrupo(data: IndicadorDatos, group: string): Record[] { const published = data['grupos']; const others = new Set(Object.keys(published && typeof published === 'object' ? published : {}) .map(id => id.toUpperCase()).filter(id => id !== group.toUpperCase())); return others.size ? data.filas.filter(row => !others.has(String(row['grupo']).toUpperCase())) : data.filas; } export function parsePeriodos(value: string, tipo: TipoPeriodo = 'semestre'): string[] | null { if (tipo !== 'semestre' && tipo !== 'mes') return null; const parts = value.split(',').map(part => part.trim()); return parts.length && parts.every(part => part.slice(0, 4) !== '0000' && (tipo === 'semestre' ? /^[0-9]{4}-S[12]$/ : /^[0-9]{4}-(0[1-9]|1[0-2])$/).test(part)) ? [...new Set(parts)] : null; } // Only assert emptiness for known traces with ordinary arrays. Unknown/binary // representations remain indeterminate; never infer counts or suppression causes. export function hasNoVisibleValues(figure: PlotlyFigure): boolean { return figure.data.every(trace => { if (trace['visible'] === false || trace['visible'] === 'legendonly') return true; const type = trace['type'] ?? 'scatter'; const values = type === 'box' ? trace['median'] ?? trace['y'] : type === 'bar' ? (trace['orientation'] === 'h' ? trace['x'] : trace['orientation'] === undefined || trace['orientation'] === 'v' ? trace['y'] : undefined) : type === 'pie' ? trace['values'] : type === 'scatter' ? trace['y'] : undefined; if (!Array.isArray(values)) return false; return values.every(value => value === null || value === undefined || (typeof value === 'number' && !Number.isFinite(value))); }); } export interface IndicadorDatos { id: string; contrato: string; tipo_periodo: TipoPeriodo; periodos: string[]; filas: Record[]; notas?: string[]; [key: string]: unknown; }