"use strict";

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

var _handlebars = _interopRequireDefault(require("handlebars"));

var _path = _interopRequireDefault(require("path"));

var _atoms = require("./atoms");

var _lab = require("./lab");

var _ = require(".");

var _templates = require("./templates");

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

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

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

/**
 * Write HTML file for lab
 * @param {object} lab JSON data for lab
 * @param {string} htmlSidebar sidebar html content
 * @param {string} targetDir target directory
 */
async function writeHtmlLab(lab, htmlSidebar, targetDir) {
  if (!lab) {
    return;
  }

  const {
    details,
    title,
    overview,
    workspace
  } = lab;

  const file = _path.default.join(targetDir, (0, _utils.filenamify)(`Lab - ${title}.html`));

  let promiseHtmlReviewVideo;

  if (lab.review_video && lab.review_video.youtube_id) {
    promiseHtmlReviewVideo = (0, _utils2.createHtmlVideo)(lab.review_video.youtube_id, targetDir, 'Lab - ', title);
  }

  const promiseInstructions = (0, _lab.createHtmlLabInstructions)(details, targetDir);
  const promiseIntroduction = (0, _lab.createHtmlLabIntroduction)(overview, title, targetDir);
  const promiseHtmlWorkspace = (0, _atoms.createHtmlWorkspaceAtom)(workspace);
  const templateLab = (0, _templates.loadTemplate)('lab');
  const [instructions, introduction, htmlWorkspace, reviewVideo, html] = await Promise.all([promiseInstructions, promiseIntroduction, promiseHtmlWorkspace, promiseHtmlReviewVideo, templateLab]);
  const dataLab = {
    introduction,
    instructions,
    reviewVideo,
    reflection: '(Reflection is only available online on Udacity website)',
    workspace: htmlWorkspace
  };

  const template = _handlebars.default.compile(html);

  const htmlLab = template(dataLab); // decide how many folders it needs to go up to access the assets

  const pathToAssets = '../'; // write html file

  const templateDataIndex = {
    docTitle: title,
    sidebar: htmlSidebar,
    pathToAssets,
    title: `Lab: ${title}`,
    contentMain: htmlLab
  };
  await (0, _.writeHtml)(templateDataIndex, file);

  _utils.logger.info(`Completed rendering lab file ${file}`);

  _utils.logger.info('____________________\n');
}