import { Component, DestroyRef, ElementRef, inject, Input, OnInit, signal } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { PlotlyChart } from '../../shared/plotly/plotly-chart'; import { PlotlyFigure } from './indicadores.models'; import { TeaAdos2, TeaAlteracionEstado, TeaEeg, TeaEegMapa, TeaPacienteDetalle, TeaSeguimientoService } from './tea-seguimiento.service'; const CLASIFICACION: Record = { autismo: 'Autismo', espectro_autista: 'Espectro autista', no_espectro: 'No espectro' }; // ADOS-2 calibrated severity score ranges. const NIVEL_CSS: Record = { minimo: 'Evidencia mínima o nula', bajo: 'Nivel bajo', moderado: 'Nivel moderado', alto: 'Nivel alto' }; const CALIDAD: Record = { bueno: 'Bueno', aceptable: 'Aceptable', malo: 'Malo' }; const ESTADO: Record = { normal: 'Dentro de referencia', aumentado: 'Aumentado', disminuido: 'Disminuido', no_evaluable: 'No evaluable' }; const INK = '#081839'; // Shape paths have no arcs: the hole is a polygon, wound against the frame so it stays empty under either fill rule. const HOLE = Array.from({ length: 96 }, (_, i) => -2 * Math.PI * i / 96) .map((a, i) => `${i ? 'L' : 'M'} ${Math.cos(a).toFixed(4)} ${Math.sin(a).toFixed(4)}`).join(' '); // A frame with a round hole hides the map outside the head, in the colour behind the map; then the head outline, nose and ears. const headShapes = (fondo: string) => [ { type: 'path', path: `M -2 -2 L 2 -2 L 2 2 L -2 2 Z ${HOLE} Z`, fillrule: 'evenodd', fillcolor: fondo, line: { width: 0 } }, { type: 'circle', xref: 'x', yref: 'y', x0: -1, y0: -1, x1: 1, y1: 1, line: { color: INK, width: 2 } }, { type: 'path', path: 'M -0.13 0.99 L 0 1.13 L 0.13 0.99', line: { color: INK, width: 2 } }, { type: 'path', path: 'M -0.99 0.14 Q -1.1 0.12 -1.08 0 Q -1.1 -0.12 -0.99 -0.14', line: { color: INK, width: 2 } }, { type: 'path', path: 'M 0.99 0.14 Q 1.1 0.12 1.08 0 Q 1.1 -0.12 0.99 -0.14', line: { color: INK, width: 2 } }, ]; export function mapaFigure(eeg: TeaEeg, mapa: TeaEegMapa, fondo = '#FFFFFF'): PlotlyFigure { const m = { ...eeg.ejes_mapa, z: mapa.z }; return { data: [ { type: 'contour', x: m.x, y: m.y, z: m.z, zmin: -3, zmax: 3, zmid: 0, colorscale: 'RdBu', connectgaps: false, contours: { coloring: 'heatmap', showlines: true }, line: { width: .5, color: 'rgba(8,24,57,.3)' }, colorbar: { title: { text: 'z', side: 'top' }, thickness: 12, len: .8 }, hovertemplate: 'z = %{z:.2f}' }, { type: 'scatter', mode: 'markers+text', x: m.electrodos.map(e => e.x), y: m.electrodos.map(e => e.y), text: m.electrodos.map(e => e.nombre), textposition: 'top center', textfont: { size: 9, color: INK }, marker: { size: 5, color: INK }, hoverinfo: 'text', showlegend: false }, ], layout: { margin: { l: 8, r: 8, t: 8, b: 8 }, showlegend: false, shapes: headShapes(fondo), paper_bgcolor: 'rgba(0,0,0,0)', plot_bgcolor: 'rgba(0,0,0,0)', xaxis: { visible: false, range: [-1.2, 1.2], fixedrange: true }, yaxis: { visible: false, range: [-1.15, 1.2], scaleanchor: 'x', fixedrange: true }, }, config: { displayModeBar: false }, }; } @Component({ selector: 'app-tea-paciente-resumen', imports: [PlotlyChart], templateUrl: './tea-paciente-resumen.html', styleUrl: './tea-paciente-resumen.scss', }) export class TeaPacienteResumen implements OnInit { @Input({ required: true }) id = ''; private readonly service = inject(TeaSeguimientoService); private readonly destroy = inject(DestroyRef); readonly status = signal<'loading' | 'ready' | 'error'>('loading'); readonly detalle = signal(undefined); figura?: PlotlyFigure; private mapaElegido?: string; private readonly elemento = inject>(ElementRef); // The first solid background around this component: the map has no box of its own. private fondo() { for (let e: HTMLElement | null = this.elemento.nativeElement; e; e = e.parentElement) { const color = getComputedStyle(e).backgroundColor; if (/^rgb\(/.test(color)) return color; } return '#FFFFFF'; } mapaActual(eeg: TeaEeg) { return eeg.mapas.find(m => m.codigo === this.mapaElegido); } elegirMapa(eeg: TeaEeg, codigo: string) { // The chosen card closes the map again, and the cards take the whole space back. const mapa = codigo === this.mapaElegido ? undefined : eeg.mapas.find(m => m.codigo === codigo); this.mapaElegido = mapa?.codigo; this.figura = mapa && mapaFigure(eeg, mapa, this.fondo()); } readonly clasificacion = CLASIFICACION; readonly nivelCss = NIVEL_CSS; readonly calidad = CALIDAD; readonly estado = ESTADO; ngOnInit() { this.load(); } load() { this.status.set('loading'); this.service.detalle(this.id).pipe(takeUntilDestroyed(this.destroy)).subscribe({ next: d => { this.mapaElegido = undefined; this.figura = undefined; this.detalle.set(d); this.status.set('ready'); }, error: () => this.status.set('error'), }); } alteradas(eeg: TeaEeg) { return eeg.alteraciones.filter(m => m.estado === 'aumentado' || m.estado === 'disminuido'); } pct(value: number, max: number) { return max > 0 ? Math.round(100 * value / max) : 0; } }