All files / src/utils imf-data.ts

92.2% Statements 142/154
77.95% Branches 99/127
100% Functions 25/25
95.23% Lines 120/126

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721                                                                                                                          1x                                                                     1x           1x         1x                                           1x                                                                                                                                                                                                                     1x 14x                               7x 6x 6x 6x 6x                     4x 3x                                                                                                                   8x 8x 4x 4x     2x 1x   1x                             9x 6x 2x 2x   1x     4x                                                   4x 4x 4x 4x   4x   2x     4x                     8x 7x 8x                               4x 4x 8x 8x 8x 8x 8x 8x 8x 8x 8x             4x 4x                                     9x 9x 9x 5x 9x 9x 9x 4x 4x 4x 4x   4x                           8x 8x 8x 8x 8x                                   6x 8x 4x 3x 4x   1x     3x                             2x 5x   3x         3x 1x   3x   1x           1x   1x   1x                               9x 6x 6x           3x   3x 2x 2x 1x       1x 1x                                                               4x     4x 3x 3x 2x 2x 1x                   1x 1x   1x   2x 1x 1x 1x         4x           4x 1x   4x                                   3x 2x   2x 2x 2x 2x       2x       3x                                                    
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Utils/IMFData
 * @description Utility functions for IMF economic data integration.
 *
 * Provides EU member state → IMF country code mapping (mostly the same
 * ISO-3166-1 alpha-3 codes as the World Bank, with a few diffs flagged
 * in `IMF_COUNTRY_CODE_OVERRIDES`), SDMX-JSON response parsing,
 * indicator formatting with forecast awareness, and the
 * `IMFEconomicContext` builder for EU Parliament article enrichment.
 *
 * Functions in this module are designed to be stateless and avoid
 * observable side effects, matching the pattern established by
 * `world-bank-data.ts`. A raw IMF SDMX response may include attribute
 * flags (`OBS_STATUS=F` for forecast), multi-dimensional keyed
 * observations, and partial data — see {@link parseSDMXJSON} for the
 * normalisation rules.
 *
 * ## ⚠️ For AI Agents / Agentic Workflows
 *
 * The constants below ({@link IMF_POLICY_INDICATORS}, {@link IMF_EU_COUNTRY_CODES})
 * are a **convenience subset** used by TypeScript code for formatting
 * and parsing. They do **NOT** represent the full IMF database
 * inventory. For indicator selection in articles and analysis:
 *
 * 1. Read `analysis/methodologies/imf-indicator-mapping.md` — canonical
 *    committee → IMF indicator mapping enforced by the validator.
 * 2. Read `analysis/imf/indicator-catalog.md` — IMF WEO/IFS/FM/BOP/ER
 *    indicators by EP policy domain.
 * 3. Use `imf-search-databases` / `imf-get-parameter-codes` to discover
 *    additional series on demand.
 */
 
import type {
  IMFDatabaseId,
  IMFEconomicContext,
  IMFEconomicIndicatorSummary,
  IMFForecastPoint,
  IMFFrequency,
  IMFMacroIndicatorKey,
  IMFObservation,
  IMFPolicyIndicatorMapping,
  IMFSeries,
} from '../types/imf.js';
import { escapeHTML } from './file-utils.js';
 
// ─── EU Member State → IMF Country Code Mapping ───────────────────────────────
 
/**
 * Maps EU member state ISO 3166-1 alpha-2 codes to IMF country codes.
 *
 * The IMF SDMX 3.0 API uses ISO-3166-1 alpha-3 codes for every EU
 * member state, which matches the World Bank code set exactly. This
 * constant is kept as a standalone copy (rather than re-exporting
 * `EU_COUNTRY_CODES` from `world-bank-data.ts`) so the IMF module
 * remains self-contained and so future IMF-specific overrides
 * (e.g. Kosovo = `UVK` at IMF vs. `XKX` at WB) can land without
 * disturbing the World Bank map.
 */
