"use strict";

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

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 content for ReflectAtom
 * @param {object} atom atom json
 * @param {string} targetDir path to save the assets folder for videos
 * @param {string} prefix prefix for video file name
 * @returns {string} HTML content
 */
async function createHtmlReflectAtom(atom, targetDir, prefix) {
  const questionTitle = (0, _utils.markdownToHtml)(atom.question.title);
  let questionText;

  if (atom.question.semantic_type === 'TextQuestion') {
    questionText = await (0, _utils2.createHtmlText)(atom.question.text, targetDir, atom.id);
  } else {
    questionText = '<p>Unknown question type. Please contact the developer to make it compatible with this atom type!</p>';
  } // download answer video if available


  const youtubeId = atom.answer.video ? atom.answer.video.youtube_id : '';
  const promiseHtmlVideo = (0, _utils2.createHtmlVideo)(youtubeId, targetDir, prefix, atom.title);
  const promiseLoadTemplate = (0, _templates.loadTemplate)('atom.reflect');
  const [video, html] = await Promise.all([promiseHtmlVideo, promiseLoadTemplate]);
  const answer = await (0, _utils2.createHtmlText)(atom.answer.text, targetDir, atom.id);
  const dataTemplate = {
    answer,
    video,
    questionTitle,
    questionText
  };

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

  return template(dataTemplate);
}