All files / scripts/data-transformers/content-generators shared.ts

87.41% Statements 250/286
80.62% Branches 208/258
75.94% Functions 60/79
87.98% Lines 227/258

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                                            23x                                                                                                                                                                                                       311x 311x 26x 26x 311x 311x       23x 8x       4x                         186x         24x 24x 24x 7x 7x 1x 7x           24x 24x                   8x 8x 5x 5x     5x 5x 5x 5x 20x 20x     5x 5x   5x                                         304x 304x   304x 744x 744x 207x   744x 744x 150x       304x             760x 760x 1691x 1700x     760x                     304x     744x 744x   304x 8x 8x   8x     8x       304x 30x 30x 30x         304x                                                               8x       8x                       296x       296x                       453x         453x 453x   304x 2888x 2888x     304x 304x 304x     304x 304x 79x 79x 79x 79x 156x   79x   79x 150x         304x 304x 302x 360x 302x 662x   302x 302x 740x 740x   302x 9x 311x   302x 302x       304x 304x 304x     304x 304x 304x     304x 304x 304x     304x 304x 304x     304x 304x 304x     304x 304x 304x     304x 75x 75x 75x     304x 304x               304x 744x   304x 304x   304x                         304x 304x 304x       304x     304x 304x 304x 14x 290x 218x       304x 30x 30x         304x 1x     304x 71x     304x       14x       14x       218x       218x                       71x       71x       304x   744x 744x 744x   304x 9x   304x 6x   304x 15x     304x 30x 30x 30x       304x       9x       9x       6x       6x       15x       15x       30x       30x       258x       258x       744x 744x 304x   304x 9x   304x 15x   304x 287x   304x       9x       9x       15x       15x       287x       287x       304x     304x 744x 24x 24x   304x 304x         744x 304x 15x       304x 30x 30x         304x 289x     304x                       15x       15x                       289x       289x       77x 75x   75x 414x 152x 152x 96x   246x 152x 150x 246x     2x       75x 75x    
/**
 * @module data-transformers/content-generators/shared
 * @description Shared internal helpers and templates used by all content generators.
 * Contains TITLE_SUFFIX_TEMPLATES, keyword extraction, event/document matching helpers,
 * and the deep analysis section generator (5W framework).
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import { escapeHtml } from '../../html-utils.js';
import type { Language } from '../../types/language.js';
import type { RawDocument, RawCalendarEvent, CIAContext } from '../types.js';
import { L, normalizePartyKey } from '../helpers.js';
import { detectPolicyDomains } from '../policy-analysis.js';
 
/** Localise raw Riksdag document type codes for display (singular/plural-aware, multi-language). */
export type DocTypeLocalization = {
  singular: Partial<Record<Language, string>>;
  plural: Partial<Record<Language, string>>;
};
 
