'use strict';

var fs = require('fs');

var getJSON = function getJSON(jsonFilename) {
  return require(jsonFilename);
};

var validateArray = function validateArray(arr) {
  if (!Array.isArray(arr)) {
    throw Error('Could not find array.');
  }
  return arr;
};

var defaultGetArray = function defaultGetArray(x) {
  return x;
};

var writeJSONLines = function writeJSONLines(jsonFilename) {
  var getArray = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetArray;

  var json = getJSON(jsonFilename);
  var arr = validateArray(getArray(json));
  var jsonlFilename = jsonFilename.replace(/.json/g, '.jsonl');
  var writeStream = fs.createWriteStream(jsonlFilename);
  arr.map(function (x) {
    return writeStream.write(JSON.stringify(x) + '\n');
  });
  writeStream.end();
  return {
    lines: arr.length,
    file: jsonlFilename
  };
};

module.exports = writeJSONLines;