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; titulo: string; contrato: string; figuras: string[]; tipos_periodo: TipoPeriodo[]; } // 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[]; } 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; }