export const DOC_TYPE_DISPLAY: Readonly<Record<string, DocTypeLocalization>> = {
  prop: {
    singular: {
      en: 'Proposition', sv: 'Proposition', da: 'Proposition', no: 'Proposisjon',
      fi: 'Hallituksen esitys', de: 'Regierungsvorlage', fr: 'Projet de loi', es: 'Proposición',
      nl: 'Wetsvoorstel', ar: 'مقترح قانون', he: 'הצעת חוק', ja: '法案', ko: '정부 제출 법안', zh: '政府法案',
    },
    plural: {
      en: 'Propositions', sv: 'Propositioner', da: 'Propositioner', no: 'Proposisjoner',
      fi: 'Hallituksen esitykset', de: 'Regierungsvorlagen', fr: 'Projets de loi', es: 'Proposiciones',
      nl: 'Wetsvoorstellen', ar: 'مقترحات قوانين', he: 'הצעות חוק', ja: '法案', ko: '정부 제출 법안', zh: '政府法案',
    },
  },
  bet: {
    singular: {
      en: 'Committee Report', sv: 'Betänkande', da: 'Udvalgsbetænkning', no: 'Komitéinnstilling',
      fi: 'Valiokunnan mietintö', de: 'Ausschussbericht', fr: 'Rapport de commission', es: 'Informe de comisión',
      nl: 'Commissieverslag', ar: 'تقرير لجنة', he: 'דוח ועדה', ja: '委員会報告書', ko: '위원회 보고서', zh: '委员会报告',
    },
    plural: {
      en: 'Committee Reports', sv: 'Betänkanden', da: 'Udvalgsbetænkninger', no: 'Komitéinnstillinger',
      fi: 'Valiokunnan mietinnöt', de: 'Ausschussberichte', fr: 'Rapports de commission', es: 'Informes de comisión',
      nl: 'Commissieverslagen', ar: 'تقارير لجان', he: 'דוחות ועדה', ja: '委員会報告書', ko: '위원회 보고서', zh: '委员会报告',
    },
  },
  mot: {
    singular: {
      en: 'Motion', sv: 'Motion', da: 'Forslag', no: 'Forslag',
      fi: 'Aloite', de: 'Antrag', fr: 'Motion', es: 'Moción',
      nl: 'Motie', ar: 'مقترح', he: 'הצעה', ja: '動議', ko: '동의안', zh: '动议',
    },
    plural: {
      en: 'Motions', sv: 'Motioner', da: 'Forslag', no: 'Forslag',
      fi: 'Aloitteet', de: 'Anträge', fr: 'Motions', es: 'Mociones',
      nl: 'Moties', ar: 'مقترحات', he: 'הצעות', ja: '動議', ko: '동의안', zh: '动议',
    },
  },
  skr: {
    singular: {
      en: 'Government Communication', sv: 'Skrivelse', da: 'Regeringsskrivelse', no: 'Regjeringsskriv',
      fi: 'Valtioneuvoston kirjelmä', de: 'Regierungsschreiben', fr: 'Communication du gouvernement', es: 'Comunicación del gobierno',
      nl: 'Regeringsmededeling', ar: 'مذكرة حكومية', he: 'מכתב ממשלתי', ja: '政府通信文書', ko: '정부 통신문', zh: '政府公文',
    },
    plural: {
      en: 'Government Communications', sv: 'Skrivelser', da: 'Regeringsskrivelser', no: 'Regjeringsskriv',
      fi: 'Valtioneuvoston kirjelmät', de: 'Regierungsschreiben', fr: 'Communications du gouvernement', es: 'Comunicaciones del gobierno',
      nl: 'Regeringsmededelingen', ar: 'مذكرات حكومية', he: 'מכתבים ממשלתיים', ja: '政府通信文書', ko: '정부 통신문', zh: '政府公文',
    },
  },
  sfs: {
    singular: {
      en: 'Law/Statute', sv: 'Lag/förordning', da: 'Lov/forordning', no: 'Lov/forordning',
      fi: 'Laki/asetus', de: 'Gesetz/Verordnung', fr: 'Loi/Règlement', es: 'Ley/Reglamento',
      nl: 'Wet/Verordening', ar: 'قانون / لائحة', he: 'חוק/תקנה', ja: '法律/条例', ko: '법률/법규', zh: '法律/法规',
    },
    plural: {
      en: 'Laws/Statutes', sv: 'Lagar/förordningar', da: 'Love/forordninger', no: 'Lover/forordninger',
      fi: 'Lait/asetukset', de: 'Gesetze/Verordnungen', fr: 'Lois/Règlements', es: 'Leyes/Reglamentos',
      nl: 'Wetten/Verordeningen', ar: 'قوانين / لوائح', he: 'חוקים/תקנות', ja: '法律/条例', ko: '법률/법규', zh: '法律/法规',
    },
  },
  fpm: {
    singular: {
      en: 'EU Position Paper', sv: 'Faktapromemoria', da: 'EU-faktanota', no: 'EU-faktanotat',
      fi: 'EU-tietomuistio', de: 'EU-Positionspapier', fr: 'Note de position UE', es: 'Documento de posición de la UE',
      nl: 'EU-positiepaper', ar: 'ورقة موقف للاتحاد الأوروبي', he: 'מסמך עמדה של האיחוד האירופי', ja: 'EUポジションペーパー', ko: 'EU 입장 문서', zh: '欧盟立场文件',
    },
    plural: {
      en: 'EU Position Papers', sv: 'Faktapromemorior', da: 'EU-faktanotaer', no: 'EU-faktanotater',
      fi: 'EU-tietomuistiot', de: 'EU-Positionspapiere', fr: 'Notes de position UE', es: 'Documentos de posición de la UE',
      nl: 'EU-positiepapers', ar: 'أوراق موقف للاتحاد الأوروبي', he: 'מסמכי עמדה של האיחוד האירופי', ja: 'EUポジションペーパー', ko: 'EU 입장 문서', zh: '欧盟立场文件',
    },
  },
  pressm: {
    singular: {
      en: 'Press Release', sv: 'Pressmeddelande', da: 'Pressemeddelelse', no: 'Pressemelding',
      fi: 'Lehdistötiedote', de: 'Pressemitteilung', fr: 'Communiqué de presse', es: 'Comunicado de prensa',
      nl: 'Persbericht', ar: 'بيان صحفي', he: 'הודעה לעיתונות', ja: 'プレスリリース', ko: '보도자료', zh: '新闻稿',
    },
    plural: {
      en: 'Press Releases', sv: 'Pressmeddelanden', da: 'Pressemeddelelser', no: 'Pressemeldinger',
      fi: 'Lehdistötiedotteet', de: 'Pressemitteilungen', fr: 'Communiqués de presse', es: 'Comunicados de prensa',
      nl: 'Persberichten', ar: 'بيانات صحفية', he: 'הודעות לעיתונות', ja: 'プレスリリース', ko: '보도자료', zh: '新闻稿',
    },
  },
  ext: {
    singular: {
      en: 'External Reference', sv: 'Extern referens', da: 'Ekstern reference', no: 'Ekstern referanse',
      fi: 'Ulkoinen viite', de: 'Externe Referenz', fr: 'Référence externe', es: 'Referencia externa',
      nl: 'Externe referentie', ar: 'مرجع خارجي', he: 'הפניה חיצונית', ja: '外部参照', ko: '외부 참조', zh: '外部参考',
    },
    plural: {
      en: 'External References', sv: 'Externa referenser', da: 'Eksterne referencer', no: 'Eksterne referanser',
      fi: 'Ulkoiset viitteet', de: 'Externe Referenzen', fr: 'Références externes', es: 'Referencias externas',
      nl: 'Externe referenties', ar: 'مراجع خارجية', he: 'הפניות חיצוניות', ja: '外部参照', ko: '외부 참조', zh: '外部参考',
    },
  },
};
 
