All files / generators deep-analysis-content.ts

94.11% Statements 64/68
86.66% Branches 26/30
100% Functions 20/20
98.21% Lines 55/56

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                                                                                78x 78x                             78x 201x 75x                                 78x 77x 169x   77x                                 78x 78x                           144x                                 144x   83x   41x   20x                                       78x 66x     144x           66x                                                                 78x           390x 78x 77x     385x           77x                                                         167x   7x   86x   73x   1x                                                               78x 73x     167x               73x                                                               78x 47x     47x             47x                             78x 77x                                                     80x   78x   78x 78x 78x 78x 78x         78x 78x           78x         78x     78x                     78x   78x            
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Generators/DeepAnalysisContent
 * @description Pure functions for building the deep political analysis HTML
 * section using the "5W + Impact" framework. This section is injected into
 * every article type to provide parliament-intelligence-grade analysis.
 *
 * The framework covers:
 * - **What**: Core subject
 * - **Who**: Key actors (political groups, rapporteurs, MEPs, institutions)
 * - **When**: Timeline and key dates
 * - **Why**: Root causes and strategic motivations
 * - **Winners/Losers**: Stakeholder impact assessment
 * - **Impact**: Multi-perspective consequences (political, economic, social, legal, geopolitical)
 * - **Actions → Consequences**: Causal chains from decisions to outcomes
 * - **Mistakes**: Miscalculations and missed opportunities
 * - **Outlook**: Strategic forward look
 */
 
import { escapeHTML } from '../utils/file-utils.js';
import { getLocalizedString, DEEP_ANALYSIS_STRINGS } from '../constants/languages.js';
import type {
  DeepAnalysis,
  StakeholderOutcome,
  ActionConsequence,
  PoliticalMistake,
} from '../types/index.js';
 
// ─── Sub-section builders ────────────────────────────────────────────────────
 
/**
 * Build the "What" sub-section
 *
 * @param what - Description of what happened
 * @param heading - Localized heading
 * @returns HTML string
 */
function buildWhatSection(what: string, heading: string): string {
  Iif (!what) return '';
  return `
            <div class="analysis-what">
              <h3>${escapeHTML(heading)}</h3>
              <p>${escapeHTML(what)}</p>
            </div>`;
}
 
/**
 * Build the "Who" sub-section with key actors list
 *
 * @param who - Array of actor names/descriptions
 * @param heading - Localized heading
 * @returns HTML string
 */
function buildWhoSection(who: readonly string[], heading: string): string {
  if (who.length === 0) return '';
  const items = who.map((actor) => `<li>${escapeHTML(actor)}</li>`).join('\n                ');
  return `
            <div class="analysis-who">
              <h3>${escapeHTML(heading)}</h3>
              <ul class="actor-list">
                ${items}
              </ul>
            </div>`;
}
 
/**
 * Build the "When" sub-section with timeline
 *
 * @param when - Array of date/milestone descriptions
 * @param heading - Localized heading
 * @returns HTML string
 */
function buildWhenSection(when: readonly string[], heading: string): string {
  if (when.length === 0) return '';
  const items = when
    .map((milestone) => `<li class="timeline-item">${escapeHTML(milestone)}</li>`)
    .join('\n                ');
  return `
            <div class="analysis-when">
              <h3>${escapeHTML(heading)}</h3>
              <ol class="timeline-list">
                ${items}
              </ol>
            </div>`;
}
 
/**
 * Build the "Why" sub-section
 *
 * @param why - Root cause analysis text
 * @param heading - Localized heading
 * @returns HTML string
 */
function buildWhySection(why: string, heading: string): string {
  Iif (!why) return '';
  return `
            <div class="analysis-why">
              <h3>${escapeHTML(heading)}</h3>
              <p>${escapeHTML(why)}</p>
            </div>`;
}
 
/**
 * Map stakeholder outcome to CSS class
 *
 * @param outcome - Winner/loser/neutral
 * @returns CSS class name
 */
function outcomeClass(outcome: StakeholderOutcome['outcome']): string {
  return `stakeholder-${outcome}`;
}
 
/**
 * Get localized label for stakeholder outcome
 *
 * @param outcome - Winner/loser/neutral
 * @param strings - Localized strings
 * @param strings.winnerLabel
 * @param strings.loserLabel
 * @param strings.neutralLabel
 * @returns Localized label
 */
function outcomeLabel(
  outcome: StakeholderOutcome['outcome'],
  strings: { winnerLabel: string; loserLabel: string; neutralLabel: string }
): string {
  switch (outcome) {
    case 'winner':
      return strings.winnerLabel;
    case 'loser':
      return strings.loserLabel;
    default:
      return strings.neutralLabel;
  }
}
 