export const IMF_EU_COUNTRY_CODES: Readonly<Record<string, string>> = {
  AT: 'AUT',
  BE: 'BEL',
  BG: 'BGR',
  HR: 'HRV',
  CY: 'CYP',
  CZ: 'CZE',
  DK: 'DNK',
  EE: 'EST',
  FI: 'FIN',
  FR: 'FRA',
  DE: 'DEU',
  GR: 'GRC',
  HU: 'HUN',
  IE: 'IRL',
  IT: 'ITA',
  LV: 'LVA',
  LT: 'LTU',
  LU: 'LUX',
  MT: 'MLT',
  NL: 'NLD',
  PL: 'POL',
  PT: 'PRT',
  RO: 'ROU',
  SK: 'SVK',
  SI: 'SVN',
  ES: 'ESP',
  SE: 'SWE',
} as const;
 
/**
 * IMF-specific country-code overrides — cases where the IMF's codelist
 * differs from the World Bank's. Keep this documented explicitly so
 * the drift surface stays visible in one place.
 */
export const IMF_COUNTRY_CODE_OVERRIDES: Readonly<Record<string, string>> = {
  // Kosovo: WB uses XKX, IMF uses UVK on some datasets; add more entries here as drift is discovered.
  XK: 'UVK',
} as const;
 
/** IMF aggregate code for the Euro Area (most widely used EU aggregation on WEO). */
export const IMF_EURO_AREA_CODE = 'EA19'; // Historical; `EA` covers the current membership.
 
/**
 * IMF aggregate labels used in EU Parliament article headings.
 */
export const IMF_AGGREGATE_LABELS: Readonly<Record<string, string>> = {
  EU: 'European Union',
  EA: 'Euro Area',
  EA19: 'Euro Area (19 members)',
  OECD: 'OECD members',
  WLD: 'World',
  G7: 'G7',
  G20: 'G20',
} as const;
 
/**
 * Curated IMF indicator mapping — policy-relevant macro/fiscal/trade
 * series that the EU Parliament Monitor uses across article types.
 *
 * Keyed by the stable {@link IMFMacroIndicatorKey} so prompts and
 * templates reference semantic names ("gdpGrowth", "govDebt") rather
 * than raw SDMX codes. When an indicator has forecasts in WEO/FM,
 * `hasForecast` is `true` and the default query horizon should extend
 * to at least `currentYear + 5`.
 */
export const IMF_POLICY_INDICATORS: Readonly<
  Record<IMFMacroIndicatorKey, IMFPolicyIndicatorMapping>
> = {
  gdp: {
    database: 'WEO',
    indicator: 'NGDPD',
    frequency: 'A',
    label: 'GDP (current USD)',
    hasForecast: true,
  },
  gdpGrowth: {
    database: 'WEO',
    indicator: 'NGDP_RPCH',
    frequency: 'A',
    label: 'Real GDP growth',
    hasForecast: true,
  },
  gdpPerCapita: {
    database: 'WEO',
    indicator: 'NGDPDPC',
    frequency: 'A',
    label: 'GDP per capita (current USD)',
    hasForecast: true,
  },
  inflation: {
    database: 'WEO',
    indicator: 'PCPIPCH',
    frequency: 'A',
    label: 'Consumer price inflation',
    hasForecast: true,
  },
  unemployment: {
    database: 'WEO',
    indicator: 'LUR',
    frequency: 'A',
    label: 'Unemployment rate',
    hasForecast: true,
  },
  population: {
    database: 'WEO',
    indicator: 'LP',
    frequency: 'A',
    label: 'Population (millions)',
    hasForecast: true,
  },
  currentAccount: {
    database: 'WEO',
    indicator: 'BCA_NGDPD',
    frequency: 'A',
    label: 'Current account balance (% of GDP)',
    hasForecast: true,
  },
  exportsGdp: {
    database: 'WEO',
    indicator: 'TX_RPCH',
    frequency: 'A',
    label: 'Export volume growth',
    hasForecast: true,
  },
  govDebt: {
    database: 'FM',
    indicator: 'GGXWDG_NGDP',
    frequency: 'A',
    label: 'General government gross debt (% of GDP)',
    hasForecast: true,
  },
  primaryBalance: {
    database: 'FM',
    indicator: 'GGXONLB_NGDP',
    frequency: 'A',
    label: 'Primary balance (% of GDP)',
    hasForecast: true,
  },
  structuralBalance: {
    database: 'FM',
    indicator: 'GGSB_NPGDP',
    frequency: 'A',
    label: 'Structural balance (% of potential GDP)',
    hasForecast: true,
  },
  fdiInflow: {
    database: 'BOP_AGG',
    indicator: 'BFD_BP6_USD',
    frequency: 'Q',
    label: 'FDI inflow (BoP, current USD)',
    hasForecast: false,
  },
  realEffectiveExchangeRate: {
    database: 'ER',
    indicator: 'EREER_IX',
    frequency: 'M',
    label: 'Real effective exchange rate',
    hasForecast: false,
  },
  policyRate: {
    database: 'IFS',
    indicator: 'FPOLM_PA',
    frequency: 'M',
    label: 'Monetary policy rate',
    hasForecast: false,
  },
};
 