export function localizeDocType(code: string, lang: Language | string, count?: number): string {
  const entry = DOC_TYPE_DISPLAY[code];
  if (!entry) return code;
  const usePlural = count !== 1;
  const primary = usePlural ? entry.plural : entry.singular;
  const fallback = usePlural ? entry.singular : entry.plural;
  return primary[lang as Language] ?? primary.en ?? fallback[lang as Language] ?? fallback.en ?? code;
}
 
/** Per-language title-suffix templates for inverted-pyramid lede construction. */
export const TITLE_SUFFIX_TEMPLATES: Readonly<Record<string, (t: string) => string>> = {
  sv: t => ` — inklusive "${t}"`,
  da: t => ` — herunder "${t}"`,
  no: t => ` — inkludert "${t}"`,
  fi: t => ` — mukaan lukien "${t}"`,
  de: t => ` — darunter "${t}"`,
  fr: t => ` — notamment "${t}"`,
  es: t => ` — incluyendo "${t}"`,
  nl: t => ` — inclusief "${t}"`,
  ar: t => ` — بما فيها "${t}"`,
  he: t => ` — כולל "${t}"`,
  ja: t => `、「${t}」を含む`,
  ko: t => `, "${t}" 포함`,
  zh: t => `,包括"${t}"`,
};
 
/** Extract meaningful keywords from text for cross-reference matching (min 2 chars, captures EU, KU, etc.; splits on whitespace, hyphens, and commas) */
function extractKeywords(text: string): string[] {
  return text.toLowerCase().split(/[\s,–-]+/u).filter(w => w.length >= 2);
}
 
/** Find documents related to a calendar event by organ match or keyword overlap (max 3) */
export function findRelatedDocuments(event: RawCalendarEvent, documents: RawDocument[]): RawDocument[] {
  const eventOrgan = event.organ ?? '';
  const keywords = extractKeywords(event.rubrik ?? event.titel ?? event.title ?? '');
  return documents.filter(doc => {
    const docOrgan = doc.organ ?? doc.committee ?? '';
    if (eventOrgan && docOrgan && eventOrgan.toLowerCase() === docOrgan.toLowerCase()) return true;
    const docText = (doc.titel ?? doc.title ?? '').toLowerCase();
    return keywords.some(kw => docText.includes(kw));
  }).slice(0, 3);
}
 
/** Find written questions related to a calendar event by keyword overlap (max 3) */
export function findRelatedQuestions(event: RawCalendarEvent, questions: RawDocument[]): RawDocument[] {
  const keywords = extractKeywords(event.rubrik ?? event.titel ?? event.title ?? '');
  return questions.filter(q => {
    const qText = (q.titel ?? q.title ?? '').toLowerCase();
    return keywords.some(kw => qText.includes(kw));
  }).slice(0, 3);
}
 
/** Extract targeted minister name from interpellation summary "till MINISTER" header line.
 *  Strips trailing topic clauses ("om X", "angående Y", etc.) and punctuation. */
export function extractMinister(summary: string): string {
  // Use non-newline whitespace ([^\S\n]+) so we don't cross into the next line
  const m = summary.match(/\btill[^\S\n]+([^\n]+)/i);
  if (!m) return '';
  const raw = m[1].trim();
  Iif (!raw) return '';
 
  // Remove common trailing topic clauses and punctuation
  const lowerRaw = raw.toLowerCase();
  const stopPhrases = [' om ', ' angående ', ' rörande ', ' beträffande '];
  let end = raw.length;
  for (const phrase of stopPhrases) {
    const idx = lowerRaw.indexOf(phrase);
    Iif (idx !== -1 && idx < end) end = idx;
  }
  // Cut at terminating punctuation if it comes earlier
  const punctIdx = raw.search(/[?:;.,]/);
  Iif (punctIdx !== -1 && punctIdx < end) end = punctIdx;
 
  return raw.slice(0, end).trim();
}
 
// ---------------------------------------------------------------------------
// Deep Analysis Section (5W Framework)
// ---------------------------------------------------------------------------
 
/** Options for generating the deep analysis section */
export interface DeepAnalysisOptions {
  documents: RawDocument[];
  lang: Language | string;
  cia?: CIAContext;
  articleType: string;
  /** Extra context sentences to inject into the "Why" subsection */
  whyContext?: string;
}
 
/**
 * Extract unique party names from a set of documents for "Who" analysis.
 */
function extractKeyActors(docs: RawDocument[]): { parties: Map<string, number>; authors: string[] } {
  const parties = new Map<string, number>();
  const authorSet = new Set<string>();
 
  for (const doc of docs) {
    const party = normalizePartyKey(doc.parti);
    if (party && party !== 'other') {
      parties.set(party, (parties.get(party) ?? 0) + 1);
    }
    const author = doc.intressent_namn || doc.author || '';
    if (author && author !== 'Unknown' && author.length > 1) {
      authorSet.add(author);
    }
  }
 
  return { parties, authors: Array.from(authorSet).slice(0, 8) };
}
 
