import { METRIC_NAMES } from '../eeg-labels'; import { KeyValuePipe } from '@angular/common'; import { Component, computed, inject, input, resource, signal } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { MatButtonModule } from '@angular/material/button'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; import { MatSelectModule } from '@angular/material/select'; import { RouterLink } from '@angular/router'; import type { Patient } from 'fhir/r4'; import { EXT, Fhir, resources } from '../fhir'; import { Loading } from '../loading'; import { ageLabel } from './figures'; import { name } from './measures'; import { matchesSearch, PatientSearch } from './patient-search'; export interface TeaRow { patient: Patient; name: string; ageGroup: string; case: string; status: string; eegStatus: string; alterations: string[]; lastEeg?: string; mchat?: number; css?: number; } const has = (p: Patient, n: string) => (p.extension ?? []).filter((e) => e.url === EXT + n); export function teaRow(p: Patient): TeaRow { const coded = (n: string) => has(p, n)[0]?.valueCoding?.code ?? ''; return { patient: p, name: p.name?.[0]?.text ?? (p.id ?? '').slice(0, 8), ageGroup: has(p, 'tea-age-group')[0]?.valueString ?? '', case: coded('tea-case'), status: coded('follow-up-status'), eegStatus: coded('tea-eeg-status'), alterations: has(p, 'tea-eeg-alteration').map((e) => e.valueCoding?.code ?? ''), lastEeg: has(p, 'tea-last-eeg')[0]?.valueDate, mchat: has(p, 'mchat-score')[0]?.valueInteger, css: has(p, 'ados2-css')[0]?.valueInteger, }; } const MCHAT = [ { id: 'low', label: 'Bajo (0 a 2)', test: (v: number) => v <= 2 }, { id: 'medium', label: 'Medio (3 a 7)', test: (v: number) => v >= 3 && v <= 7 }, { id: 'high', label: 'Alto (8 a 20)', test: (v: number) => v >= 8 }, ]; const CSS = [ { id: 'minimal', label: 'Mínimo (1 a 2)', test: (v: number) => v <= 2 }, { id: 'low', label: 'Bajo (3 a 4)', test: (v: number) => v >= 3 && v <= 4 }, { id: 'moderate', label: 'Moderado (5 a 7)', test: (v: number) => v >= 5 && v <= 7 }, { id: 'high', label: 'Alto (8 a 10)', test: (v: number) => v >= 8 }, ]; const EEG_STATUS: Record = { normal: 'Normal', abnormal: 'Anormal', no_valid_eeg: 'Sin EEG válido', }; const CASE: Record = { confirmed: 'Diagnóstico confirmado', in_process: 'En proceso', not_confirmed: 'Sin TEA', }; export const alteration = (a: string) => { const [metric, side] = a.split(':'); const shown = (METRIC_NAMES[metric] ?? metric).replace(/ \(.*\)$/, ''); return side ? `${shown} (${side === 'above' ? 'aumentado' : 'disminuido'})` : shown; }; @Component({ selector: 'app-tab-riesgos-tea', imports: [ PatientSearch, FormsModule, KeyValuePipe, Loading, MatButtonModule, MatFormFieldModule, MatInputModule, MatSelectModule, RouterLink, ], templateUrl: './tab-riesgos-tea.html', styleUrls: ['./tab-riesgos.scss', './tab-riesgos-tea.scss'], }) export class TabRiesgosTea { private readonly fhir = inject(Fhir); readonly domain = input.required(); protected readonly cutoff = signal(''); protected readonly window = signal(3); protected readonly eeg = signal(''); protected readonly age = signal(''); protected readonly mchat = signal(''); protected readonly css = signal(''); protected readonly caseState = signal(''); protected readonly altered = signal(''); protected readonly size = signal(50); protected readonly search = signal(''); protected readonly pageIndex = signal(0); private readonly asked = signal({ cutoff: '', window: 3 }); protected readonly mchatBands = MCHAT; protected readonly cssLevels = CSS; protected readonly eegStatuses = EEG_STATUS; protected readonly cases = CASE; protected readonly label = name; protected readonly ageLabel = ageLabel; protected readonly alteration = alteration; private readonly list = resource({ params: () => ({ domain: this.domain(), ...this.asked() }), loader: async ({ params: { domain, cutoff, window } }) => { const pages = await this.fhir.all(domain, 'Patient', { 'follow-up-window': window, ...(cutoff ? { 'follow-up-cutoff': cutoff } : {}), _count: 200, }); return resources(pages, 'Patient').map(teaRow); }, }); protected readonly loading = computed(() => this.list.isLoading()); protected readonly all = computed(() => this.list.value() ?? []); protected readonly ages = computed(() => [...new Set(this.all().map((r) => r.ageGroup))].sort()); protected readonly alterations = computed(() => [...new Set(this.all().flatMap((r) => r.alterations))].sort(), ); protected readonly filtered = computed(() => { const band = MCHAT.find((b) => b.id === this.mchat()); const level = CSS.find((l) => l.id === this.css()); return this.all().filter( (r) => (!this.eeg() || r.eegStatus === this.eeg()) && (!this.age() || r.ageGroup === this.age()) && (!band || (r.mchat !== undefined && band.test(r.mchat))) && (!level || (r.css !== undefined && level.test(r.css))) && (!this.caseState() || r.case === this.caseState()) && (!this.altered() || r.alterations.includes(this.altered())) && matchesSearch(this.search(), [ r.name, r.patient.id, ...(r.patient.name ?? []).map((n) => n.text), ...(r.patient.identifier ?? []).map((i) => i.value), ]), ); }); protected readonly shown = computed(() => this.filtered().slice(this.pageIndex() * this.size(), (this.pageIndex() + 1) * this.size()), ); protected readonly pages = computed(() => Math.max(1, Math.ceil(this.filtered().length / this.size())), ); protected readonly summary = computed(() => { const rows = this.filtered(); const count = (s: string) => rows.filter((r) => r.eegStatus === s).length; const by = new Map(); for (const r of rows) for (const a of r.alterations) by.set(a, (by.get(a) ?? 0) + 1); return { normal: count('normal'), abnormal: count('abnormal'), none: count('no_valid_eeg'), alterations: [...by.entries()].sort((a, b) => b[1] - a[1]), }; }); protected consult(): void { this.pageIndex.set(0); this.asked.set({ cutoff: this.cutoff(), window: this.window() }); } protected setFilter(set: (v: string) => void, value: string): void { set(value); this.pageIndex.set(0); } }