/**
 * Short indicator codes — the single source of truth for the content
 * validator's IMF fingerprints list. Derived from the SDMX codes in
 * {@link IMF_POLICY_INDICATORS}. Consumers should prefer this constant
 * over hand-rolled string lists so validator drift is impossible.
 */
export const IMF_INDICATOR_SDMX_CODES: readonly string[] = Array.from(
  new Set(Object.values(IMF_POLICY_INDICATORS).map((m) => m.indicator))
);
 
// ─── Country Code Lookup ───────────────────────────────────────────────────────
 
/**
 * Resolve an ISO 3166-1 alpha-2 code to the IMF country code.
 *
 * Applies {@link IMF_COUNTRY_CODE_OVERRIDES} first so IMF-specific
 * overrides win, then falls back to the EU alpha-3 map, then returns
 * `null` when the code is not recognised.
 *
 * @param iso2Code - Country ISO 3166-1 alpha-2 code (case-insensitive).
 * @returns IMF country code or `null`.
 */
export function getIMFCountryCode(iso2Code: string): string | null {
  if (!iso2Code) return null;
  const upper = iso2Code.toUpperCase();
  const override = IMF_COUNTRY_CODE_OVERRIDES[upper];
  Iif (override) return override;
  return IMF_EU_COUNTRY_CODES[upper] ?? null;
}
 
/**
 * Check whether `iso2Code` is one of the 27 EU member states covered
 * by the IMF codelist.
 *
 * @param iso2Code - Country ISO 3166-1 alpha-2 code.
 * @returns `true` when the code maps to an EU member state.
 */
export function isIMFEUMemberState(iso2Code: string): boolean {
  if (!iso2Code) return false;
  return iso2Code.toUpperCase() in IMF_EU_COUNTRY_CODES;
}
 
// ─── SDMX-JSON Parser ─────────────────────────────────────────────────────────
 
/**
 * Narrowed shape of an SDMX-JSON 2.0 / 3.0 data response. The IMF API
 * returns `dataSets[0].series` keyed by concatenated dimension indices
 * with an `observations` map. We only consume the fields we need so
 * new SDMX attributes (reserved by future IMF releases) are ignored
 * silently rather than raising parse errors.
 */
interface SDMXJSONResponse {
  data?: {
    dataSets?: Array<{
      series?: Record<string, { observations?: Record<string, Array<number | string | null>> }>;
    }>;
    structure?: {
      dimensions?: {
        series?: Array<{
          id: string;
          values?: Array<{ id: string; name?: string }>;
        }>;
        observation?: Array<{
          id: string;
          values?: Array<{ id: string; name?: string }>;
        }>;
      };
      attributes?: {
        observation?: Array<{
          id: string;
          values?: Array<{ id: string; name?: string }>;
        }>;
      };
    };
  };
}
 