/**
 * Aggregate policy domains across all documents for "What" analysis.
 */
function aggregateDomains(docs: RawDocument[], lang: Language | string): Map<string, number> {
  const domainCounts = new Map<string, number>();
  for (const doc of docs) {
    for (const domain of detectPolicyDomains(doc, lang)) {
      domainCounts.set(domain, (domainCounts.get(domain) ?? 0) + 1);
    }
  }
  return domainCounts;
}
 
/**
 * Determine winners and losers based on document patterns and CIA context.
 */
function analyseWinnersLosers(
  docs: RawDocument[],
  cia: CIAContext | undefined,
  lang: Language | string,
): string {
  const parts: string[] = [];
 
  // Count government vs opposition documents
  const govDocs = docs.filter(d => d.doktyp === 'prop' || d.doktyp === 'bet' || d.doktyp === 'skr');
  const oppDocs = docs.filter(d => d.doktyp === 'mot');
 
  if (govDocs.length > 0 && oppDocs.length > 0) {
    const ratio = govDocs.length / Math.max(1, oppDocs.length);
    Iif (ratio > 2) {
      parts.push(govAdvantageText(lang, govDocs.length, oppDocs.length));
    } else if (Iratio < 0.5) {
      parts.push(oppPressureText(lang, oppDocs.length));
    } else {
      parts.push(balancedText(lang));
    }
  }
 
  if (cia) {
    const margin = cia.coalitionStability?.majorityMargin ?? 0;
    const stability = cia.coalitionStability?.stabilityScore ?? 100;
    Iif (margin <= 3 && stability < 60) {
      parts.push(coalitionRiskText(lang, margin));
    }
  }
 
  return parts.join(' ') || neutralText(lang);
}
 
function govAdvantageText(lang: Language | string, gov: number, opp: number): string {
  const templates: Record<string, string> = {
    en: `The governing coalition holds the initiative with ${gov} legislative items versus ${opp} opposition motions, suggesting strong agenda control.`,
    sv: `Regeringskoalitionen håller initiativet med ${gov} lagstiftningsärenden mot ${opp} oppositionsyrkanden, vilket tyder på stark agendakontroll.`,
    da: `Regeringskoalitionen har initiativet med ${gov} lovgivningspunkter mod ${opp} oppositionsforslag.`,
    no: `Regjeringskoalisjonen holder initiativet med ${gov} lovforslag mot ${opp} opposisjonsforslag.`,
    fi: `Hallituskoalitio pitää aloitteen ${gov} lainsäädäntöasialla vastaan ${opp} opposition aloitetta.`,
    de: `Die Regierungskoalition hält die Initiative mit ${gov} Gesetzgebungspunkten gegenüber ${opp} Oppositionsanträgen.`,
    fr: `La coalition gouvernementale tient l'initiative avec ${gov} points législatifs contre ${opp} motions de l'opposition.`,
    es: `La coalición gobernante mantiene la iniciativa con ${gov} puntos legislativos frente a ${opp} mociones de la oposición.`,
    nl: `De regeringscoalitie houdt het initiatief met ${gov} wetgevingspunten versus ${opp} oppositiemoties.`,
    ar: `يحتفظ الائتلاف الحاكم بالمبادرة بـ${gov} بنداً تشريعياً مقابل ${opp} اقتراحات معارضة.`,
    he: `הקואליציה שומרת על היוזמה עם ${gov} סעיפי חקיקה מול ${opp} הצעות אופוזיציה.`,
    ja: `与党連合は${gov}の立法項目で主導権を握り、野党の${opp}動議に対して優位に立っています。`,
    ko: `여당 연합은 ${gov}건의 입법 항목으로 주도권을 유지하며, 야당의 ${opp}건 동의에 대해 우위를 보이고 있습니다.`,
    zh: `执政联盟以${gov}项立法议题保持主动权,对比反对党的${opp}项动议。`,
  };
  return templates[lang as string] ?? templates.en;
}
 
function oppPressureText(lang: Language | string, opp: number): string {
  const templates: Record<string, string> = {
    en: `The opposition is applying significant pressure with ${opp} motions, signalling broad dissatisfaction with government policy.`,
    sv: `Oppositionen utövar betydande tryck med ${opp} motioner, vilket signalerar brett missnöje med regeringens politik.`,
  };
  return templates[lang as string] ?? templates.en;
}
 
function balancedText(lang: Language | string): string {
  const templates: Record<string, string> = {
    en: 'The balance between government and opposition activity suggests a competitive legislative environment where neither side dominates.',
    sv: 'Balansen mellan regerings- och oppositionsaktivitet tyder på en konkurrensutsatt lagstiftningsmiljö.',
  };
  return templates[lang as string] ?? templates.en;
}
 
