const fs = require('fs');
const csv = require('csv-parser');
const {
    convertEpochToDate
} = require('../helpers/epoch');

const results = []

async function analyse(file, flag) {
    fs.createReadStream(file)
        .pipe(csv())
        .on('data', (data) => results.push(data))
        .on('end', () => {
            if(flag == 'coin'){
                const uniqueCoins = [...new Set(results.map(item => item.token))];
                return uniqueCoins
            }
            else if(flag == 'date'){
                const lastDate = results[results.length - 1].date
                const date = convertEpochToDate(lastDate)
                return date
            }
            process.exit(1)
        });
}

module.exports = analyse