import { ChangeDetectorRef, Component, DestroyRef, inject, Input, OnChanges } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { catchError, of, Subject, switchMap } from 'rxjs'; import { Dominio, IndicadorMetadatos } from './indicadores.models'; import { IndicadoresService } from './indicadores.service'; @Component({ selector: 'app-indicador-metadata', styleUrl: './indicador-metadata.scss', template: ` @if (loading) {

Cargando información metodológica…

} @if (description; as text) {

Descripción

{{ text }}

} @if (fichaFields.length) {

Ficha técnica

@for (field of fichaFields; track field.key) { }
{{ field.label }}
} @if (references.length) {

Referencias

} @if (populations.length) {

Poblaciones del estudio

} @if (!loading && !hasUsableMetadata) {

La información metodológica ampliada no está disponible para este indicador.

} `, }) export class IndicadorMetadata implements OnChanges { @Input({ required: true }) dominio!: Dominio; @Input({ required: true }) indicador!: string; private readonly service = inject(IndicadoresService); private readonly requests = new Subject(); metadata?: IndicadorMetadatos; loading = false; private readonly changeDetector = inject(ChangeDetectorRef); private readonly fichaLabels = [ ['codigo', 'Código'], ['para', 'Para quién'], ['unidad', 'Unidad'], ['pregunta', 'Pregunta'], ['definicion', 'Definición'], ['nota', 'Nota'], ] as const; hasText(value: unknown): value is string { return typeof value === 'string' && value.trim().length > 0; } get description() { return this.hasText(this.metadata?.descripcion) ? this.metadata!.descripcion!.trim() : undefined; } get fichaFields() { const ficha = this.metadata?.ficha; return ficha ? this.fichaLabels.flatMap(([key, label]) => this.hasText(ficha[key]) ? [{ key, label, value: ficha[key]!.trim() }] : []) : []; } get references() { return (this.metadata?.referencias ?? []).filter(ref => this.hasText(ref.etiqueta)); } get populations() { return (this.metadata?.poblaciones ?? []).filter(population => this.hasText(population.descripcion)); } get hasUsableMetadata() { return !!(this.description || this.fichaFields.length || this.references.length || this.populations.length); } constructor() { this.requests.pipe(switchMap(() => this.service.metadatos(this.dominio, this.indicador).pipe(catchError(() => of(undefined)))), takeUntilDestroyed(inject(DestroyRef))).subscribe(value => { this.metadata = value; this.loading = false; this.changeDetector.markForCheck(); }); } ngOnChanges() { this.metadata = undefined; this.loading = true; this.requests.next(); } }