/* @flow strict-local */
import type { Message, Narrow, Outbox, RenderedSectionDescriptor } from '../types';
import { isTopicNarrow, isPrivateOrGroupNarrow } from '../utils/narrow';
import { isSameRecipient } from '../utils/recipient';
import { isSameDay } from '../utils/date';

export default (
  messages: $ReadOnlyArray<Message | Outbox>,
  narrow: Narrow,
): RenderedSectionDescriptor[] => {
  let prevItem;
  const showHeader = !isPrivateOrGroupNarrow(narrow) && !isTopicNarrow(narrow);

  return messages.reduce(
    (sections, item) => {
      const diffDays =
        prevItem
        && !isSameDay(new Date(prevItem.timestamp * 1000), new Date(item.timestamp * 1000));
      if (!prevItem || diffDays) {
        sections[sections.length - 1].data.push({
          key: `time${item.timestamp}`,
          type: 'time',
          timestamp: item.timestamp,
          firstMessage: item,
        });
      }
      const diffRecipient = !isSameRecipient(prevItem, item);
      if (showHeader && diffRecipient) {
        sections.push({
          key: `header${item.id}`,
          message: item,
          data: [],
        });
      }
      const shouldGroupWithPrev =
        !diffRecipient
        && !diffDays
        && prevItem
        && prevItem.sender_full_name === item.sender_full_name;

      sections[sections.length - 1].data.push({
        key: item.id,
        type: 'message',
        isBrief: shouldGroupWithPrev,
        message: item,
      });

      prevItem = item;

      return sections;
    },
    [{ key: 0, data: [], message: {} }],
  );
};

@@ -493,52 +493,85 @@
 %0A%0A  
-return messages.reduce(%0A    (sections, 
+const sections = %5B%7B key: 0, data: %5B%5D, message: %7B%7D %7D%5D;%0A  messages.forEach(
 item
-)
  =%3E 
@@ -572,26 +572,24 @@
 em =%3E %7B%0A    
- 
- 
 const diffDa
@@ -599,26 +599,24 @@
 =%0A      
-  
 prevItem
 %0A       
@@ -603,32 +603,24 @@
     prevItem
-%0A       
  && !isSameD
@@ -693,18 +693,16 @@
 1000));%0A
-  
     if (
@@ -724,34 +724,32 @@
 ffDays) %7B%0A      
-  
 sections%5Bsection
@@ -778,26 +778,24 @@
 h(%7B%0A        
-  
 key: %60time$%7B
@@ -808,26 +808,24 @@
 imestamp%7D%60,%0A
-  
         type
@@ -842,18 +842,16 @@
         
-  
 timestam
@@ -877,18 +877,16 @@
         
-  
 firstMes
@@ -893,26 +893,24 @@
 sage: item,%0A
-  
       %7D);%0A  
@@ -903,38 +903,34 @@
 ,%0A      %7D);%0A    
-  %7D%0A  
+%7D%0A
     const diffRe
@@ -977,18 +977,16 @@
 m);%0A    
-  
 if (show
@@ -1014,26 +1014,24 @@
 nt) %7B%0A      
- 
- 
 sections.pus
@@ -1030,26 +1030,24 @@
 ions.push(%7B%0A
-  
         key:
@@ -1071,26 +1071,24 @@
 %7D%60,%0A        
-  
 message: ite
@@ -1098,18 +1098,16 @@
         
- 
- 
 data: %5B%5D
@@ -1114,26 +1114,24 @@
 ,%0A      
-  
 %7D);%0A    
   %7D%0A    
@@ -1122,22 +1122,18 @@
 %7D);%0A    
- 
- 
 %7D%0A
-  
     cons
@@ -1162,18 +1162,16 @@
 =%0A      
-  
 !diffRec
@@ -1183,18 +1183,16 @@
 t%0A      
-  
 && !diff
@@ -1196,18 +1196,16 @@
 iffDays%0A
-  
       &&
@@ -1216,26 +1216,24 @@
 vItem%0A      
-  
 && prevItem.
@@ -1277,18 +1277,16 @@
 _name;%0A%0A
-  
     sect
@@ -1325,26 +1325,24 @@
 ush(%7B%0A      
-  
 key: item.id
@@ -1345,26 +1345,24 @@
 m.id,%0A      
- 
- 
 type: 'messa
@@ -1362,26 +1362,24 @@
  'message',%0A
-  
       isBrie
@@ -1400,34 +1400,32 @@
 WithPrev,%0A      
-  
 message: item,%0A 
@@ -1431,17 +1431,13 @@
     
- 
- 
 %7D);%0A%0A
-  
     
@@ -1453,21 +1453,22 @@
 = item;%0A
-%0A    
+  %7D);%0A
   return
@@ -1482,60 +1482,7 @@
 ns;%0A
-    %7D,%0A    %5B%7B key: 0, data: %5B%5D, message: %7B%7D %7D%5D,%0A  );%0A
 %7D;%0A

Rewrite false `reduce` as `forEach` in `renderMessages`.