"use strict";

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

var _ = require(".");

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

/**
 * Loop through lessons and call @function downloadLesson
 * to download the lesson JSON data
 * @param {object} lessons lessons JSON data from Nanodegree JSON
 * @param {string} ndKey Nanodegree key
 * @param {string} dirCourse Nanodegree part's path or free course's path
 * @param {string} udacityAuthToken Udacity authentication token
 */
async function downloadLessons(lessons, courseId, dirCourse, udacityAuthToken) {
  for (let i = 0, len = lessons.length; i < len; i += 1) {
    try {
      const lesson = lessons[i];
      const numbering = i + 1 < 10 ? `0${i + 1}` : i + 1;
      const dirLessonName = (0, _utils.filenamify)(`Lesson ${numbering}_${lesson.title}`);
      const dirLesson = (0, _utils.makeDir)(dirCourse, dirLessonName);
      await (0, _.downloadLesson)(lesson, courseId, dirLesson, udacityAuthToken);
    } catch (error) {
      throw error;
    }
  }

  return null;
}