import type { SampleResponse } from "../../content/approach-survey-fixture"; export const sentimentGroup=(score:number):"positive"|"neutral"|"negative"=> score>.2?"positive":score<-.2?"negative":"neutral"; /** Filtering precedes every aggregation; a theme counts once per response. */ export function summarizeResponses(input:readonly SampleResponse[],area="all"){ const records=input.filter(record=>area==="all"||record.area===area); const themes=new Map(); const sentiments={positive:0,neutral:0,negative:0}; const contacts={meeting:0,email:0,phone:0}; for(const record of records){ for(const theme of new Set(record.themes))themes.set(theme,(themes.get(theme)??0)+1); sentiments[sentimentGroup(record.sentiment)]++; contacts[record.contact]++; } return {records,sentiments,contacts,themes:[...themes.entries()].sort((a,b)=>b[1]-a[1]||a[0].localeCompare(b[0]))}; }