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

88.88% Statements 72/81
62.06% Branches 36/58
90.9% Functions 10/11
88.31% Lines 68/77

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                                                          99x   99x   99x 2x 2x       97x 97x     99x     99x 99x 176x 176x 176x       116x 99x 19x 19x 19x     19x   19x       97x   97x 4x 4x   4x 4x 4x 4x 4x 4x   4x 6x 6x               97x   99x 95x 2x       95x 95x 170x 170x 170x 170x   95x   95x       70x 70x 144x 144x   144x 144x     144x         26x         97x   36x 36x 36x   36x 36x 36x 55x 55x 55x     55x     36x       97x 99x                               97x      
/**
 * @module data-transformers/content-generators/motions
 * @description Generator for "motions" article content. Renders opposition motions
 * grouped by party with strategy analysis.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import { escapeHtml } from '../../html-utils.js';
import type { Language } from '../../types/language.js';
import type { ArticleContentData, RawDocument } from '../types.js';
import { getPillarTransition } from '../../editorial-pillars.js';
import {
  L,
  svSpan,
  sanitizeUrl,
  normalizePartyKey,
} from '../helpers.js';
import { detectPolicyDomains, generatePolicySignificance, generateDeepPolicyAnalysis } from '../policy-analysis.js';
import {
  groupMotionsByProposition,
  generateOppositionStrategySection,
  renderMotionEntry,
  generateDocumentIntelligenceAnalysis,
  PROP_TITLE_SUFFIX_REGEX,
} from '../document-analysis.js';
 
export function generateMotionsContent(data: ArticleContentData, lang: Language | string): string {
  const motions = data.motions || [];
 
  let content = `<h2>${L(lang, 'oppMotions')}</h2>\n`;
 
  if (motions.length === 0) {
    content += `<p>${L(lang, 'noMotions')}</p>\n`;
    return content;
  }
 
  // Analytical lede paragraph
  const breakdownFn = L(lang, 'motionsBreakdown') as string | ((n: number) => string);
  const breakdownText = typeof breakdownFn === 'function'
    ? breakdownFn(motions.length)
    : `${motions.length} new opposition motions filed.`;
  content += `<p class="article-lede">${escapeHtml(String(breakdownText))}</p>\n`;
 
  // Group motions by party for strategic analysis
  const byParty: Record<string, RawDocument[]> = {};
  motions.forEach(motion => {
    const party = normalizePartyKey(motion.parti);
    if (!byParty[party]) byParty[party] = [];
    byParty[party].push(motion);
  });
 
  // Opposition strategy section with per-party analysis
  const partyCount = Object.keys(byParty).filter(p => p !== 'other').length;
  if (partyCount > 1) {
    content += `\n    <h2>${L(lang, 'oppositionStrategy')}</h2>\n`;
    const strategyFn = L(lang, 'oppositionStrategyContext') as string | ((n: number) => string);
    const strategyContext = typeof strategyFn === 'function'
      ? strategyFn(partyCount)
      : `Motions from ${partyCount} different parties reveal the breadth of opposition political criticism and alternative policy agendas.`;
    content += `    <p>${escapeHtml(String(strategyContext))}</p>\n`;
    // Per-party analysis with domain focus
    content += generateOppositionStrategySection(motions, lang);
  }
 
  // Group "med anledning av prop." motions by parent proposition to eliminate duplicate headings
  const { grouped: groupedByProp, independent: independentMotions } = groupMotionsByProposition(motions);
 
  if (groupedByProp.size > 0) {
    content += `\n    <h2>${L(lang, 'responsesToProp')}</h2>\n`;
    groupedByProp.forEach((propMotions, propRef) => {
      // Extract the descriptive title portion that follows the prop ID
      const firstTitle = propMotions[0]?.titel || propMotions[0]?.title || '';
      const suffixMatch = firstTitle.match(PROP_TITLE_SUFFIX_REGEX);
      const propTitle = suffixMatch?.[1]?.trim() || String(propRef);
      const safePropRef = escapeHtml(String(propRef));
      const safePropTitle = escapeHtml(propTitle);
      content += `    <h3>Prop. ${safePropRef}: ${svSpan(safePropTitle, lang)}</h3>\n`;
      // Individual motions inside a prop group use h4 to maintain h2→h3→h4 hierarchy
      propMotions.forEach(m => {
        const html = renderMotionEntry(m, lang);
        content += html.replace(/<h3(\b[^>]*)?>/g, '<h4$1>').replace(/<\/h3>/g, '</h4>');
      });
    });
  }
 
  // Motions to render with thematic analysis:
  // - when proposition groups exist: only independent motions (non-"med anledning av")
  // - when no proposition groups: all motions (preserves existing thematic behaviour)
  const thematicMotions = groupedByProp.size > 0 ? independentMotions : motions;
 
  if (thematicMotions.length > 0) {
    if (groupedByProp.size > 0) {
      content += `\n    <h2>${L(lang, 'independentMotions')}</h2>\n`;
    }
 
    // Group motions by primary policy theme for thematic analysis
    const byTheme: Record<string, RawDocument[]> = {};
    thematicMotions.forEach(motion => {
      const domains = detectPolicyDomains(motion, lang);
      const theme = domains[0] || String(L(lang, 'generalMatters'));
      if (!byTheme[theme]) byTheme[theme] = [];
      byTheme[theme].push(motion);
    });
    const themeCount = Object.keys(byTheme).length;
 
    if (themeCount > 1 && groupedByProp.size === 0) {
      // Suppress "Thematic Analysis" h2 when already under an "Independent Motions" h2
      // (groupedByProp.size === 0 means we are NOT in the split-section layout, so it is
      // safe to emit the additional h2 without creating two consecutive section headers)
      content += `\n    <h2>${L(lang, 'thematicAnalysis')}</h2>\n`;
      Object.entries(byTheme).forEach(([theme, themeMotions]) => {
        content += `\n    <h3>${escapeHtml(theme)} (${themeMotions.length})</h3>\n`;
        themeMotions.forEach(motion => {
          // Demote motion entry headings one level when inside a themed section
          const entryHtml = renderMotionEntry(motion, lang);
          const demotedHtml = entryHtml
            .replace(/<h3(\b[^>]*)?>/g, '<h4$1>')
            .replace(/<\/h3>/g, '</h4>');
          content += demotedHtml;
        });
      });
    } else {
      // Single theme, no detection, or alongside proposition groups: flat list
      thematicMotions.forEach(motion => { content += renderMotionEntry(motion, lang); });
    }
  }
 
  // Party activity breakdown
  if (partyCount > 0) {
    // Narrative bridge before cross-party analysis (inter-pillar transition)
    const watchTransition = getPillarTransition(lang, 'watchToOpposition');
    Eif (watchTransition) {
      content += `    <p class="pillar-transition">${escapeHtml(watchTransition)}</p>\n`;
    }
    content += `\n    <h2>${L(lang, 'coalitionDynamics')}</h2>\n`;
    content += `    <div class="context-box">\n      <ul>\n`;
    Object.entries(byParty).forEach(([party, partyMotions]) => {
      Eif (party !== 'other') {
        const detailFn = L(lang, 'partyMotionsFiled') as string | ((party: string, n: number) => string);
        const detail = typeof detailFn === 'function'
          ? detailFn(party, partyMotions.length)
          : `${party}: ${partyMotions.length} motions filed`;
        content += `        <li>${escapeHtml(String(detail))}</li>\n`;
      }
    });
    content += `      </ul>\n    </div>\n`;
  }
 
  // Government department engagement section (from analyze_g0v_by_department)
  const govDeptData = data.govDeptData ?? [];
  Iif (govDeptData.length > 0) {
    content += `\n    <h2>${L(lang, 'govEngagement')}</h2>\n`;
    content += `    <div class="context-box">\n      <ul>\n`;
    govDeptData.slice(0, 5).forEach(dept => {
      const deptName = escapeHtml(String(dept['name'] ?? dept['departement'] ?? dept['department'] ?? ''));
      const deptCount = dept['count'] ?? dept['total'] ?? dept['document_count'];
      if (deptName) {
        const hasDeptCount = deptCount !== null && deptCount !== undefined;
        content += hasDeptCount
          ? `        <li><strong>${deptName}</strong> (${escapeHtml(String(deptCount))})</li>\n`
          : `        <li><strong>${deptName}</strong></li>\n`;
      }
    });
    content += `      </ul>\n    </div>\n`;
  }
 
  return content;
}