/**
 * Transforms an object to a raw data CSV file.
 *
 * @param {Object} rows Data
 * @return {String} Formatted CSV data
 */
function toCSV(rows) {
    const [columns] = rows;
    let content = Object.keys(columns).join('\t');
    for (let row of rows) {
        const values = Object.values(row);
        const finalValues = values.map(value => {
            if (value instanceof Date) return formatDate(value);
            if (value === null) return '';
            return value;
        });
        content += '\n';
        content += finalValues.join('\t');
    }
    return content;
}

function formatDate(date) {
    return new Intl.DateTimeFormat('es', {
        year: 'numeric',
        month: 'numeric',
        day: 'numeric',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
    }).format(date);
}

module.exports = {
    toCSV,
    formatDate
};