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 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 | 9x 9x 33x 28x 28x 33x 19x 19x 19x 2x 8x 8x 6x 5x 5x 1x 41x 21x 21x 19x 20x 18x 90x 9x 90x 88x 88x 81x 56x 56x 90x 90x 90x 90x 90x 90x 90x 280x 278x 92x 28x 64x 64x 64x 64x 64x 64x 44x 64x 22x 64x 64x 64x 22x 64x 64x 83x 56x 56x 56x 56x 243x 56x 83x 23x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 13x 13x 3x 2x 13x 13x 13x 9x 9x 9x 9x 8x 8x 4x 8x 9x 5x 8x 8x 4x 21x 8x 8x 7x 7x | // SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
/**
* @module Generators/WeekAheadContent
* @description Pure functions for parsing MCP data and building week-ahead article HTML.
* No side effects — all functions accept data and return strings.
*/
import { escapeHTML } from '../utils/file-utils.js';
import {
getLocalizedString,
EDITORIAL_STRINGS,
WEEK_AHEAD_STRINGS,
WEEK_AHEAD_STAKEHOLDER_STRINGS,
} from '../constants/languages.js';
import type {
ParliamentEvent,
CommitteeMeeting,
LegislativeDocument,
LegislativeProcedure,
ParliamentaryQuestion,
WeekAheadData,
DateRange,
MCPToolResult,
LegislativeVelocity,
StakeholderImpactSection,
StakeholderImpactRow,
PoliticalTemperature,
PoliticalTemperatureBand,
WeekAheadStakeholderStrings,
} from '../types/index.js';
/** Keyword constant for article tagging */
const KEYWORD_EUROPEAN_PARLIAMENT = 'European Parliament';
/** Placeholder events used when MCP is unavailable or returns no sessions */
export const PLACEHOLDER_EVENTS: ParliamentEvent[] = [
{
date: '',
title: 'Plenary Session',
type: 'Plenary',
description: 'Full parliamentary session',
},
{
date: '',
title: 'ENVI Committee Meeting',
type: 'Committee',
description: 'Environment committee discussion',
},
];
/**
* Generic parser for settled MCP results.
* Extracts an array from the JSON payload at the given key and maps each element.
*
* @param settled - Promise.allSettled result
* @param key - JSON key containing the array of items
* @param mapper - Function to map raw items to typed objects
* @returns Array of typed objects, or empty array on failure
*/
function parseSettledMCPResult<T>(
settled: PromiseSettledResult<MCPToolResult>,
key: string,
mapper: (raw: Record<string, unknown>) => T
): T[] {
if (settled.status !== 'fulfilled') return [];
try {
const data = JSON.parse(settled.value.content?.[0]?.text ?? '{}') as Record<string, unknown>;
if (!Object.hasOwn(data, key)) return [];
// eslint-disable-next-line security/detect-object-injection
const items: unknown = data[key];
Iif (!Array.isArray(items)) return [];
return items.map(mapper);
} catch {
return [];
}
}
/**
* Parse plenary sessions from a settled MCP result
*
* @param settled - Promise.allSettled result
* @param fallbackDate - Fallback date when session has none
* @returns Array of parliament events
*/
export function parsePlenarySessions(
settled: PromiseSettledResult<MCPToolResult>,
fallbackDate: string
): ParliamentEvent[] {
return parseSettledMCPResult(settled, 'sessions', (s) => ({
date: (s.date as string | undefined) ?? fallbackDate,
title: (s.title as string | undefined) ?? 'Parliamentary Session',
type: (s.type as string | undefined) ?? 'Session',
description: (s.description as string | undefined) ?? '',
}));
}
/**
* Parse EP events (hearings, conferences, seminars) from a settled MCP result
*
* @param settled - Promise.allSettled result
* @param fallbackDate - Fallback date when event has none
* @returns Array of parliament events
*/
export function parseEPEvents(
settled: PromiseSettledResult<MCPToolResult>,
fallbackDate: string
): ParliamentEvent[] {
return parseSettledMCPResult(settled, 'events', (e) => ({
date: (e.date as string | undefined) ?? fallbackDate,
title: (e.title as string | undefined) ?? 'EP Event',
type: (e.type as string | undefined) ?? 'Event',
description: (e.description as string | undefined) ?? '',
}));
}
/**
* Parse committee meetings from a settled MCP result
*
* @param settled - Promise.allSettled result
* @param fallbackDate - Fallback date when meeting has no date
* @returns Array of committee meetings
*/
export function parseCommitteeMeetings(
settled: PromiseSettledResult<MCPToolResult>,
fallbackDate?: string
): CommitteeMeeting[] {
return parseSettledMCPResult(settled, 'committees', (c) => ({
id: c.id as string | undefined,
committee: (c.committee as string | undefined) ?? 'Unknown',
committeeName: c.committeeName as string | undefined,
date: (c.date as string | undefined) ?? fallbackDate ?? '',
time: c.time as string | undefined,
location: c.location as string | undefined,
agenda: (
c.agenda as
| Array<{
item?: number | undefined;
title?: string | undefined;
type?: string | undefined;
}>
| undefined
)?.map((a) => ({
item: a.item,
title: a.title ?? '',
type: a.type,
})),
}));
}
/**
* Parse legislative documents from a settled MCP result
*
* @param settled - Promise.allSettled result
* @returns Array of legislative documents
*/
export function parseLegislativeDocuments(
settled: PromiseSettledResult<MCPToolResult>
): LegislativeDocument[] {
return parseSettledMCPResult(settled, 'documents', (d) => ({
id: d.id as string | undefined,
type: d.type as string | undefined,
title: (d.title as string | undefined) ?? 'Untitled Document',
date: d.date as string | undefined,
status: d.status as string | undefined,
committee: d.committee as string | undefined,
rapporteur: d.rapporteur as string | undefined,
}));
}
/**
* Parse legislative pipeline from a settled MCP result
*
* @param settled - Promise.allSettled result
* @returns Array of legislative procedures
*/
export function parseLegislativePipeline(
settled: PromiseSettledResult<MCPToolResult>
): LegislativeProcedure[] {
return parseSettledMCPResult(settled, 'procedures', (p) => ({
id: p.id as string | undefined,
title: (p.title as string | undefined) ?? 'Unnamed Procedure',
stage: p.stage as string | undefined,
committee: p.committee as string | undefined,
status: p.status as string | undefined,
bottleneck: p.bottleneck as boolean | undefined,
}));
}
/**
* Parse parliamentary questions from a settled MCP result
*
* @param settled - Promise.allSettled result
* @returns Array of parliamentary questions
*/
export function parseParliamentaryQuestions(
settled: PromiseSettledResult<MCPToolResult>
): ParliamentaryQuestion[] {
return parseSettledMCPResult(settled, 'questions', (q) => ({
id: q.id as string | undefined,
type: q.type as string | undefined,
author: q.author as string | undefined,
subject: (q.subject as string | undefined) ?? 'No subject',
date: q.date as string | undefined,
status: q.status as string | undefined,
}));
}
// ─── Render helpers ──────────────────────────────────────────────────────────
/**
* Render a single plenary event as HTML
*
* @param event - Parliament event data
* @returns HTML string
*/
function renderPlenaryEvent(event: ParliamentEvent): string {
return `
<div class="event-item">
<div class="event-date">${escapeHTML(event.date)}</div>
<div class="event-details">
<h3>${escapeHTML(event.title)}</h3>
<p class="event-type">${escapeHTML(event.type)}</p>
${event.description ? `<p>${escapeHTML(event.description)}</p>` : ''}
</div>
</div>`;
}
/**
* Render a single committee meeting as HTML
*
* @param meeting - Committee meeting data
* @returns HTML string
*/
function renderCommitteeMeeting(meeting: CommitteeMeeting): string {
const agendaHtml =
meeting.agenda && meeting.agenda.length > 0
? `<ul class="agenda-list">${meeting.agenda.map((item) => `<li>${escapeHTML(item.title)}${item.type ? ` <span class="agenda-type">(${escapeHTML(item.type)})</span>` : ''}</li>`).join('')}</ul>`
: '';
return `
<div class="committee-item">
<div class="committee-date">${escapeHTML(meeting.date)}${meeting.time ? ` ${escapeHTML(meeting.time)}` : ''}</div>
<div class="committee-details">
<h3>${escapeHTML(meeting.committeeName ?? meeting.committee)}</h3>
${meeting.location ? `<p class="committee-location">${escapeHTML(meeting.location)}</p>` : ''}
${agendaHtml}
</div>
</div>`;
}
/**
* Render a single legislative document as HTML
*
* @param doc - Legislative document
* @returns HTML string
*/
function renderLegislativeDocument(doc: LegislativeDocument): string {
return `
<li class="document-item">
<span class="document-title">${escapeHTML(doc.title)}</span>
${doc.type ? ` <span class="document-type">(${escapeHTML(doc.type)})</span>` : ''}
${doc.committee ? ` — <span class="document-committee">${escapeHTML(doc.committee)}</span>` : ''}
${doc.status ? ` <span class="document-status">[${escapeHTML(doc.status)}]</span>` : ''}
</li>`;
}
/**
* Render a single pipeline procedure as HTML
*
* @param proc - Legislative procedure
* @returns HTML string
*/
function renderPipelineProcedure(proc: LegislativeProcedure): string {
return `
<li class="pipeline-item${proc.bottleneck ? ' bottleneck' : ''}">
<span class="procedure-title">${escapeHTML(proc.title)}</span>
${proc.stage ? ` <span class="procedure-stage">${escapeHTML(proc.stage)}</span>` : ''}
${proc.committee ? ` — <span class="procedure-committee">${escapeHTML(proc.committee)}</span>` : ''}
${proc.bottleneck ? ' <span class="bottleneck-indicator">⚠ Bottleneck</span>' : ''}
</li>`;
}
/**
* Render a single parliamentary question as HTML
*
* @param q - Parliamentary question
* @returns HTML string
*/
function renderQuestion(q: ParliamentaryQuestion): string {
return `
<li class="qa-item">
<span class="qa-subject">${escapeHTML(q.subject)}</span>
${q.type ? ` <span class="qa-type">(${escapeHTML(q.type)})</span>` : ''}
${q.author ? ` — <span class="qa-author">${escapeHTML(q.author)}</span>` : ''}
</li>`;
}
// ─── Content builders ────────────────────────────────────────────────────────
/**
* Clamp a number to the 0–100 range.
*
* @param n - Number to clamp
* @returns Clamped value between 0 and 100
*/
function clamp0to100(n: number): number {
return Math.max(0, Math.min(100, n));
}
/**
* Map from band key to CSS class suffix used in `temp-*` class names.
*/
const BAND_CSS_CLASS: Record<PoliticalTemperatureBand, string> = {
low: 'temp-low',
moderate: 'temp-moderate',
high: 'temp-high',
veryHigh: 'temp-very-high',
};
/**
* Derive a language-agnostic temperature band from a 0–100 score.
*
* This is the single source of truth for score → band mapping.
* `computeWeekPoliticalTemperature()` uses this to derive the
* `temperature.band` value that the renderer later consumes, avoiding
* duplicated threshold logic.
*
* @param score - Clamped score (0–100)
* @returns Band key
*/
function temperatureBand(score: number): PoliticalTemperatureBand {
if (score >= 75) return 'veryHigh';
Iif (score >= 50) return 'high';
if (score >= 25) return 'moderate';
return 'low';
}
/**
* Resolve the localized temperature descriptor for a given band.
*
* @param band - Language-agnostic band key
* @param strings - Localized stakeholder strings
* @returns Localized descriptor (e.g. "Modéré", "高い")
*/
function localizedTempLabel(
band: PoliticalTemperatureBand,
strings: WeekAheadStakeholderStrings
): string {
const map: Record<PoliticalTemperatureBand, string> = {
low: strings.tempLow,
moderate: strings.tempModerate,
high: strings.tempHigh,
veryHigh: strings.tempVeryHigh,
};
return map[band];
}
/**
* Compute a composite political temperature score (0–100) indicating how
* contentious or urgent the upcoming parliamentary week is likely to be.
*
* The score is derived from the volume and diversity of scheduled events
* and questions — a pure scoring function with no side effects.
*
* @param events - Plenary / parliamentary events for the week
* @param questions - Parliamentary questions tabled for the week
* @returns Political temperature score and band
*/
export function computeWeekPoliticalTemperature(
events: readonly ParliamentEvent[],
questions: readonly ParliamentaryQuestion[]
): PoliticalTemperature {
// Base: volume-driven heuristic — more events & questions ⇒ higher temperature
const eventContribution = Math.min(events.length * 10, 50);
const questionContribution = Math.min(questions.length * 5, 30);
// Diversity bonus: distinct event types signal a broader agenda
const uniqueTypes = new Set(events.map((e) => e.type));
const diversityBonus = Math.min(uniqueTypes.size * 5, 20);
const raw = eventContribution + questionContribution + diversityBonus;
const score = clamp0to100(Math.round(raw));
return { score, band: temperatureBand(score) };
}
/**
* Determine impact level based on a count and a threshold for "high".
*
* @param count - Item count
* @param highThreshold - Minimum count for high impact
* @returns Impact level
*/
function impactFromCount(count: number, highThreshold: number): 'high' | 'medium' | 'low' {
if (count >= highThreshold) return 'high';
return count > 0 ? 'medium' : 'low';
}
/**
* Build a stakeholder impact matrix from scheduled events and legislative
* documents, assessing which groups are most affected by the agenda.
*
* Returns an empty section when no events or documents are available
* (graceful fallback). This function only constructs raw data; HTML
* escaping (for example via `escapeHTML()`) must be applied at render
* time by the caller (e.g. in `renderStakeholderSection()`).
*
* @param events - Parliament events for the upcoming week
* @param docs - Legislative documents expected in the period
* @param lang - Language code for localized labels (defaults to 'en')
* @returns Stakeholder impact section with rows
*/
export function buildStakeholderImpactMatrix(
events: readonly ParliamentEvent[],
docs: readonly LegislativeDocument[],
lang: string = 'en'
): StakeholderImpactSection {
if (events.length === 0 && docs.length === 0) {
return { rows: [] };
}
const strings = getLocalizedString(WEEK_AHEAD_STAKEHOLDER_STRINGS, lang);
const rows: StakeholderImpactRow[] = [];
const eventCount = events.length;
const docCount = docs.length;
const totalCount = eventCount + docCount;
if (eventCount > 0) {
rows.push({
stakeholder: strings.stakeholderPoliticalGroups,
impact: impactFromCount(eventCount, 3),
reason: strings.reasonEventsScheduled.replace('{count}', String(eventCount)),
});
}
if (docCount > 0) {
rows.push({
stakeholder: strings.stakeholderCivilSociety,
impact: impactFromCount(docCount, 3),
reason: strings.reasonDocumentsUnderReview.replace('{count}', String(docCount)),
});
}
rows.push({
stakeholder: strings.stakeholderIndustry,
impact: impactFromCount(totalCount, 5),
reason: strings.reasonIndustryRegulatoryAgenda,
});
rows.push({
stakeholder: strings.stakeholderEuCitizens,
impact: impactFromCount(eventCount, 3),
reason: strings.reasonCitizensDecisionsShapePolicy,
});
if (docCount > 0) {
rows.push({
stakeholder: strings.stakeholderNationalGovernments,
impact: impactFromCount(docCount, 3),
reason: strings.reasonDocumentsRequireTransposition.replace('{count}', String(docCount)),
});
}
rows.push({
stakeholder: strings.stakeholderEuInstitutions,
impact: impactFromCount(totalCount, 4),
reason: strings.reasonInstitutionsCoordination,
});
return { rows };
}
/**
* Render the stakeholder impact section as HTML.
*
* @param section - Stakeholder impact data
* @param temperature - Political temperature score
* @param lang - Language code for localized headings
* @returns HTML string, or empty string when section is empty
*/
function renderStakeholderSection(
section: StakeholderImpactSection,
temperature: PoliticalTemperature,
lang: string
): string {
if (section.rows.length === 0) return '';
const strings = getLocalizedString(WEEK_AHEAD_STAKEHOLDER_STRINGS, lang);
const tempClass = BAND_CSS_CLASS[temperature.band];
const tempDescriptor = localizedTempLabel(temperature.band, strings);
const tableRows = section.rows
.map(
(row) =>
`<tr>` +
`<td>${escapeHTML(row.stakeholder)}</td>` +
`<td class="impact-${escapeHTML(row.impact)}">${escapeHTML(row.impact)}</td>` +
`<td>${escapeHTML(row.reason)}</td>` +
`</tr>`
)
.join('');
return `
<section class="stakeholder-impact" lang="${escapeHTML(lang)}">
<h2>${escapeHTML(strings.heading)}</h2>
<div class="political-temperature ${tempClass}">
<span class="temp-label">${escapeHTML(strings.temperatureLabel)}:</span>
<span class="temp-score">${temperature.score}/100</span>
<span class="temp-descriptor">(${escapeHTML(tempDescriptor)})</span>
</div>
<table class="stakeholder-matrix">
<thead>
<tr>
<th scope="col">${escapeHTML(strings.stakeholderHeader)}</th>
<th scope="col">${escapeHTML(strings.impactHeader)}</th>
<th scope="col">${escapeHTML(strings.reasonHeader)}</th>
</tr>
</thead>
<tbody>
${tableRows}
</tbody>
</table>
</section>`;
}
/**
* Build the supplementary lede sentence about committee and pipeline counts.
*
* @param committeeCount - Number of committee meetings
* @param pipelineCount - Number of pipeline procedures
* @returns Sentence fragment or empty string
*/
function buildLedeDetail(committeeCount: number, pipelineCount: number): string {
if (committeeCount === 0 && pipelineCount === 0) return '';
const committeePart =
committeeCount > 0
? `${committeeCount} committee meeting${committeeCount !== 1 ? 's are' : ' is'} scheduled`
: '';
const pipelinePart =
pipelineCount > 0
? `${pipelineCount} legislative procedure${pipelineCount !== 1 ? 's are' : ' is'} advancing through the pipeline`
: '';
const conjunction = committeePart && pipelinePart ? ' and ' : '';
return ` Notably, ${committeePart}${conjunction}${pipelinePart}.`;
}
/**
* Build article content HTML from week-ahead data
*
* @param weekData - Aggregated week-ahead data
* @param dateRange - Date range for the article
* @param lang - Language code for editorial strings (default: 'en')
* @returns HTML content string
*/
export function buildWeekAheadContent(
weekData: WeekAheadData,
dateRange: DateRange,
lang = 'en'
): string {
const editorial = getLocalizedString(EDITORIAL_STRINGS, lang);
const strings = getLocalizedString(WEEK_AHEAD_STRINGS, lang);
const plenaryHtml =
weekData.events.length > 0
? weekData.events.map(renderPlenaryEvent).join('')
: `<p>${escapeHTML(strings.noPlenary)}</p>`;
const committeeSection =
weekData.committees.length > 0
? `<section class="committee-calendar">
<h2>${escapeHTML(strings.committeeMeetings)}</h2>
${weekData.committees.map(renderCommitteeMeeting).join('')}
</section>`
: '';
const documentsSection =
weekData.documents.length > 0
? `<section class="legislative-documents">
<h2>${escapeHTML(strings.legislativeDocuments)}</h2>
<ul class="document-list">${weekData.documents.map(renderLegislativeDocument).join('')}</ul>
</section>`
: '';
const pipelineSection =
weekData.pipeline.length > 0
? `<section class="legislative-pipeline">
<h2>${escapeHTML(strings.legislativePipeline)}</h2>
<ul class="pipeline-list">${weekData.pipeline.map(renderPipelineProcedure).join('')}</ul>
</section>`
: '';
const qaSection =
weekData.questions.length > 0
? `<section class="qa-schedule">
<h2>${escapeHTML(strings.parliamentaryQuestions)}</h2>
<ul class="qa-list">${weekData.questions.map(renderQuestion).join('')}</ul>
</section>`
: '';
const ledeDetail = buildLedeDetail(weekData.committees.length, weekData.pipeline.length);
const whyThisMattersSection = `
<section class="why-this-matters">
<h2>${escapeHTML(editorial.whyThisMatters)}</h2>
<p>${escapeHTML(editorial.parliamentaryContext)}: ${escapeHTML(editorial.sourceAttribution)} — parliamentary schedules determine the legislative agenda affecting EU citizens directly.</p>
</section>`;
// Stakeholder impact analysis
const stakeholderData = buildStakeholderImpactMatrix(weekData.events, weekData.documents, lang);
const temperature = computeWeekPoliticalTemperature(weekData.events, weekData.questions);
const stakeholderSection = renderStakeholderSection(stakeholderData, temperature, lang);
return `
<div class="article-content">
<section class="lede">
<p>${escapeHTML(strings.lede)} from ${escapeHTML(dateRange.start)} to ${escapeHTML(dateRange.end)}.${escapeHTML(ledeDetail)}</p>
</section>
${whyThisMattersSection}
<section class="plenary-schedule">
<h2>${escapeHTML(strings.plenarySessions)}</h2>
${plenaryHtml}
</section>
${committeeSection}
${documentsSection}
${pipelineSection}
${qaSection}
${stakeholderSection}
<!-- /article-content -->
</div>
`;
}
/**
* Build article keywords from week-ahead data
*
* @param weekData - Aggregated week-ahead data
* @returns Array of keyword strings
*/
export function buildKeywords(weekData: WeekAheadData): string[] {
const keywords = [KEYWORD_EUROPEAN_PARLIAMENT, 'week ahead', 'plenary', 'committees'];
for (const c of weekData.committees) {
if (c.committee && !keywords.includes(c.committee)) {
keywords.push(c.committee);
}
}
if (weekData.pipeline.length > 0) keywords.push('legislative pipeline');
if (weekData.questions.length > 0) keywords.push('parliamentary questions');
return keywords;
}
// ─── What-to-Watch section ─────────────────────────────────────────────────
/** CSS class for bottleneck-risk watch items */
const CSS_WATCH_HIGH = 'watch-high';
/** CSS class for bottlenecked procedure watch items */
const CSS_WATCH_PROCEDURE = 'watch-procedure';
/** CSS class for standard velocity watch items */
const CSS_WATCH_ITEM = 'watch-item';
/** Maximum number of non-bottleneck velocity items to include */
const WATCH_MAX_NORMAL_ITEMS = 3;
/**
* Build list items for high-risk velocities
*
* @param velocities - All legislative velocities
* @returns HTML list items for high bottleneck risk items
*/
function buildHighRiskItems(velocities: LegislativeVelocity[]): string {
return velocities
.filter((v) => v.bottleneckRisk === 'high')
.map(
(v) =>
`<li class="${CSS_WATCH_HIGH}">${escapeHTML(v.title)} — ` +
`Stage: ${escapeHTML(v.stage)} (bottleneck risk detected)</li>`
)
.join('');
}
/**
* Build list items for procedures flagged as bottlenecks
*
* @param procedures - All legislative procedures
* @returns HTML list items for bottlenecked procedures
*/
function buildBottleneckProcedureItems(procedures: LegislativeProcedure[]): string {
return procedures
.filter((p) => p.bottleneck === true)
.map(
(p) =>
`<li class="${CSS_WATCH_PROCEDURE}">${escapeHTML(p.title)} — ` +
`${escapeHTML(p.stage ?? 'in progress')} stage</li>`
)
.join('');
}
/**
* Build list items for normal-risk velocities
*
* @param velocities - All legislative velocities
* @returns HTML list items for the top normal-risk velocity items
*/
function buildNormalVelocityItems(velocities: LegislativeVelocity[]): string {
return velocities
.filter((v) => v.bottleneckRisk !== 'high')
.slice(0, WATCH_MAX_NORMAL_ITEMS)
.map(
(v) =>
`<li class="${CSS_WATCH_ITEM}">${escapeHTML(v.title)} — ` +
`predicted completion: ${escapeHTML(v.predictedCompletion)}</li>`
)
.join('');
}
/**
* Build predictive "What to Watch" analysis section showing legislative
* procedures and velocity data ordered by bottleneck risk.
* Returns an empty string when both input arrays are empty or yield no items.
*
* @param procedures - Legislative procedures to analyse
* @param velocities - Legislative velocity data (for example from a separate velocity analysis feed)
* @param language - BCP 47 language code used as the section lang attribute
* @returns HTML string for the "What to Watch" section
*/
export function buildWhatToWatchSection(
procedures: LegislativeProcedure[],
velocities: LegislativeVelocity[],
language: string
): string {
if (procedures.length === 0 && velocities.length === 0) return '';
const allItems =
buildHighRiskItems(velocities) +
buildBottleneckProcedureItems(procedures) +
buildNormalVelocityItems(velocities);
if (!allItems) return '';
const strings = getLocalizedString(WEEK_AHEAD_STRINGS, language);
return `
<section class="what-to-watch" lang="${escapeHTML(language)}">
<h2>${escapeHTML(strings.whatToWatch)}</h2>
<ul>
${allItems}
</ul>
</section>`;
}
|