All files / src/utils world-bank-data.ts

98.07% Statements 102/104
84.11% Branches 90/107
100% Functions 17/17
97.84% Lines 91/93

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                                                                                            2x                                                                                               2x           2x                                                                                                 2x                                   2x                                 2x                                                                     87x 84x 84x 80x 79x 79x 69x 67x                         2x                                                                                       2x                                 168x                     50x                       17x 17x 17x   17x 1017x 1017x 20x   2x 2x   18x   997x 85x 85x   912x     17x 17x                       12x 3x     9x 9x 1x     48x 12x 48x     12x   12x 9x 9x 9x 9x 9x   9x 8x                     8x                         22x 1x       21x 4x 2x   2x 1x   1x 1x           17x 2x 1x   1x 1x           15x                               13x       2x 1x     1x                           7x 5x 2x   3x 3x                                   2x   2x                                                       2x 2x 2x 2x                   2x                                 8x 8x                   6x                               3x 1x     2x     2x       2x                        
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Utils/WorldBankData
 * @description Utility functions for World Bank economic data integration.
 *
 * Provides EU member state → World Bank country code mapping,
 * CSV parsing for MCP responses, indicator formatting, and economic
 * context building for EU Parliament article enrichment.
 *
 * Functions in this module are designed to be stateless and avoid observable
 * side effects, with the exception of explicitly recording metadata such as
 * data timestamps in returned objects.
 *
 * ## ⚠️ For AI Agents / Agentic Workflows
 *
 * The constants below ({@link POLICY_INDICATORS}, {@link EU_COUNTRY_CODES},
 * {@link COMPARISON_COUNTRIES}) are a **convenience subset** used by TypeScript
 * code for formatting and parsing. They do **NOT** represent the full World Bank
 * indicator inventory.
 *
 * **For indicator selection in articles and analysis:**
 * 1. Read `analysis/worldbank/indicator-catalog.md` — **200+ indicators** by EP policy domain
 * 2. Use `search-indicators` MCP tool to **discover indicators on demand** by keyword
 * 3. Read `analysis/worldbank/eu-country-mapping.md` for country codes + comparison groups
 * 4. Read `analysis/worldbank/chart-integration-guide.md` for Chart.js + Mermaid templates
 *
 * The World Bank MCP server has thousands of indicators beyond the ones listed
 * here. Use `search-indicators` to find the best match for any policy topic.
 */
 
import type {
  EconomicContext,
  EconomicIndicatorSummary,
  PolicyRelevantIndicators,
  WorldBankIndicator,
} from '../types/world-bank.js';
import { escapeHTML } from './file-utils.js';
 
// ─── EU Member State → World Bank Country Code Mapping ───────────────────────
 
/**
 * Maps EU member state ISO 3166-1 alpha-2 codes to World Bank alpha-3 codes.
 * Covers all 27 EU member states.
 */
export const EU_COUNTRY_CODES: Readonly<Record<string, string>> = {
  AT: 'AUT', // Austria
  BE: 'BEL', // Belgium
  BG: 'BGR', // Bulgaria
  HR: 'HRV', // Croatia
  CY: 'CYP', // Cyprus
  CZ: 'CZE', // Czech Republic
  DK: 'DNK', // Denmark
  EE: 'EST', // Estonia
  FI: 'FIN', // Finland
  FR: 'FRA', // France
  DE: 'DEU', // Germany
  GR: 'GRC', // Greece
  HU: 'HUN', // Hungary
  IE: 'IRL', // Ireland
  IT: 'ITA', // Italy
  LV: 'LVA', // Latvia
  LT: 'LTU', // Lithuania
  LU: 'LUX', // Luxembourg
  MT: 'MLT', // Malta
  NL: 'NLD', // Netherlands
  PL: 'POL', // Poland
  PT: 'PRT', // Portugal
  RO: 'ROU', // Romania
  SK: 'SVK', // Slovakia
  SI: 'SVN', // Slovenia
  ES: 'ESP', // Spain
  SE: 'SWE', // Sweden
} as const;
 
