const fs = require('fs-extra');
const jsonToCsv = require('./json-to-csv');

function writeTouchpoint(data) {
  const promises = [];
  promises.push(
    fs.writeFile('mock-api/resources/touchpoint-contents.csv', `${jsonToCsv(data.content.data)}\n`),
  );
  promises.push(
    fs.writeFile(
      'mock-api/resources/touchpoint-webapp-manifest.json',
      `${JSON.stringify(data.webapp_manifest, null, 2)}\n`,
    ),
  );

  const tp = { ...data };
  tp.webapp_manifest = {};
  tp.content.data = {};
  promises.push(
    fs.writeFile('mock-api/resources/touchpoint.json', `${JSON.stringify(tp, null, 2)}\n`),
  );

  return Promise.all(promises);
}

module.exports = writeTouchpoint;