/**
 * Return `true` when the raw SDMX observation attributes flag the
 * observation as a forecast (`OBS_STATUS=F`) — the IMF convention.
 * Tolerates missing/partial attribute arrays so malformed responses
 * simply default to `isForecast=false`.
 *
 * @param obsAttributes - Attribute values for the observation (the
 *   slice after the observation value in the SDMX `observations` array).
 * @param obsStatusAttributeIndex - Position of `OBS_STATUS` in the
 *   observation-attribute structure (or `-1` when absent).
 * @param forecastCodeIndex - Position of the `F` code in the
 *   `OBS_STATUS` code list (or `-1` when absent).
 * @returns `true` when the observation is flagged as a forecast.
 * @internal
 */
function observationIsForecast(
  obsAttributes: ReadonlyArray<number | string | null> | undefined,
  obsStatusAttributeIndex: number,
  forecastCodeIndex: number
): boolean {
  Iif (!obsAttributes) return false;
  if (obsStatusAttributeIndex < 0) return false;
  const flag = obsAttributes[obsStatusAttributeIndex];
  if (flag === null || flag === undefined) return false;
  // The IMF SDMX response encodes attribute values either as the code
  // index (numeric) or the literal code string. `F` → forecast.
  if (typeof flag === 'number') {
    return flag === forecastCodeIndex;
  }
  return String(flag).toUpperCase() === 'F';
}
 
/**
 * Deserialise a raw SDMX payload (string or already-parsed object)
 * into a typed {@link SDMXJSONResponse}. Returns `null` when the
 * payload is missing, empty, or not valid JSON.
 *
 * @param raw - Raw payload.
 * @returns Parsed object or `null`.
 * @internal
 */
function deserialiseSDMXPayload(
  raw: string | SDMXJSONResponse | null | undefined
): SDMXJSONResponse | null {
  if (!raw) return null;
  if (typeof raw === 'string') {
    try {
      return JSON.parse(raw) as SDMXJSONResponse;
    } catch {
      return null;
    }
  }
  return raw;
}
 
/**
 * Context extracted once per response: the time-label map and the
 * precomputed OBS_STATUS attribute indexes used to detect forecasts.
 *
 * @internal
 */
interface SDMXDecodingContext {
  timeLabels: ReadonlyArray<{ id: string; name?: string }>;
  obsStatusAttrIndex: number;
  forecastCodeIndex: number;
}
 
/**
 * Build the one-response decoding context: time labels and the
 * OBS_STATUS attribute indexes.
 *
 * @param structure - `structure` block of the SDMX payload.
 * @returns Decoding context used by {@link decodeObservations}.
 * @internal
 */
function buildDecodingContext(
  structure: NonNullable<NonNullable<SDMXJSONResponse['data']>['structure']>
): SDMXDecodingContext {
  const timeDimension = structure.dimensions?.observation?.[0];
  const timeLabels = timeDimension?.values ?? [];
  const obsAttributes = structure.attributes?.observation ?? [];
  const obsStatusAttrIndex = obsAttributes.findIndex((a) => a.id === 'OBS_STATUS');
  const forecastCodeIndex =
    obsStatusAttrIndex >= 0
      ? (obsAttributes[obsStatusAttrIndex]?.values ?? []).findIndex(
          (v) => v.id.toUpperCase() === 'F'
        )
      : -1;
  return { timeLabels, obsStatusAttrIndex, forecastCodeIndex };
}
 
/**
 * Coerce a raw SDMX observation cell to a finite number or `null`.
 *
 * @param rawValue - First element of the SDMX observation array.
 * @returns Number or `null`.
 * @internal
 */
function coerceObservationValue(rawValue: unknown): number | null {
  if (rawValue === null || rawValue === undefined || rawValue === '') return null;
  const n = typeof rawValue === 'number' ? rawValue : Number(rawValue);
  return Number.isFinite(n) ? n : null;
}
 