/**
 * EU aggregate code in World Bank (European Union).
 *
 * ⚠️ **Not accepted by `worldbank-mcp@1.0.1`** — the MCP server returns
 * `Error: Country not found` when this code is passed to any of
 * `get-country-info`, `get-economic-data`, `get-social-data`,
 * `get-health-data`, or `get-education-data`. The code is valid only
 * against the raw World Bank REST API.
 *
 * For EU-wide **economic** context (GDP, inflation, unemployment,
 * fiscal, trade), use the IMF client
 * (`src/mcp/imf-mcp-client.ts`) with IMF aggregate `EU` or `EA` — see
 * {@link IMF_AGGREGATE_LABELS} in `src/utils/imf-data.ts`.
 *
 * Retained as a constant for back-compatibility with raw-REST call paths
 * (e.g., `committee-indicator-map.ts`). Guard every MCP invocation with
 * {@link isMCPSupportedWBCountryCode} to avoid silent empty responses.
 */
export const EU_AGGREGATE_CODE = 'EUU';
 
/**
 * Comparison country codes for benchmarking EU performance against global peers.
 * Organized by geopolitical relevance to EU Parliament policy analysis.
 */
export const COMPARISON_COUNTRIES: Readonly<Record<string, string>> = {
  // ── G7 Non-EU ──
  US: 'USA', // United States
  GB: 'GBR', // United Kingdom (post-Brexit benchmark)
  JP: 'JPN', // Japan
  CA: 'CAN', // Canada
  // ── BRICS ──
  CN: 'CHN', // China
  IN: 'IND', // India
  BR: 'BRA', // Brazil
  RU: 'RUS', // Russia
  ZA: 'ZAF', // South Africa
  // ── EU Candidate States ──
  UA: 'UKR', // Ukraine
  TR: 'TUR', // Türkiye
  RS: 'SRB', // Serbia
  ME: 'MNE', // Montenegro
  AL: 'ALB', // Albania
  MK: 'MKD', // North Macedonia
  MD: 'MDA', // Moldova
  BA: 'BIH', // Bosnia & Herzegovina
  GE: 'GEO', // Georgia
  // ── Key Trade Partners ──
  KR: 'KOR', // South Korea
  AU: 'AUS', // Australia
  NO: 'NOR', // Norway (EEA)
  CH: 'CHE', // Switzerland (EFTA)
  IL: 'ISR', // Israel
} as const;
 
/**
 * Aggregate/region codes useful for EU benchmarking via the raw World
 * Bank REST API.
 *
 * ⚠️ **Not accepted by `worldbank-mcp@1.0.1`** — every code in this
 * table is rejected by the MCP server with `Error: Country not found`.
 * The codes are valid only against the raw WB REST API
 * (`https://api.worldbank.org/v2/country/{code}/indicator/...`).
 *
 * For EU-aggregate **economic** queries, use the IMF client instead
 * (see `src/mcp/imf-mcp-client.ts` and `IMF_AGGREGATE_LABELS` in
 * `src/utils/imf-data.ts`). For non-economic EU-level context, build an
 * explicit country list (e.g., Big Four `DE`+`FR`+`IT`+`ES` as an
 * EU-GDP proxy) and query the MCP per-country.
 *
 * Always gate MCP calls with {@link isMCPSupportedWBCountryCode}.
 *
 * Keys are World Bank group codes; values are human-readable labels.
 */
export const WB_AGGREGATE_LABELS: Readonly<Record<string, string>> = {
  EUU: 'European Union',
  EMU: 'Euro area',
  OED: 'OECD members',
  WLD: 'World',
  ECS: 'Europe & Central Asia',
  NAC: 'North America',
  EAS: 'East Asia & Pacific',
  SSF: 'Sub-Saharan Africa',
} as const;
 
/**
 * Set of aggregate codes that the `worldbank-mcp@1.0.1` server rejects.
 * Used by {@link isMCPSupportedWBCountryCode} as the primary denylist.
 *
 * Kept in sync with {@link WB_AGGREGATE_LABELS}; `as const` preserves the
 * literal-string type so consumers can narrow against known values.
 */
const WB_UNSUPPORTED_MCP_AGGREGATES: ReadonlySet<string> = new Set([
  'EUU',
  'EMU',
  'OED',
  'WLD',
  'ECS',
  'NAC',
  'EAS',
  'SSF',
]);
 
/**
 * Set of informal country aliases that the `worldbank-mcp@1.0.1` server
 * rejects even though the underlying country is available under a
 * different code. E.g., the informal `UK` alias is rejected; use the
 * ISO 3166-1 alpha-2 code `GB` instead.
 */
const WB_UNSUPPORTED_MCP_ALIASES: ReadonlySet<string> = new Set(['UK']);
 
