"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = downloadProjectRubric;

var _fs = _interopRequireDefault(require("fs"));

var _api = require("../../../api");

var _utils = require("../../utils");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Download project rubric JSON data and save it locally
 * @param {object} project project JSON data
 * @param {string} targetDir target directory to save JSON file
 * @param {string} token Udacity authentication token
 */
async function downloadProjectRubric(project, targetDir, token) {
  try {
    const res = await (0, _api.fetchProjectRubric)(project.rubric_id, token);
    const jsonStr = JSON.stringify(res, null, 2);
    const file = `${targetDir}/rubric.json`; // save JSON file

    _fs.default.writeFileSync(file, jsonStr);

    _utils.logger.info(`Downloaded rubric JSON of project ${project.title} for ${targetDir}`);
  } catch (error) {
    throw error;
  }
}