function coalitionRiskText(lang: Language | string, margin: number): string {
  const templates: Record<string, string> = {
    en: `With a majority margin of only ${margin} seats, the coalition faces elevated risk of legislative defeats on contested measures.`,
    sv: `Med en majoritetsmarginal på bara ${margin} mandat löper koalitionen förhöjd risk för nederlag vid omstridda omröstningar.`,
  };
  return templates[lang as string] ?? templates.en;
}
 
function neutralText(lang: Language | string): string {
  const templates: Record<string, string> = {
    en: 'The political landscape remains fluid, with both government and opposition positioning for advantage.',
    sv: 'Det politiska landskapet förblir rörligt, med både regering och opposition som positionerar sig.',
  };
  return templates[lang as string] ?? templates.en;
}
 
/**
 * Generate a comprehensive Deep Analysis section following the 5W framework
 * (Who, What, When, Why, Winners/Losers) plus impact, consequences, and critical
 * assessment subsections. This section is designed for highly analytical readers
 * who seek multi-perspective intelligence on parliamentary developments.
 *
 * @returns HTML string for the deep analysis section, or empty string if insufficient data
 */
export function generateDeepAnalysisSection(opts: DeepAnalysisOptions): string {
  const { documents, lang, cia, articleType, whyContext } = opts;
 
  // Deep analysis requires at least 2 documents for cross-document insights
  // in standard article types. For deep-inspection articles, allow single-
  // document analysis since the whole article is dedicated to in-depth review.
  const minDocs = articleType === 'deep-inspection' ? 1 : 2;
  if (!documents || documents.length < minDocs) return '';
 
  const lbl = (key: string): string => {
    const val = L(lang, key);
    return typeof val === 'string' ? val : key;
  };
 
  const parts: string[] = [];
  parts.push(`\n    <section class="deep-analysis" aria-label="${escapeHtml(lbl('deepAnalysis'))}">`);
  parts.push(`    <h2>${escapeHtml(lbl('deepAnalysis'))}</h2>`);
 
  // ── WHO: Key Actors ────────────────────────────────────────────────────────
  const { parties, authors } = extractKeyActors(documents);
  if (parties.size > 0 || authors.length > 0) {
    parts.push(`    <h3>${escapeHtml(lbl('deepAnalysisWho'))}</h3>`);
    Eif (parties.size > 0) {
      const sortedParties = [...parties.entries()].sort((a, b) => b[1] - a[1]);
      const partyList = sortedParties
        .map(([p, count]) => `<strong>${escapeHtml(p)}</strong> (${count})`)
        .join(', ');
      parts.push(`    <p>${partyList}</p>`);
    }
    if (authors.length > 0) {
      parts.push(`    <p>${authors.map(a => escapeHtml(a)).join(', ')}</p>`);
    }
  }
 
  // ── WHAT: What Happened ────────────────────────────────────────────────────
  const domains = aggregateDomains(documents, lang);
  if (domains.size > 0) {
    parts.push(`    <h3>${escapeHtml(lbl('deepAnalysisWhat'))}</h3>`);
    const sortedDomains = [...domains.entries()].sort((a, b) => b[1] - a[1]);
    const domainItems = sortedDomains.slice(0, 6)
      .map(([d, c]) => `${escapeHtml(d)} (${c})`)
      .join(', ');
    const docTypes = new Map<string, number>();
    for (const doc of documents) {
      const t = doc.doktyp || doc.documentType || 'other';
      docTypes.set(t, (docTypes.get(t) ?? 0) + 1);
    }
    const typeList = [...docTypes.entries()]
      .sort((a, b) => b[1] - a[1])
      .map(([t, c]) => `${escapeHtml(localizeDocType(t, lang, c))}: ${c}`)
      .join(', ');
    parts.push(`    <p>${domainItems}</p>`);
    parts.push(`    <p><em>${typeList}</em></p>`);
  }
 
  // ── WHEN: Timeline & Context ───────────────────────────────────────────────
  parts.push(`    <h3>${escapeHtml(lbl('deepAnalysisWhen'))}</h3>`);
  const timelineContext = generateTimelineContext(documents, lang, articleType);
  parts.push(`    <p>${timelineContext}</p>`);
 
  // ── WHY: Why This Matters ──────────────────────────────────────────────────
  parts.push(`    <h3>${escapeHtml(lbl('deepAnalysisWhy'))}</h3>`);
  const whyText = generateWhyAnalysis(documents, lang, cia, whyContext);
  parts.push(`    <p>${whyText}</p>`);
 
  // ── WINNERS & LOSERS ───────────────────────────────────────────────────────
  parts.push(`    <h3>${escapeHtml(lbl('deepAnalysisWinners'))}</h3>`);
  const winnersText = analyseWinnersLosers(documents, cia, lang);
  parts.push(`    <p>${winnersText}</p>`);
 
  // ── POLITICAL IMPACT ───────────────────────────────────────────────────────
  parts.push(`    <h3>${escapeHtml(lbl('deepAnalysisImpact'))}</h3>`);
  const impactText = generateImpactAnalysis(documents, lang, cia);
  parts.push(`    <p>${impactText}</p>`);
 
  // ── ACTIONS & CONSEQUENCES ─────────────────────────────────────────────────
  parts.push(`    <h3>${escapeHtml(lbl('deepAnalysisConsequences'))}</h3>`);
  const consequencesText = generateConsequencesAnalysis(documents, lang, articleType);
  parts.push(`    <p>${consequencesText}</p>`);
 
  // ── CRITICAL ASSESSMENT ────────────────────────────────────────────────────
  parts.push(`    <h3>${escapeHtml(lbl('deepAnalysisCritical'))}</h3>`);
  const criticalText = generateCriticalAssessment(documents, lang, cia);
  parts.push(`    <p>${criticalText}</p>`);
 
  // ── MULTIPLE PERSPECTIVES ──────────────────────────────────────────────────
  if (parties.size >= 2) {
    parts.push(`    <h3>${escapeHtml(lbl('deepAnalysisPerspectives'))}</h3>`);
    const perspectivesText = generatePerspectivesAnalysis(documents, lang, parties);
    parts.push(`    <p>${perspectivesText}</p>`);
  }
 
  parts.push('    </section>\n');
  return parts.join('\n');
}
 