/**
 * Guard: returns `true` if `code` is safe to pass to any
 * `worldbank-mcp@1.0.1` tool (`get-country-info`, `get-economic-data`,
 * `get-social-data`, `get-health-data`, `get-education-data`), and
 * `false` if the code would cause the MCP server to return
 * `Error: Country not found` — which is handled as an empty-data
 * fallback upstream and therefore fails silently.
 *
 * The guard accepts any 2-letter (ISO 3166-1 alpha-2) or 3-letter
 * (alpha-3) country code and rejects:
 *
 * - The 8 aggregate codes listed in {@link WB_AGGREGATE_LABELS}
 *   (`EUU`, `EMU`, `OED`, `WLD`, `ECS`, `NAC`, `EAS`, `SSF`). These
 *   work only against the raw WB REST API — use the IMF client for
 *   EU-aggregate economic context.
 * - The informal alias `UK` — use `GB` instead.
 * - Empty, non-string, or malformed inputs.
 *
 * The guard is case-insensitive: `'euu'` and `'EUU'` both return
 * `false`, `'de'` and `'DE'` both return `true`.
 *
 * @param code - Country code candidate
 * @returns `true` when the code is a per-country identifier accepted by the
 *   MCP server, `false` when it is an aggregate, an informal alias, or invalid
 *
 * @example
 * isMCPSupportedWBCountryCode('DE');   // → true
 * isMCPSupportedWBCountryCode('DEU');  // → true
 * isMCPSupportedWBCountryCode('EUU');  // → false (aggregate)
 * isMCPSupportedWBCountryCode('UK');   // → false (use 'GB')
 * isMCPSupportedWBCountryCode('');     // → false
 */
export function isMCPSupportedWBCountryCode(code: unknown): boolean {
  if (typeof code !== 'string') return false;
  const trimmed = code.trim();
  if (trimmed.length !== 2 && trimmed.length !== 3) return false;
  if (!/^[A-Za-z]+$/u.test(trimmed)) return false;
  const upper = trimmed.toUpperCase();
  if (WB_UNSUPPORTED_MCP_AGGREGATES.has(upper)) return false;
  if (WB_UNSUPPORTED_MCP_ALIASES.has(upper)) return false;
  return true;
}
 
/**
 * World Bank indicator IDs relevant to EU Parliament policy analysis.
 *
 * ⚠️ **AI Agents**: This is a convenience subset of 25 core indicators used by
 * TypeScript formatting code. The World Bank has **thousands** of indicators.
 * For article/analysis generation:
 * - Read `analysis/worldbank/indicator-catalog.md` for the full **200+ indicator** reference
 * - Use `search-indicators` MCP tool to discover indicators on demand by keyword
 * - See `analysis/worldbank/use-cases.md` for when each indicator type adds value
 */
export const POLICY_INDICATORS: PolicyRelevantIndicators = {
  // Macro-economic (get-economic-data)
  gdp: 'NY.GDP.MKTP.CD',
  gdpGrowth: 'NY.GDP.MKTP.KD.ZG',
  gdpPerCapita: 'NY.GDP.PCAP.CD',
  gniPerCapita: 'NY.GNP.PCAP.CD',
  inflation: 'FP.CPI.TOTL.ZG',
  unemployment: 'SL.UEM.TOTL.ZS',
  exportsGdp: 'NE.EXP.GNFS.ZS',
  fdiNet: 'BN.KLT.DINV.CD',
 
  // Trade & fiscal
  trade: 'NE.TRD.GNFS.ZS',
  taxRevenue: 'GC.TAX.TOTL.GD.ZS',
  govExpenditure: 'NE.CON.GOVT.ZS',
  militaryExpenditure: 'MS.MIL.XPND.GD.ZS',
 
  // Social (get-social-data)
  population: 'SP.POP.TOTL',
  lifeExpectancy: 'SP.DYN.LE00.IN',
  birthRate: 'SP.DYN.CBRT.IN',
  deathRate: 'SP.DYN.CDRT.IN',
  internetUsers: 'IT.NET.USER.ZS',
 
  // Health (get-health-data)
  healthExpenditure: 'SH.XPD.CHEX.GD.ZS',
  physicians: 'SH.MED.PHYS.ZS',
  hospitalBeds: 'SH.MED.BEDS.ZS',
 
  // Education (get-education-data)
  educationExpenditure: 'SE.XPD.TOTL.GD.ZS',
 
  // Environment & energy
  co2Emissions: 'EN.ATM.CO2E.PC',
  renewableEnergy: 'EG.FEC.RNEW.ZS',
 
  // Research & innovation
  rdExpenditure: 'GB.XPD.RSDV.GD.ZS',
  hightechExports: 'TX.VAL.TECH.MF.ZS',
} as const;
 
