"use strict";

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

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

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

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

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

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

/**
 * Create HTML for Lab Introduction
 * @param {object} overview Introduction JSON data
 * @param {string} targetDir target directory
 */
async function createHtmlLabIntroduction(overview, labTitle, targetDir) {
  if (!overview) {
    return '(No Lab Introduction data available)';
  }

  let {
    title,
    summary
  } = overview;
  const {
    video
  } = overview;
  let keyTakeaways = overview.key_takeaways;
  const mediaFileLabel = 'lab-intro'; // process all markdown to HTML

  title = (0, _utils.markdownToHtml)(title);
  summary = await (0, _utils2.createHtmlText)(summary, targetDir, mediaFileLabel);
  keyTakeaways = keyTakeaways || [];

  for (let i = 0, len = keyTakeaways.length; i < len; i += 1) {
    keyTakeaways[i] = await (0, _utils2.createHtmlText)(keyTakeaways[i], targetDir, mediaFileLabel);
  }

  let htmlVideo;

  if (video && video.youtube_id) {
    htmlVideo = await (0, _utils2.createHtmlVideo)(video.youtube_id, targetDir, '', labTitle);
  }

  if (!title && !summary && (!keyTakeaways || !keyTakeaways.length) && !htmlVideo) {
    return '(No Lab Introduction data available)';
  }

  const html = await (0, _templates.loadTemplate)('lab.introduction');
  const dataTemplate = {
    title,
    summary,
    keyTakeaways,
    video: htmlVideo
  };

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

  const htmlResult = template(dataTemplate);
  return htmlResult;
}