// ---------------------------------------------------------------------------
// Deep Analysis subsection generators
// ---------------------------------------------------------------------------
 
function generateTimelineContext(docs: RawDocument[], lang: Language | string, articleType: string): string {
  const count = docs.length;
  const committees = new Set(docs.map(d => d.organ || d.committee || '').filter(Boolean));
 
  const prospective = articleType.includes('ahead');
  const retrospective = articleType.includes('review');
 
  const templates: Record<string, Record<string, string>> = {
    en: {
      prospective: `${count} items are scheduled across ${committees.size} committee${committees.size !== 1 ? 's' : ''}, creating a dense legislative calendar that demands close monitoring. Each item moves through committee review and chamber debate — timing and sequencing will determine which issues reach a vote.`,
      retrospective: `Over the review period, ${count} parliamentary actions were recorded across ${committees.size} committee${committees.size !== 1 ? 's' : ''}. The volume and distribution of activity reveals the government's priorities and the opposition's strategic responses.`,
      default: `${count} parliamentary items across ${committees.size} active committee${committees.size !== 1 ? 's' : ''} define the current legislative landscape. The pace of activity signals the political urgency driving these proceedings.`,
    },
    sv: {
      prospective: `${count} ärenden är planerade i ${committees.size} utskott, vilket skapar en tät lagstiftningskalender som kräver noggrann bevakning.`,
      retrospective: `Under granskningsperioden registrerades ${count} parlamentariska åtgärder i ${committees.size} utskott. Volymen och fördelningen avslöjar regeringens prioriteringar.`,
      default: `${count} riksdagsärenden i ${committees.size} aktiva utskott definierar det aktuella lagstiftningslandskapet.`,
    },
  };
 
  const langTemplates = templates[lang as string] ?? templates.en;
  const key = prospective ? 'prospective' : retrospective ? 'retrospective' : 'default';
  return langTemplates[key] ?? langTemplates.default;
}
 
function generateWhyAnalysis(docs: RawDocument[], lang: Language | string, cia: CIAContext | undefined, extraContext?: string): string {
  const parts: string[] = [];
 
  // Domain breadth signals policy ambition
  const domains = aggregateDomains(docs, lang);
  const domainCount = domains.size;
  if (domainCount >= 4) {
    parts.push(broadAgendaText(lang, domainCount));
  } else if (domainCount >= 2) {
    parts.push(focusedAgendaText(lang, domainCount));
  }
 
  // Coalition stability context
  if (cia) {
    const stability = cia.coalitionStability?.stabilityScore ?? 100;
    Iif (stability < 50) {
      parts.push(instabilityText(lang));
    }
  }
 
  if (extraContext) {
    parts.push(escapeHtml(extraContext));
  }
 
  if (parts.length === 0) {
    parts.push(defaultWhyText(lang));
  }
 
  return parts.join(' ');
}
 
function broadAgendaText(lang: Language | string, n: number): string {
  const templates: Record<string, string> = {
    en: `With ${n} policy domains in play, this represents a broad legislative push that will shape multiple aspects of Swedish society. The breadth of activity makes this a critical period for understanding the government's strategic direction.`,
    sv: `Med ${n} politikområden i spel representerar detta en bred lagstiftningssatsning som kommer att forma flera aspekter av det svenska samhället.`,
  };
  return templates[lang as string] ?? templates.en;
}
 
function focusedAgendaText(lang: Language | string, n: number): string {
  const templates: Record<string, string> = {
    en: `Activity concentrated in ${n} policy domains suggests targeted legislative priorities rather than broad reform, making each initiative particularly consequential.`,
    sv: `Aktivitet koncentrerad till ${n} politikområden tyder på riktade lagstiftningsprioriteringar snarare än bred reform.`,
  };
  return templates[lang as string] ?? templates.en;
}
 
function instabilityText(lang: Language | string): string {
  const templates: Record<string, string> = {
    en: 'The current coalition instability adds significant uncertainty to all legislative proceedings. Any controversial measure could become a confidence test.',
    sv: 'Den nuvarande koalitionsinstabiliteten tillför betydande osäkerhet till alla lagstiftningsförfaranden.',
  };
  return templates[lang as string] ?? templates.en;
}
 