// ─── CSV Parsing ─────────────────────────────────────────────────────────────
 
/** Known CSV header aliases for each World Bank field */
const HEADER_ALIASES: Readonly<Record<string, readonly string[]>> = {
  country: ['country.id', 'countryiso3code', 'country_id'],
  countryName: ['country.value', 'country_name', 'country'],
  indicator: ['indicator.id', 'indicator_id'],
  indicatorName: ['indicator.value', 'indicator_name', 'indicator'],
  date: ['date', 'year'],
  value: ['value'],
};
 
/**
 * Find the column index for a field by matching header aliases.
 *
 * @param headers - Lowercase header names
 * @param aliases - Allowed header aliases for the field
 * @returns Column index or -1 if not found
 */
function findColumnIndex(headers: readonly string[], aliases: readonly string[]): number {
  return headers.findIndex((h) => aliases.includes(h));
}
 
/**
 * Safely read a column value from a row.
 *
 * @param cols - Split row columns
 * @param idx - Column index (-1 means absent)
 * @returns Column value or empty string
 */
function readCol(cols: readonly string[], idx: number): string {
  return idx >= 0 ? (cols[idx] ?? '') : '';
}
 
/**
 * Split a CSV line respecting quoted fields (RFC 4180).
 * Commas inside double-quoted values are preserved as part of the field.
 * Escaped quotes (`""`) inside a quoted field are treated as a literal `"`.
 *
 * @param line - A single CSV row
 * @returns Array of field values with surrounding quotes removed
 */
function splitCSVLine(line: string): string[] {
  const fields: string[] = [];
  let current = '';
  let inQuotes = false;
 
  for (let i = 0; i < line.length; i++) {
    const ch = line[i] ?? '';
    if (ch === '"') {
      if (inQuotes && i + 1 < line.length && line[i + 1] === '"') {
        // RFC 4180 escaped quote: "" → literal "
        current += '"';
        i++; // skip the second quote
      } else {
        inQuotes = !inQuotes;
      }
    } else if (ch === ',' && !inQuotes) {
      fields.push(current.trim());
      current = '';
    } else {
      current += ch;
    }
  }
  fields.push(current.trim());
  return fields;
}
 
/**
 * Parse CSV data from World Bank MCP response into structured indicator objects.
 * The World Bank MCP server returns indicator data as CSV text via pandas.
 * Handles quoted fields that may contain commas (e.g., indicator names).
 *
 * @param csvText - Raw CSV text from the MCP tool response (accepts null/undefined for convenience)
 * @returns Array of parsed World Bank indicator data points
 */
export function parseWorldBankCSV(csvText: string | null | undefined): WorldBankIndicator[] {
  if (!csvText || typeof csvText !== 'string') {
    return [];
  }
 
  const lines = csvText.trim().split(/\r?\n/);
  if (lines.length < 2) {
    return [];
  }
 
  const headers = splitCSVLine(lines[0] ?? '').map((h) => h.toLowerCase());
  const colMap = Object.fromEntries(
    Object.entries(HEADER_ALIASES).map(([key, aliases]) => [key, findColumnIndex(headers, aliases)])
  );
 
  const results: WorldBankIndicator[] = [];
 
  for (let i = 1; i < lines.length; i++) {
    const cols = splitCSVLine(lines[i] ?? '');
    const rawValue = readCol(cols, colMap['value'] ?? -1);
    const parsedValue = rawValue !== '' ? Number(rawValue) : null;
    const yearStr = readCol(cols, colMap['date'] ?? -1);
    const year = yearStr ? parseInt(yearStr, 10) : 0;
 
    if (year > 0) {
      results.push({
        countryId: readCol(cols, colMap['country'] ?? -1),
        countryName: readCol(cols, colMap['countryName'] ?? -1),
        indicatorId: readCol(cols, colMap['indicator'] ?? -1),
        indicatorName: readCol(cols, colMap['indicatorName'] ?? -1),
        year,
        value: Number.isFinite(parsedValue) ? parsedValue : null,
      });
    }
  }
 
  return results;
}
 
