import { Component, computed, inject, input, resource, signal } from '@angular/core'; import { MatButtonToggleModule } from '@angular/material/button-toggle'; import type { Measure, MeasureReport } from 'fhir/r4'; import { Fhir, resources } from '../fhir'; import { Loading } from '../loading'; import { Figure, Plot } from '../plot'; import { historyFigure } from './figures'; import { periodLabel, periods, PeriodType, reading } from './measures'; import { HISTORY, HistoryPanel } from './pages'; interface Drawn { panel: HistoryPanel; /** the group's heading, on the first panel of its run */ heading?: string; figure?: Figure; } /** Trazabilidad as the old tab: the domain's declared panels (pages.HISTORY), in their order, * each measure period by period. */ @Component({ selector: 'app-tab-trazabilidad', imports: [Loading, MatButtonToggleModule, Plot], template: `

Filtros

Mensual Semestral
@if (series.isLoading()) { } @else if (series.error()) { } @else { @for (d of drawn(); track d.panel.measure) { @if (d.heading) {

{{ d.heading }}

}
@if (d.panel.group) {

{{ d.panel.title }}

} @else {

{{ d.panel.title }}

} @if (d.figure) { } @else {

Sin datos para el periodo.

}
} } `, styles: ` :host { display: block; padding-top: 16px; } .card { background: var(--mat-sys-surface); border: 1px solid var(--mat-sys-outline-variant); border-radius: 12px; padding: 16px 20px; margin-bottom: 16px; } .filters-title { font-size: 16px; font-weight: 500; margin: 0 0 12px; } .panel-title, .group-title { font-size: 20px; font-weight: 700; margin: 0 0 8px; } h3.panel-title { font-size: 16px; } .group-title { margin: 24px 0 12px; } `, }) export class TabTrazabilidad { private readonly fhir = inject(Fhir); readonly domain = input.required(); readonly measures = input.required(); protected readonly type = signal('month'); protected readonly series = resource({ params: () => ({ domain: this.domain(), type: this.type(), measures: this.measures() }), loader: async ({ params }) => { const history = HISTORY[params.domain]; if (!history) return []; const asked = periods(params.type).reverse(); return Promise.all( history.panels.map(async (panel) => { const measure = params.measures.find((m) => m.name === panel.measure); if (!measure) return { panel, points: [] }; const pages = await this.fhir.all(params.domain, 'MeasureReport', { measure: measure.id!, 'reporting-period': asked.join(','), }); // One report per period asked, in the order asked. const found = resources(pages, 'MeasureReport'); return { panel, points: asked.map((period, i) => ({ period, r: reading(found[i]) })) }; }), ); }, }); protected readonly drawn = computed(() => { const history = HISTORY[this.domain()]; const xTitle = this.type() === 'month' ? 'Mes y año' : 'Semestre'; let group: string | undefined; if (!this.series.hasValue()) return []; return this.series.value().map(({ panel, points }) => { const heading = panel.group && panel.group !== group ? panel.group : undefined; group = panel.group; const has = (p: (typeof points)[number]) => (p.r?.populations['initial-population'] ?? 0) > 0; const first = points.findIndex(has); if (first < 0 || !history) return { panel, heading }; const last = points.length - 1 - [...points].reverse().findIndex(has); const shown = history.months && this.type() === 'month' ? points.slice(Math.max(first, last + 1 - history.months), last + 1) : points.slice(first, last + 1); const series = shown.map((p) => ({ label: periodLabel(p.period), r: p.r })); return { panel, heading, figure: historyFigure(panel, series, history.line, xTitle) }; }); }); }