import _ from 'lodash'
import fs from 'fs'
import test from 'tape'
import Excel from 'exceljs'
import tempWrite from 'temp-write'
import Writer from '../../src/io/writer'

const readXlsx = function readXlsx (filePath) {
  const values = []
  const workbook = new Excel.Workbook()
  return workbook.xlsx.readFile(filePath)
    .then(() => {
      const worksheet = workbook.getWorksheet(1)
      worksheet.eachRow((row) => {
        let rowValues = row.values
        rowValues.shift()

        rowValues = _.map(rowValues,
          item => (_.isObject(item) || _.isNil(item) ? '' : item))
        values.push(rowValues)
      })
      return values
    })
}

/* eslint-disable no-unused-vars */
test(`Writer
  should throw an error when outputFile is not specified`, (t) => {
  const expectedOutput = 'Error: OutputFile was not specified'

  try {
    const writer = new Writer({
      exportFormat: 'csv',
    })
    t.fail('Should throw an error')
  } catch (e) {
    t.equal(e.toString(), expectedOutput, 'Should throw an error')
    t.end()
  }
})

test(`Writer
  should throw an error when incorrect export format is specified`, (t) => {
  const expectedOutput = 'Error: Unsupported file type: unknown,' +
    ' alowed formats are xlsx,csv'

  try {
    const writer = new Writer({
      outputFile: 'out.csv',
      exportFormat: 'unknown',
    })

    t.fail('Should throw an error')
  } catch (e) {
    t.equal(e.toString(), expectedOutput, 'Should throw an error')
    t.end()
  }
})

test(`Writer
  should throw an error when incorrect encoding is specified`, (t) => {
  const expectedOutput = 'Error: Encoding does not exist: unknown'

  try {
    const writer = new Writer({
      outputFile: 'out.csv',
      exportFormat: 'csv',
      encoding: 'unknown',
    })

    t.fail('Should throw an error')
  } catch (e) {
    t.equal(e.toString(), expectedOutput, 'Should throw an error')
    t.end()
  }
})

test(`Writer
  should return an error when writing fails`, (t) => {
  const filePath = tempWrite.sync()
  const expectedOutput = 'TypeError: must start with number, ' +
    'buffer, array or string'

  const writer = new Writer({
    outputFile: filePath,
    exportFormat: 'csv',
  })

  try {
    writer._writeCsvRows(Buffer())
      .then(() => {
        t.fail('Should throw an error')
      })
  } catch (e) {
    t.equal(e.toString(), expectedOutput, 'Should throw an error')
    t.end()
  }
})
/* eslint-enable no-unused-vars */

test(`Writer
  should write data to csv file`, (t) => {
  const filePath = tempWrite.sync()
  const expectedOutput = ',,,1,1,2,0,-1,str'
  const row = [null, undefined, false, true, 1, 2, 0, -1, 'str']

  const writer = new Writer({
    outputFile: filePath,
    exportFormat: 'csv',
  })

  writer._writeCsvRows([row])
    .then(() => {
      const csv = fs.readFileSync(filePath).toString()
      t.equal(expectedOutput, csv.split('\n')[0], 'Should write csv to file')
      t.end()
    })
})

test(`Writer
  should write data to xlsx file`, (t) => {
  const filePath = tempWrite.sync()
  const header = 'num,num0,num1,num2,string,undefined,null,true,false'
  const expectedOutput = [-1, 0, 1, 2, 'abcd', '', '', 1, '']
  const row = [-1, 0, 1, 2, 'abcd', undefined, null, true, false]

  const writer = new Writer({
    outputFile: filePath,
    exportFormat: 'xlsx',
  })

  writer.setHeader(header.split(','))
    .then(() => writer.write([row]))
    .then(() => writer.flush())
    .then(() => readXlsx(filePath))
    .then((data) => {
      t.equal(data.length, 2, 'Two exported rows')
      t.deepEqual(expectedOutput, data[1], 'Mapped right values')
      t.end()
    })
})


@@ -382,16 +382,43 @@
 heet(1)%0A
+      let headerLen = null%0A
       wo
@@ -506,16 +506,380 @@
 hift()%0A%0A
+        if (headerLen)%0A          // older exceljs reader version trims last value%0A          // if it is an empty string, so we will pad row%0A          // to header length with empty string%0A          rowValues = %5B%0A            ...rowValues,%0A            ...Array(headerLen - rowValues.length).fill(''),%0A          %5D%0A        else%0A          headerLen = rowValues.length%0A%0A
         
@@ -3946,24 +3946,33 @@
 t.deepEqual(
+data%5B1%5D, 
 expectedOutp
@@ -3978,17 +3978,8 @@
 put,
- data%5B1%5D,
  'Ma

Fix tests for older exceljs version