"use strict";

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

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 TaskListAtom
 * @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 createHtmlTaskListAtom(atom, targetDir, prefix) {
  let {
    description
  } = atom;
  let positiveFeedback = atom.positive_feedback;
  description = await (0, _utils2.createHtmlText)(description, targetDir, atom.id);
  positiveFeedback = await (0, _utils2.createHtmlText)(positiveFeedback, targetDir, atom.id);
  const tasks = [];

  for (let i = 0, len = atom.tasks.length; i < len; i += 1) {
    const task = (0, _utils.markdownToHtml)(atom.tasks[i]);
    const id = `${atom.key}--${i}`;
    tasks.push({
      id,
      task
    });
  } // download feedback video if available


  const youtubeId = atom.video_feedback ? atom.video_feedback.youtube_id : '';
  const promiseHtmlVideo = (0, _utils2.createHtmlVideo)(youtubeId, targetDir, prefix, atom.title);
  const promiseLoadTemplate = (0, _templates.loadTemplate)('atom.taskList');
  const [video, html] = await Promise.all([promiseHtmlVideo, promiseLoadTemplate]);
  const hasFeedback = video || positiveFeedback;
  const dataTemplate = {
    description,
    hasFeedback,
    tasks,
    video,
    positiveFeedback
  };

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

  return template(dataTemplate);
}