/**
 * Decode every observation within a single SDMX series into the
 * normalised {@link IMFObservation} shape.
 *
 * @param seriesObservations - `observations` map keyed by observation index.
 * @param ctx - Decoding context from {@link buildDecodingContext}.
 * @returns Ordered array of normalised observations.
 * @internal
 */
function decodeObservations(
  seriesObservations: Record<string, Array<number | string | null>>,
  ctx: SDMXDecodingContext
): IMFObservation[] {
  const out: IMFObservation[] = [];
  for (const [obsIdx, obsArr] of Object.entries(seriesObservations)) {
    const timeIdx = Number.parseInt(obsIdx, 10);
    const labelEntry = Number.isFinite(timeIdx) ? ctx.timeLabels[timeIdx] : undefined;
    const period = labelEntry?.id ?? String(obsIdx);
    const year = parsePeriodYear(period);
    Iif (year === null) continue;
    const arr = Array.isArray(obsArr) ? obsArr : [];
    const value = coerceObservationValue(arr[0]);
    const attrs = arr.slice(1);
    out.push({
      period,
      year,
      value,
      isForecast: observationIsForecast(attrs, ctx.obsStatusAttrIndex, ctx.forecastCodeIndex),
    });
  }
  out.sort((a, b) => a.period.localeCompare(b.period));
  return out;
}
 
/**
 * Parse an SDMX-JSON response into a map of series key → ordered
 * observations.
 *
 * This parser is intentionally tolerant: missing observations,
 * attribute arrays, or structure blocks all degrade to an empty result
 * rather than throwing. That lets the caller pipeline treat a broken
 * response as "no data" and fall through to a World Bank fallback.
 *
 * @param raw - Raw SDMX-JSON payload as returned by the IMF MCP server
 *   (accepts string, object, null, or undefined).
 * @returns Map of series key → ordered observations.
 */
export function parseSDMXJSON(
  raw: string | SDMXJSONResponse | null | undefined
): Map<string, IMFObservation[]> {
  const result = new Map<string, IMFObservation[]>();
  const payload = deserialiseSDMXPayload(raw);
  if (!payload) return result;
  const dataSet = payload.data?.dataSets?.[0];
  const structure = payload.data?.structure;
  const series = dataSet?.series;
  if (!series || !structure) return result;
  const ctx = buildDecodingContext(structure);
  for (const [seriesKey, seriesPayload] of Object.entries(series)) {
    const obs = seriesPayload.observations ?? {};
    result.set(seriesKey, decodeObservations(obs, ctx));
  }
  return result;
}
 
/**
 * Extract the year component from an IMF period label. Supports
 * annual (`2026`), quarterly (`2026-Q1`), and monthly (`2026-04`)
 * formats. Returns `null` when the label is unparseable so callers
 * can skip corrupt rows.
 *
 * @param period - Period label (e.g. `"2026"`, `"2026-Q1"`, `"2026-04"`).
 * @returns Numeric year or `null`.
 * @internal
 */
function parsePeriodYear(period: string): number | null {
  Iif (!period) return null;
  const match = /^(\d{4})/u.exec(period);
  Iif (!match) return null;
  const year = Number.parseInt(match[1] ?? '', 10);
  return Number.isFinite(year) ? year : null;
}
 
// ─── Series / Observation Helpers ─────────────────────────────────────────────
 
/**
 * Extract the most recent observation (by year) from a series,
 * preferring published actuals over forecasts when both are present
 * for the same latest year.
 *
 * Returns `null` when the series contains only null values or is empty.
 *
 * @param observations - Series observations (in any order).
 * @returns Most recent observation with a non-null value, or `null`.
 */
export function getMostRecentObservation(
  observations: readonly IMFObservation[]
): IMFObservation | null {
  if (observations.length === 0) return null;
  const withValues = observations.filter((o) => o.value !== null);
  if (withValues.length === 0) return null;
  withValues.sort((a, b) => {
    if (b.year !== a.year) return b.year - a.year;
    // Prefer actual (isForecast=false) over forecast at the same year.
    Eif (a.isForecast !== b.isForecast) return a.isForecast ? 1 : -1;
    return b.period.localeCompare(a.period);
  });
  return withValues[0] ?? null;
}
 