function defaultWhyText(lang: Language | string): string {
  const templates: Record<string, string> = {
    en: 'These parliamentary developments carry significance for Swedish governance, reflecting ongoing policy debates and power dynamics within the Riksdag.',
    sv: 'Dessa riksdagshändelser har betydelse för svensk styrning och speglar pågående politiska debatter och maktdynamik.',
  };
  return templates[lang as string] ?? templates.en;
}
 
function generateImpactAnalysis(docs: RawDocument[], lang: Language | string, cia: CIAContext | undefined): string {
  const parts: string[] = [];
 
  const propCount = docs.filter(d => d.doktyp === 'prop').length;
  const motCount = docs.filter(d => d.doktyp === 'mot').length;
  const betCount = docs.filter(d => d.doktyp === 'bet').length;
 
  if (propCount > 0) {
    parts.push(propImpactText(lang, propCount));
  }
  if (betCount > 0) {
    parts.push(betImpactText(lang, betCount));
  }
  if (motCount > 0) {
    parts.push(motImpactText(lang, motCount));
  }
 
  if (cia) {
    const margin = cia.coalitionStability?.majorityMargin ?? 0;
    Eif (margin <= 5) {
      parts.push(thinMajorityImpactText(lang, margin));
    }
  }
 
  return parts.join(' ') || genericImpactText(lang);
}
 
function propImpactText(lang: Language | string, n: number): string {
  const t: Record<string, string> = {
    en: `${n} government proposition${n !== 1 ? 's' : ''} will, if adopted, directly alter Swedish law and policy, affecting citizens, businesses, and institutions.`,
    sv: `${n} regeringsproposition${n !== 1 ? 'er' : ''} kommer, om de antas, att direkt ändra svensk lag och politik.`,
  };
  return t[lang as string] ?? t.en;
}
 
function betImpactText(lang: Language | string, n: number): string {
  const t: Record<string, string> = {
    en: `${n} committee report${n !== 1 ? 's' : ''} represent${n === 1 ? 's' : ''} the culmination of legislative review, with recommendations that guide chamber votes.`,
    sv: `${n} betänkande${n !== 1 ? 'n' : ''} representerar kulmen av lagstiftningsöversynen.`,
  };
  return t[lang as string] ?? t.en;
}
 
function motImpactText(lang: Language | string, n: number): string {
  const t: Record<string, string> = {
    en: `${n} opposition motion${n !== 1 ? 's' : ''} challenge${n === 1 ? 's' : ''} the government's position, even though most motions are historically rejected; they signal future electoral battlegrounds.`,
    sv: `${n} oppositionsmotion${n !== 1 ? 'er' : ''} utmanar regeringens position och signalerar framtida valfrågor.`,
  };
  return t[lang as string] ?? t.en;
}
 
function thinMajorityImpactText(lang: Language | string, margin: number): string {
  const t: Record<string, string> = {
    en: `The thin majority margin of ${margin} seat${margin !== 1 ? 's' : ''} means any defection could defeat government measures, amplifying the political stakes.`,
    sv: `Den tunna majoritetsmarginal på ${margin} mandat innebär att varje avhopp kan fälla regeringens förslag.`,
  };
  return t[lang as string] ?? t.en;
}
 
function genericImpactText(lang: Language | string): string {
  const t: Record<string, string> = {
    en: 'The legislative activity reflects the ongoing interplay between governing ambition and opposition scrutiny that characterises Swedish parliamentary democracy.',
    sv: 'Lagstiftningsaktiviteten speglar det pågående samspelet mellan regeringsambitioner och oppositionens granskning.',
  };
  return t[lang as string] ?? t.en;
}
 
function generateConsequencesAnalysis(docs: RawDocument[], lang: Language | string, _articleType: string): string {
  const propCount = docs.filter(d => d.doktyp === 'prop').length;
  const motCount = docs.filter(d => d.doktyp === 'mot').length;
  const parts: string[] = [];
 
  if (propCount > 0) {
    parts.push(propConsequencesText(lang, propCount));
  }
  if (motCount > 0) {
    parts.push(motConsequencesText(lang, motCount));
  }
  if (parts.length === 0) {
    parts.push(genericConsequencesText(lang));
  }
  return parts.join(' ');
}
 
function propConsequencesText(lang: Language | string, n: number): string {
  const t: Record<string, string> = {
    en: `If adopted, these ${n} proposition${n !== 1 ? 's' : ''} will trigger implementation across government agencies, requiring regulatory changes, budget allocations, and administrative adaptation. Failure to pass would signal coalition weakness and embolden the opposition.`,
    sv: `Om de antas kommer dessa ${n} proposition${n !== 1 ? 'er' : ''} att utlösa implementering i myndigheter, kräva regeländringar och budgetanpassningar.`,
  };
  return t[lang as string] ?? t.en;
}
 
function motConsequencesText(lang: Language | string, n: number): string {
  const t: Record<string, string> = {
    en: `The ${n} opposition motion${n !== 1 ? 's' : ''}, while likely to be rejected, establish the policy alternatives that opposition parties will champion in the next election cycle. Rejection does not diminish their strategic value as campaign ammunition.`,
    sv: `De ${n} oppositionsmotion${n !== 1 ? 'erna' : 'en'}, även om de troligen avslås, etablerar policyalternativ för nästa valcykel.`,
  };
  return t[lang as string] ?? t.en;
}
 