// ─── Value Formatting ────────────────────────────────────────────────────────
 
/**
 * Format a numeric value for display based on the indicator type.
 *
 * @param value - The numeric value to format
 * @param indicatorId - The World Bank indicator ID (determines formatting style)
 * @returns Formatted display string
 */
export function formatIndicatorValue(value: number | null, indicatorId: string): string {
  if (value === null || !Number.isFinite(value)) {
    return 'N/A';
  }
 
  // GDP - format as currency with magnitude suffix
  if (indicatorId === POLICY_INDICATORS.gdp) {
    if (Math.abs(value) >= 1e12) {
      return `$${(value / 1e12).toFixed(1)}T`;
    }
    if (Math.abs(value) >= 1e9) {
      return `$${(value / 1e9).toFixed(1)}B`;
    }
    Eif (Math.abs(value) >= 1e6) {
      return `$${(value / 1e6).toFixed(1)}M`;
    }
    return `$${value.toFixed(0)}`;
  }
 
  // Population - format with magnitude suffix
  if (indicatorId === POLICY_INDICATORS.population) {
    if (Math.abs(value) >= 1e9) {
      return `${(value / 1e9).toFixed(2)}B`;
    }
    Eif (Math.abs(value) >= 1e6) {
      return `${(value / 1e6).toFixed(1)}M`;
    }
    return `${value.toFixed(0)}`;
  }
 
  // Percentage indicators
  if (
    indicatorId === POLICY_INDICATORS.gdpGrowth ||
    indicatorId === POLICY_INDICATORS.inflation ||
    indicatorId === POLICY_INDICATORS.unemployment ||
    indicatorId === POLICY_INDICATORS.trade ||
    indicatorId === POLICY_INDICATORS.taxRevenue ||
    indicatorId === POLICY_INDICATORS.govExpenditure ||
    indicatorId === POLICY_INDICATORS.militaryExpenditure ||
    indicatorId === POLICY_INDICATORS.exportsGdp ||
    indicatorId === POLICY_INDICATORS.healthExpenditure ||
    indicatorId === POLICY_INDICATORS.educationExpenditure ||
    indicatorId === POLICY_INDICATORS.internetUsers ||
    indicatorId === POLICY_INDICATORS.renewableEnergy ||
    indicatorId === POLICY_INDICATORS.rdExpenditure ||
    indicatorId === POLICY_INDICATORS.hightechExports
  ) {
    return `${value.toFixed(1)}%`;
  }
 
  // CO2 emissions - metric tons per capita
  if (indicatorId === POLICY_INDICATORS.co2Emissions) {
    return `${value.toFixed(1)} t/cap`;
  }
 
  return value.toFixed(2);
}
 
// ─── Most Recent Value ───────────────────────────────────────────────────────
 
/**
 * Extract the most recent non-null data point from a series of World Bank indicators.
 *
 * @param indicators - Array of indicator data points
 * @returns The most recent data point with a non-null value, or null if none found
 */
export function getMostRecentValue(
  indicators: readonly WorldBankIndicator[]
): WorldBankIndicator | null {
  const withValues = indicators.filter((i) => i.value !== null);
  if (withValues.length === 0) {
    return null;
  }
  withValues.sort((a, b) => b.year - a.year);
  return withValues[0] ?? null;
}
 
// ─── Economic Context Builder ────────────────────────────────────────────────
 
/**
 * Build an economic context summary from raw World Bank indicator data.
 *
 * @param countryCode - EU member state ISO2 code
 * @param countryName - Country display name
 * @param indicatorData - Map of indicator ID to parsed data points
 * @returns Structured economic context for article enrichment
 */