/**
 * Return just the forecast points from a series, oldest first.
 * Useful for Chart.js dashed-line overlays.
 *
 * @param observations - Series observations.
 * @param vintage - Optional IMF vintage label to stamp on each point.
 * @returns Ordered forecast points.
 */
export function getForecastPoints(
  observations: readonly IMFObservation[],
  vintage?: string
): IMFForecastPoint[] {
  return observations
    .filter((o) => o.isForecast && o.value !== null)
    .map((o) => {
      const point: IMFForecastPoint = {
        period: o.period,
        year: o.year,
        value: o.value as number,
      };
      if (vintage !== undefined) {
        point.vintage = vintage;
      }
      return point;
    })
    .sort((a, b) => a.year - b.year);
}
 
// ─── Value Formatting ────────────────────────────────────────────────────────
 
/** Magnitude threshold for trillion formatting. */
const TRILLION = 1e12;
/** Magnitude threshold for billion formatting. */
const BILLION = 1e9;
/** Magnitude threshold for million formatting. */
const MILLION = 1e6;
 
/**
 * Format a numeric IMF value for display, based on the indicator
 * mapping's label hints.
 *
 * Percentage-family indicators render as `X.Y %`; GDP renders with
 * the T/B/M magnitude suffix; population renders in millions to
 * match the WEO publication convention. Unknown indicators fall back
 * to two decimal places.
 *
 * @param value - Numeric value, or `null` for N/A.
 * @param mapping - Indicator mapping entry (from {@link IMF_POLICY_INDICATORS}).
 * @returns A human-readable formatted string, or `'N/A'` for `null` / non-finite inputs.
 */
export function formatIMFValue(value: number | null, mapping: IMFPolicyIndicatorMapping): string {
  if (value === null || !Number.isFinite(value)) return 'N/A';
  const label = mapping.label.toLowerCase();
  if (
    label.includes('% of gdp') ||
    label.includes('growth') ||
    label.includes('inflation') ||
    label.includes('unemployment')
  ) {
    return `${value.toFixed(1)}%`;
  }
  if (label.includes('gdp') && !label.includes('per capita')) {
    const abs = Math.abs(value);
    if (abs >= TRILLION) return `$${(value / TRILLION).toFixed(1)}T`;
    Eif (abs >= BILLION) return `$${(value / BILLION).toFixed(1)}B`;
    if (abs >= MILLION) return `$${(value / MILLION).toFixed(1)}M`;
    return `$${value.toFixed(0)}`;
  }
  Eif (label.includes('population')) {
    return `${value.toFixed(1)}M`;
  }
  if (label.includes('exchange rate') || label.includes('policy rate')) {
    return value.toFixed(2);
  }
  return value.toFixed(2);
}
 
// ─── Economic Context Builder ─────────────────────────────────────────────────
 
/**
 * Build an {@link IMFEconomicContext} from an indexed series map.
 *
 * Each entry in `seriesByKey` should be keyed by the {@link IMFMacroIndicatorKey}
 * so the builder can look up the mapping and produce a stable display row.
 * Rows with no observations are skipped silently. When ANY indicator
 * carries a forecast, `forecastHorizonYear` is set to the maximum
 * forecast year so the caller can render a dashed overlay region
 * starting from that year.
 *
 * @param countryCode - EU member state ISO2 code or IMF aggregate code.
 * @param countryName - Country display name.
 * @param seriesByKey - Map of {@link IMFMacroIndicatorKey} to series.
 * @param vintage - IMF data vintage label (e.g. `WEO-April-2026`).
 * @returns Populated {@link IMFEconomicContext} (may have an empty `indicators` array).
 */
