import { Component, computed, input, signal } from '@angular/core'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatSelectModule } from '@angular/material/select'; /** One scenario's row of a model's scenario summary (models.factors.scenario_row). */ export interface ScenarioFactors { escenario: string; n_escenario: number; horizonte_meses?: number; riesgo_definitorios: string; riesgo_mayoritarios: string; incluye_factor_riesgo: boolean; } interface Entry { text: string; robust: boolean; } /** A listing cell, `variable=category (robusto|sugerente, share)` separated by `;`. */ export function entries(cell: string | null | undefined): Entry[] { return (cell ?? '') .split(';') .map((e) => e.trim()) .filter(Boolean) .map((text) => ({ text, robust: /\(\s*robusto\b/i.test(text) })); } /** "Factores de riesgo de este escenario", as the old record: per scenario of hers, the risk * factors present in at least 95 % of it and those in 50 to 95 %, a robust one solid and a * suggestive one outlined; with a horizon to choose when the model has several. */ @Component({ selector: 'app-factores', imports: [MatFormFieldModule, MatSelectModule], template: `

Factores de riesgo

@if (horizons().length > 1) { Horizonte @for (h of horizons(); track h) { {{ h }} meses } } @for (s of shown(); track s.escenario) {

{{ s.escenario }} (n = {{ s.n }})

@if (s.defining.length || s.majority.length) { @if (s.defining.length) {

Presentes en al menos 95 % del escenario

} @if (s.majority.length) {

Presentes en 50 a 95 % del escenario

} } @else {

Sin factores de riesgo identificados en este escenario.

} } @empty {

El informe del modelo no describe factores de sus escenarios.

} `, styles: ` .horizon { margin: 4px 0 8px; } .level { margin: 6px 0 2px; font-size: 0.85rem; color: var(--mat-sys-on-surface-variant); } .factors { list-style: none; padding: 0; margin: 0 0 6px; display: flex; flex-wrap: wrap; gap: 6px; } .factors li { padding: 2px 10px; border-radius: 12px; border: 1px solid var(--mat-sys-primary); font-size: 0.85rem; } .factors li.robust { background: var(--mat-sys-primary); color: var(--mat-sys-on-primary); } `, }) export class Factores { /** Her scenarios' rows (every horizon when the model has several). */ readonly rows = input.required(); protected readonly chosen = signal(undefined); protected readonly horizons = computed(() => [ ...new Set( this.rows() .map((r) => r.horizonte_meses) .filter((h) => h !== undefined), ), ].sort((a, b) => a! - b!), ); protected readonly horizon = computed(() => this.chosen() ?? this.horizons().at(-1)); protected readonly shown = computed(() => this.rows() .filter((r) => r.horizonte_meses === undefined || r.horizonte_meses === this.horizon()) .map((r) => ({ escenario: r.escenario, n: r.n_escenario, defining: entries(r.riesgo_definitorios), majority: entries(r.riesgo_mayoritarios), })), ); }