export function buildEconomicContext(
  countryCode: string,
  countryName: string,
  indicatorData: ReadonlyMap<string, readonly WorldBankIndicator[]>
): EconomicContext {
  const indicators: EconomicIndicatorSummary[] = [];
 
  const indicatorNames: Record<string, string> = {
    [POLICY_INDICATORS.gdp]: 'GDP',
    [POLICY_INDICATORS.gdpGrowth]: 'GDP Growth',
    [POLICY_INDICATORS.gdpPerCapita]: 'GDP per Capita',
    [POLICY_INDICATORS.gniPerCapita]: 'GNI per Capita',
    [POLICY_INDICATORS.inflation]: 'Inflation',
    [POLICY_INDICATORS.unemployment]: 'Unemployment',
    [POLICY_INDICATORS.exportsGdp]: 'Exports (% of GDP)',
    [POLICY_INDICATORS.fdiNet]: 'FDI Net Inflows',
    [POLICY_INDICATORS.trade]: 'Trade (% of GDP)',
    [POLICY_INDICATORS.taxRevenue]: 'Tax Revenue (% of GDP)',
    [POLICY_INDICATORS.govExpenditure]: 'Gov. Expenditure (% of GDP)',
    [POLICY_INDICATORS.militaryExpenditure]: 'Military Expenditure (% of GDP)',
    [POLICY_INDICATORS.population]: 'Population',
    [POLICY_INDICATORS.lifeExpectancy]: 'Life Expectancy',
    [POLICY_INDICATORS.birthRate]: 'Birth Rate',
    [POLICY_INDICATORS.deathRate]: 'Death Rate',
    [POLICY_INDICATORS.internetUsers]: 'Internet Users (%)',
    [POLICY_INDICATORS.healthExpenditure]: 'Health Expenditure (% of GDP)',
    [POLICY_INDICATORS.physicians]: 'Physicians (per 1,000)',
    [POLICY_INDICATORS.hospitalBeds]: 'Hospital Beds (per 1,000)',
    [POLICY_INDICATORS.educationExpenditure]: 'Education Expenditure (% of GDP)',
    [POLICY_INDICATORS.co2Emissions]: 'CO₂ Emissions',
    [POLICY_INDICATORS.renewableEnergy]: 'Renewable Energy (%)',
    [POLICY_INDICATORS.rdExpenditure]: 'R&D Expenditure (% of GDP)',
    [POLICY_INDICATORS.hightechExports]: 'High-Tech Exports (%)',
  };
 
  for (const [indicatorId, dataPoints] of indicatorData) {
    const recent = getMostRecentValue(dataPoints);
    Eif (recent) {
      indicators.push({
        name: indicatorNames[indicatorId] ?? indicatorId,
        indicatorId,
        value: recent.value,
        year: recent.year,
        formatted: formatIndicatorValue(recent.value, indicatorId),
      });
    }
  }
 
  return {
    countryCode,
    countryName,
    indicators,
    dataTimestamp: new Date().toISOString(),
  };
}
 
// ─── EU Country Lookup ───────────────────────────────────────────────────────
 
/**
 * Get the World Bank country code for an EU member state.
 *
 * @param iso2Code - EU member state ISO 3166-1 alpha-2 code (e.g., 'DE', 'FR')
 * @returns World Bank alpha-3 code or null if not an EU member state
 */
export function getWorldBankCountryCode(iso2Code: string): string | null {
  const upper = iso2Code.toUpperCase();
  return EU_COUNTRY_CODES[upper] ?? null;
}
 
/**
 * Check if a country code corresponds to an EU member state.
 *
 * @param iso2Code - ISO 3166-1 alpha-2 country code
 * @returns True if the country is an EU member state
 */
export function isEUMemberState(iso2Code: string): boolean {
  return iso2Code.toUpperCase() in EU_COUNTRY_CODES;
}
 
// ─── HTML Context Section ────────────────────────────────────────────────────
 
/**
 * Generate an HTML section with economic context data for article embedding.
 *
 * Note: UI strings are currently in English. A future enhancement should accept
 * a `lang` parameter and use localized string maps (similar to `WEEK_AHEAD_STRINGS`)
 * to support all 14 article languages.
 *
 * @param context - Economic context data
 * @returns Sanitized HTML string for the economic context section
 */
export function buildEconomicContextHTML(context: EconomicContext): string {
  if (context.indicators.length === 0) {
    return '';
  }
 
  const rows = context.indicators
    .map(
      (ind) =>
        `<tr><td>${escapeHTML(ind.name)}</td><td>${escapeHTML(ind.formatted)}</td><td>${escapeHTML(String(ind.year))}</td></tr>`
    )
    .join('\n');
 
  return `<section class="economic-context" aria-label="Economic indicators for ${escapeHTML(context.countryName)}">
<h2>Economic Context: ${escapeHTML(context.countryName)}</h2>
<table>
<caption>Economic indicators for ${escapeHTML(context.countryName)}</caption>
<thead><tr><th scope="col">Indicator</th><th scope="col">Value</th><th scope="col">Year</th></tr></thead>
<tbody>
${rows}
</tbody>
</table>
<p class="data-source">Source: World Bank Open Data</p>
</section>`;
}