function genericConsequencesText(lang: Language | string): string {
  const t: Record<string, string> = {
    en: 'The outcomes of these proceedings will cascade through committee deliberations, chamber votes, and ultimately into policy implementation — or be shelved, affecting political credibility and future legislative strategy.',
    sv: 'Resultaten av dessa ärenden kommer att kaskadgenomslag genom utskottsbehandling, kammarröstning och slutligen policyimplementering.',
  };
  return t[lang as string] ?? t.en;
}
 
function generateCriticalAssessment(docs: RawDocument[], lang: Language | string, cia: CIAContext | undefined): string {
  const parts: string[] = [];
 
  // Check for single-party dominance in motions (potential echo chamber)
  const motionParties = new Map<string, number>();
  docs.filter(d => d.doktyp === 'mot').forEach(d => {
    const p = normalizePartyKey(d.parti);
    if (p && p !== 'other') motionParties.set(p, (motionParties.get(p) ?? 0) + 1);
  });
  const totalMotions = [...motionParties.values()].reduce((a, b) => a + b, 0);
  Iif (motionParties.size === 1 && totalMotions > 3) {
    parts.push(singlePartyDominanceText(lang));
  }
 
  // Check for lack of debate data (information gap)
  const withSpeeches = docs.filter(d => d.speeches && d.speeches.length > 0).length;
  if (withSpeeches === 0 && docs.length > 3) {
    parts.push(noDebateDataText(lang));
  }
 
  // Thin majority risk assessment
  if (cia) {
    const stability = cia.coalitionStability?.stabilityScore ?? 100;
    Iif (stability < 40) {
      parts.push(criticalStabilityText(lang));
    }
  }
 
  if (parts.length === 0) {
    parts.push(defaultCriticalText(lang));
  }
 
  return parts.join(' ');
}
 
function singlePartyDominanceText(lang: Language | string): string {
  const t: Record<string, string> = {
    en: 'Opposition activity is dominated by a single party, which may indicate either strategic focus or a failure of other parties to engage. Watch for whether this concentration reflects genuine policy leadership or internal opposition dysfunction.',
    sv: 'Oppositionsaktiviteten domineras av ett enda parti, vilket kan tyda på antingen strategiskt fokus eller andra partiers misslyckande att engagera sig.',
  };
  return t[lang as string] ?? t.en;
}
 
function noDebateDataText(lang: Language | string): string {
  const t: Record<string, string> = {
    en: 'No chamber debate data is available for these items, limiting our ability to assess the depth of parliamentary deliberation. This information gap should be monitored — the quality of democracy depends on substantive debate, not just procedural passage.',
    sv: 'Inga debattdata från kammaren finns tillgängliga, vilket begränsar vår förmåga att bedöma parlamentariskt deliberationsdjup.',
  };
  return t[lang as string] ?? t.en;
}
 
function criticalStabilityText(lang: Language | string): string {
  const t: Record<string, string> = {
    en: 'Coalition stability has deteriorated to critical levels. The risk of a government crisis is non-trivial, and any procedural surprise could trigger a confidence vote. All legislative analysis must be read through this lens of instability.',
    sv: 'Koalitionsstabiliteten har försämrats till kritiska nivåer. Risken för en regeringskris är icke-trivial.',
  };
  return t[lang as string] ?? t.en;
}
 
function defaultCriticalText(lang: Language | string): string {
  const t: Record<string, string> = {
    en: 'Standard parliamentary procedures are being followed, but vigilance is warranted. The gap between legislative intent and implementation often reveals the true political winners and losers. Monitor committee amendments and chamber debate quality for the full picture.',
    sv: 'Standardiserade parlamentariska förfaranden följs, men vaksamhet är motiverad. Klyftan mellan lagstiftningsavsikt och implementering avslöjar ofta de verkliga vinnarna och förlorarna.',
  };
  return t[lang as string] ?? t.en;
}
 
function generatePerspectivesAnalysis(docs: RawDocument[], lang: Language | string, parties: Map<string, number>): string {
  const sortedParties = [...parties.entries()].sort((a, b) => b[1] - a[1]).slice(0, 4);
  const partyAnalyses: string[] = [];
 
  for (const [party, count] of sortedParties) {
    const partyDocs = docs.filter(d => normalizePartyKey(d.parti) === party);
    const partyDomains = aggregateDomains(partyDocs, lang);
    const topDomains = [...partyDomains.entries()]
      .sort((a, b) => b[1] - a[1])
      .slice(0, 2)
      .map(([d]) => d);
    if (topDomains.length > 0) {
      partyAnalyses.push(
        `<strong>${escapeHtml(party)}</strong> (${count}): ${topDomains.map(d => escapeHtml(d)).join(', ')}`
      );
    } else {
      partyAnalyses.push(`<strong>${escapeHtml(party)}</strong> (${count})`);
    }
  }
 
  Iif (partyAnalyses.length === 0) return '';
  return partyAnalyses.join(' · ');
}