/**
 * Build the "Winners & Losers" sub-section
 *
 * @param outcomes - Stakeholder assessments
 * @param heading - Localized heading
 * @param strings - Localized label strings
 * @param strings.winnerLabel
 * @param strings.loserLabel
 * @param strings.neutralLabel
 * @returns HTML string
 */
function buildStakeholderSection(
  outcomes: readonly StakeholderOutcome[],
  heading: string,
  strings: { winnerLabel: string; loserLabel: string; neutralLabel: string }
): string {
  if (outcomes.length === 0) return '';
  const items = outcomes
    .map(
      (s) =>
        `<li class="stakeholder-item ${outcomeClass(s.outcome)}">` +
        `<span class="stakeholder-badge">${escapeHTML(outcomeLabel(s.outcome, strings))}</span> ` +
        `<strong>${escapeHTML(s.actor)}</strong>: ${escapeHTML(s.reason)}` +
        `</li>`
    )
    .join('\n                ');
  return `
            <div class="analysis-stakeholders">
              <h3>${escapeHTML(heading)}</h3>
              <ul class="stakeholder-list">
                ${items}
              </ul>
            </div>`;
}
 
/**
 * Build the multi-perspective "Impact Assessment" sub-section
 *
 * @param impact - Impact strings per perspective
 * @param heading - Localized heading
 * @param labels - Localized perspective labels
 * @param labels.politicalLabel
 * @param labels.economicLabel
 * @param labels.socialLabel
 * @param labels.legalLabel
 * @param labels.geopoliticalLabel
 * @returns HTML string
 */
function buildImpactSection(
  impact: DeepAnalysis['impactAssessment'],
  heading: string,
  labels: {
    politicalLabel: string;
    economicLabel: string;
    socialLabel: string;
    legalLabel: string;
    geopoliticalLabel: string;
  }
): string {
  const perspectives = [
    { label: labels.politicalLabel, text: impact.political, css: 'impact-political' },
    { label: labels.economicLabel, text: impact.economic, css: 'impact-economic' },
    { label: labels.socialLabel, text: impact.social, css: 'impact-social' },
    { label: labels.legalLabel, text: impact.legal, css: 'impact-legal' },
    { label: labels.geopoliticalLabel, text: impact.geopolitical, css: 'impact-geopolitical' },
  ].filter((p) => p.text);
  if (perspectives.length === 0) return '';
  const items = perspectives
    .map(
      (p) =>
        `<div class="impact-perspective ${p.css}">` +
        `<h4>${escapeHTML(p.label)}</h4>` +
        `<p>${escapeHTML(p.text)}</p>` +
        `</div>`
    )
    .join('\n              ');
  return `
            <div class="analysis-impact">
              <h3>${escapeHTML(heading)}</h3>
              <div class="impact-grid">
              ${items}
              </div>
            </div>`;
}
 
/**
 * Get localized severity label
 *
 * @param severity - Severity level
 * @param strings - Localized strings
 * @param strings.severityLow
 * @param strings.severityMedium
 * @param strings.severityHigh
 * @param strings.severityCritical
 * @returns Localized label
 */
function severityLabel(
  severity: ActionConsequence['severity'],
  strings: {
    severityLow: string;
    severityMedium: string;
    severityHigh: string;
    severityCritical: string;
  }
): string {
  switch (severity) {
    case 'low':
      return strings.severityLow;
    case 'medium':
      return strings.severityMedium;
    case 'high':
      return strings.severityHigh;
    case 'critical':
      return strings.severityCritical;
    default:
      return String(severity);
  }
}
 
/**
 * Build the "Actions → Consequences" sub-section
 *
 * @param items - Action-consequence pairs
 * @param heading - Localized heading
 * @param labels - Localized column labels
 * @param labels.actionLabel
 * @param labels.consequenceLabel
 * @param strings - Localized severity strings
 * @param strings.severityLow
 * @param strings.severityMedium
 * @param strings.severityHigh
 * @param strings.severityCritical
 * @returns HTML string
 */
