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 { Bundle } from 'fhir/r4'; import { Fhir } from '../fhir'; import { Loading } from '../loading'; import { windowStart } from './follow-up'; import { name } from './measures'; import { PatientSearch, patientSearch } from './patient-search'; import { ageMonths, exportTea, fetchTeaRows, parameters, SCORE_FILTERS, teaParams, teaPatientPage, teaRows, TOKEN_FILTERS, type TeaFilters, } from './tea-rows'; @Component({ selector: 'app-tab-seguimiento-tea', imports: [ FormsModule, Loading, MatButtonModule, MatFormFieldModule, MatInputModule, MatSelectModule, PatientSearch, RouterLink, ], templateUrl: './tab-seguimiento-tea.html', styleUrl: './tab-seguimiento-tea.scss', }) export class TabSeguimientoTea { private readonly fhir = inject(Fhir); readonly patientId = input(); private readonly cleared = signal(undefined); protected readonly focused = computed(() => this.patientId() === this.cleared() ? undefined : this.patientId(), ); protected readonly filters = signal({ window: 3 }); protected readonly tokenFilters = TOKEN_FILTERS.filter( ({ key }) => key !== 'sex' && key !== 'alteration' && key !== 'status', ); protected readonly scoreFilters = SCORE_FILTERS; private readonly eegOptions = TOKEN_FILTERS.find(({ key }) => key === 'eeg')!.options; protected readonly size = signal(20); protected readonly search = signal(''); protected readonly name = name; protected readonly ageMonths = ageMonths; private readonly request = computed(() => { const id = this.focused(); return id ? { _id: id, 'follow-up-start': '0001-01-01' } : { ...teaParams(this.filters()), ...patientSearch(this.search()) }; }); private readonly first = resource({ params: () => ({ ...this.request(), _count: this.size() }), loader: ({ params }) => teaPatientPage(this.fhir, params), }); private readonly moved = signal< { first: Bundle; page: Bundle } | undefined >(undefined); protected readonly page = computed(() => { const first = this.first.hasValue() ? this.first.value() : undefined; const moved = this.moved(); return moved && moved.first === first ? moved.page : first; }); private readonly applied = computed(() => parameters(this.page())); protected readonly cutoff = computed(() => this.applied()['follow-up-cutoff'] ?? ''); protected readonly start = computed(() => this.cutoff() ? windowStart(this.cutoff(), this.filters().window ?? 3) : '', ); protected readonly rows = computed(() => { const page = this.page(); return page ? teaRows(page) : []; }); protected readonly pageNumber = computed(() => { const self = this.page()?.link?.find(({ relation }) => relation === 'self')?.url; const offset = self ? Number(new URL(self).searchParams.get('_offset')) : 0; return Math.floor(offset / this.size()) + 1; }); protected readonly pageCount = computed(() => Math.max(1, Math.ceil((this.page()?.total ?? 0) / this.size())), ); protected readonly error = this.first.error; protected readonly moving = signal(false); protected readonly loading = computed(() => this.first.isLoading() || this.moving()); protected readonly exporting = signal(false); protected readonly actionError = signal(''); protected eegDisplay(code: string | undefined): string { return this.eegOptions.find((option) => option.code === code)?.label ?? '—'; } protected clearFocus(id: string): void { this.cleared.set(id); } protected setFilter(key: K, value: TeaFilters[K]): void { this.filters.update((filters) => ({ ...filters, [key]: value })); } protected has(relation: 'next' | 'previous'): boolean { return !!this.page()?.link?.some((link) => link.relation === relation); } protected async go(relation: 'next' | 'previous'): Promise { const first = this.first.hasValue() ? this.first.value() : undefined; const page = this.page(); if (!first || !page) return; const next = this.fhir.follow('tea', page, relation); if (!next) return; this.moving.set(true); this.actionError.set(''); try { const found = await next; if (this.first.hasValue() && this.first.value() === first) { this.moved.set({ first, page: found }); } } catch { this.actionError.set('No se pudo cambiar de página.'); } finally { this.moving.set(false); } } protected async export(): Promise { this.exporting.set(true); this.actionError.set(''); try { const applied = this.applied(); const { rows } = await fetchTeaRows(this.fhir, { ...this.request(), 'follow-up-cutoff': applied['follow-up-cutoff'], }); await exportTea(rows, applied, false); } catch { this.actionError.set('No se pudo exportar el archivo.'); } finally { this.exporting.set(false); } } }