import CSVReader from 'readers/csv/csv';
import { isNumber } from 'base/utils';

const CSVTimeInColumnsReader = CSVReader.extend({

  _name: 'csv-time_in_columns',

  init(readerInfo) {
    this._super(readerInfo);
  },

  load() {
    return this._super()
      .then(({ data, columns }) => {
        const indicatorKey = columns[this.keySize];

        let concepts = data.reduce((result, row) => {
          Object.keys(row).forEach((concept) => {
            concept = concept === indicatorKey ? row[indicatorKey] : concept;

            if (Number(concept) != concept && !result.includes(concept)) {
              result.push(concept);
            }
          });

          return result;
        }, []);
        concepts.splice(1, 0, 'time');

        const indicators = concepts.slice(2);
        const [entityDomain] = concepts;
        return {
          columns: concepts,
          data: data.reduce((result, row) => {
            Object.keys(row).forEach((key) => {
              if (![entityDomain, indicatorKey].includes(key)) {
                result.push(
                  Object.assign({
                      [entityDomain]: row[entityDomain],
                      time: key,
                    }, indicators.reduce((result, indicator) => {
                      result[indicator] = row[indicatorKey] === indicator ? row[key] : null;
                      return result;
                    }, {})
                  )
                );
              }
            });

            return result;
          }, [])
        };
      });
  }

});

export default CSVTimeInColumnsReader;

@@ -212,13 +212,40 @@
 o);%0A
+    this.timeKey = 'time';%0A
   %7D,%0A
-
 %0A  l
@@ -761,22 +761,28 @@
 e(1, 0, 
-'
+this.
 time
-'
+Key
 );%0A%0A    
@@ -949,32 +949,345 @@
 sult, row) =%3E %7B%0A
+            const resultRows = result.filter((resultRow) =%3E resultRow%5BentityDomain%5D === row%5BentityDomain%5D);%0A            if (resultRows.length) %7B%0A              resultRows.forEach((resultRow) =%3E %7B%0A                resultRow%5Brow%5BindicatorKey%5D%5D = row%5BresultRow%5Bthis.timeKey%5D%5D;%0A              %7D);%0A            %7D else %7B%0A  
             Obje
@@ -1314,24 +1314,26 @@
 ((key) =%3E %7B%0A
+  
             
@@ -1393,32 +1393,34 @@
                 
+  
 result.push(%0A   
@@ -1416,16 +1416,18 @@
 t.push(%0A
+  
         
@@ -1474,16 +1474,18 @@
         
+  
 %5BentityD
@@ -1537,19 +1537,33 @@
     
-time
+  %5Bthis.timeKey%5D
 : key,%0A
+  
     
@@ -1620,24 +1620,26 @@
 cator) =%3E %7B%0A
+  
             
@@ -1733,32 +1733,34 @@
                 
+  
 return result;%0A 
@@ -1778,16 +1778,18 @@
         
+  
 %7D, %7B%7D)%0A 
@@ -1805,18 +1805,22 @@
         
+ 
+ 
 )%0A
+  
         
@@ -1836,34 +1836,38 @@
 ;%0A              
-%7D%0A
+  %7D%0A  
             %7D);%0A
@@ -1856,33 +1856,46 @@
               %7D)
-;
+%0A            %7D
 %0A%0A            re

Fix mapping for duplicating rows in csv time in columns reader (#2365)