function buildConsequencesSection(
  items: readonly ActionConsequence[],
  heading: string,
  labels: { actionLabel: string; consequenceLabel: string },
  strings: {
    severityLow: string;
    severityMedium: string;
    severityHigh: string;
    severityCritical: string;
  }
): string {
  if (items.length === 0) return '';
  const rows = items
    .map(
      (item) =>
        `<tr class="consequence-row severity-${escapeHTML(item.severity)}">` +
        `<td class="action-cell">${escapeHTML(item.action)}</td>` +
        `<td class="arrow-cell">→</td>` +
        `<td class="consequence-cell">${escapeHTML(item.consequence)}</td>` +
        `<td class="severity-cell"><span class="severity-badge severity-${escapeHTML(item.severity)}">${escapeHTML(severityLabel(item.severity, strings))}</span></td>` +
        `</tr>`
    )
    .join('\n                ');
  return `
            <div class="analysis-consequences">
              <h3>${escapeHTML(heading)}</h3>
              <table class="consequences-table" role="table">
                <thead>
                  <tr>
                    <th scope="col">${escapeHTML(labels.actionLabel)}</th>
                    <th scope="col" aria-hidden="true"></th>
                    <th scope="col">${escapeHTML(labels.consequenceLabel)}</th>
                    <th scope="col"></th>
                  </tr>
                </thead>
                <tbody>
                ${rows}
                </tbody>
              </table>
            </div>`;
}
 
/**
 * Build the "Mistakes" sub-section
 *
 * @param mistakes - Political mistake assessments
 * @param heading - Localized heading
 * @param alternativeLabel - Localized "should have" label
 * @returns HTML string
 */
function buildMistakesSection(
  mistakes: readonly PoliticalMistake[],
  heading: string,
  alternativeLabel: string
): string {
  if (mistakes.length === 0) return '';
  const items = mistakes
    .map(
      (m) =>
        `<div class="mistake-item">` +
        `<p class="mistake-actor"><strong>${escapeHTML(m.actor)}</strong></p>` +
        `<p class="mistake-desc">${escapeHTML(m.description)}</p>` +
        `<p class="mistake-alt"><em>${escapeHTML(alternativeLabel)}:</em> ${escapeHTML(m.alternative)}</p>` +
        `</div>`
    )
    .join('\n              ');
  return `
            <div class="analysis-mistakes">
              <h3>${escapeHTML(heading)}</h3>
              ${items}
            </div>`;
}
 
/**
 * Build the "Strategic Outlook" sub-section
 *
 * @param outlook - Forward-looking analysis text
 * @param heading - Localized heading
 * @returns HTML string
 */
function buildOutlookSection(outlook: string, heading: string): string {
  if (!outlook) return '';
  return `
            <div class="analysis-outlook">
              <h3>${escapeHTML(heading)}</h3>
              <p>${escapeHTML(outlook)}</p>
            </div>`;
}
 
// ─── Main builder ────────────────────────────────────────────────────────────
 
/**
 * Build the complete deep political analysis section HTML.
 *
 * This section provides parliament-intelligence-grade analysis using the
 * "5W + Impact" framework. It is designed for sophisticated readers who
 * want multi-perspective understanding of European Parliament decisions.
 *
 * Returns an empty string if the analysis object is null/undefined or
 * contains no meaningful content.
 *
 * @param analysis - Deep analysis data (null/undefined returns empty string)
 * @param lang - BCP 47 language code for localized headings
 * @returns HTML section string or empty string
 */
export function buildDeepAnalysisSection(
  analysis: DeepAnalysis | null | undefined,
  lang: string
): string {
  if (!analysis) return '';
 
  const strings = getLocalizedString(DEEP_ANALYSIS_STRINGS, lang);
 
  const whatHtml = buildWhatSection(analysis.what, strings.whatHeading);
  const whoHtml = buildWhoSection(analysis.who, strings.whoHeading);
  const whenHtml = buildWhenSection(analysis.when, strings.whenHeading);
  const whyHtml = buildWhySection(analysis.why, strings.whyHeading);
  const stakeholderHtml = buildStakeholderSection(
    analysis.stakeholderOutcomes,
    strings.stakeholderHeading,
    strings
  );
  const impactHtml = buildImpactSection(analysis.impactAssessment, strings.impactHeading, strings);
  const consequencesHtml = buildConsequencesSection(
    analysis.actionConsequences,
    strings.consequencesHeading,
    strings,
    strings
  );
  const mistakesHtml = buildMistakesSection(
    analysis.mistakes,
    strings.mistakesHeading,
    strings.alternativeLabel
  );
  const outlookHtml = buildOutlookSection(analysis.outlook, strings.outlookHeading);
 
  const innerContent =
    whatHtml +
    whoHtml +
    whenHtml +
    whyHtml +
    stakeholderHtml +
    impactHtml +
    consequencesHtml +
    mistakesHtml +
    outlookHtml;
 
  // If all sub-sections are empty, return nothing
  Iif (!innerContent.trim()) return '';
 
  return `
          <section class="deep-analysis" lang="${escapeHTML(lang)}">
            <h2>${escapeHTML(strings.sectionHeading)}</h2>
            ${innerContent}
          </section>`;
}