export function buildIMFEconomicContext(
  countryCode: string,
  countryName: string,
  seriesByKey: ReadonlyMap<IMFMacroIndicatorKey, IMFSeries>,
  vintage?: string
): IMFEconomicContext {
  const indicators: IMFEconomicIndicatorSummary[] = [];
  let forecastHorizonYear: number | undefined;
 
  for (const [key, series] of seriesByKey) {
    const mapping = IMF_POLICY_INDICATORS[key];
    if (!mapping) continue;
    const latest = getMostRecentObservation(series.observations);
    if (!latest) continue;
    const row: IMFEconomicIndicatorSummary = {
      name: mapping.label,
      indicatorId: mapping.indicator,
      database: mapping.database,
      value: latest.value,
      period: latest.period,
      year: latest.year,
      isForecast: latest.isForecast,
      formatted: formatIMFValue(latest.value, mapping),
    };
    Eif (vintage !== undefined) {
      row.vintage = vintage;
    }
    indicators.push(row);
 
    const forecasts = series.observations.filter((o) => o.isForecast);
    for (const f of forecasts) {
      Eif (forecastHorizonYear === undefined || f.year > forecastHorizonYear) {
        forecastHorizonYear = f.year;
      }
    }
  }
 
  const context: IMFEconomicContext = {
    countryCode,
    countryName,
    indicators,
    dataTimestamp: new Date().toISOString(),
  };
  if (forecastHorizonYear !== undefined) {
    context.forecastHorizonYear = forecastHorizonYear;
  }
  return context;
}
 
// ─── HTML Context Section ─────────────────────────────────────────────────────
 
/**
 * Build a WCAG-compliant HTML `<section>` summarising the IMF
 * economic context for a single country.
 *
 * Forecast rows are marked with a `data-forecast="true"` attribute on
 * the `<tr>` so CSS/Chart.js can visually differentiate them from
 * actuals. Source attribution cites the IMF WEO/FM vintage when
 * available. Safe against XSS via `escapeHTML`.
 *
 * @param context - Economic context payload.
 * @returns HTML string with the economic context section, or `''` when there are no indicators.
 */
export function buildIMFEconomicContextHTML(context: IMFEconomicContext): string {
  if (context.indicators.length === 0) return '';
  const rows = context.indicators
    .map((ind) => {
      const forecastAttr = ind.isForecast ? ' data-forecast="true"' : '';
      const forecastLabel = ind.isForecast ? ' <span class="forecast-flag">(forecast)</span>' : '';
      const vintageCell = ind.vintage ? `<td>${escapeHTML(ind.vintage)}</td>` : '<td></td>';
      return `<tr${forecastAttr}><td>${escapeHTML(ind.name)}${forecastLabel}</td><td>${escapeHTML(ind.formatted)}</td><td>${escapeHTML(ind.period)}</td>${vintageCell}</tr>`;
    })
    .join('\n');
 
  const horizonNote = context.forecastHorizonYear
    ? ` <span class="forecast-horizon">Projections extend through ${escapeHTML(String(context.forecastHorizonYear))}.</span>`
    : '';
 
  return `<section class="economic-context imf-economic-context" aria-label="IMF economic indicators for ${escapeHTML(context.countryName)}">
<h2>Economic Context (IMF): ${escapeHTML(context.countryName)}</h2>
<table>
<caption>IMF economic indicators for ${escapeHTML(context.countryName)}.${horizonNote}</caption>
<thead><tr><th scope="col">Indicator</th><th scope="col">Value</th><th scope="col">Period</th><th scope="col">Vintage</th></tr></thead>
<tbody>
${rows}
</tbody>
</table>
<p class="data-source">Source: IMF (<a href="https://data.imf.org/" rel="noopener noreferrer">data.imf.org</a>)</p>
</section>`;
}
 
// ─── Type Re-Exports (convenience) ────────────────────────────────────────────
 
export type {
  IMFDatabaseId,
  IMFEconomicContext,
  IMFEconomicIndicatorSummary,
  IMFForecastPoint,
  IMFFrequency,
  IMFMacroIndicatorKey,
  IMFObservation,
  IMFPolicyIndicatorMapping,
  IMFSeries,
};