"use strict";

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

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

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

var _index = require("./index");

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

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

/**
 * Create HTML for ImageFormQuestion type of QuizAtom
 * @param {object} atom atom JSON
 * @param {string} outputPath path to save the assets folder for videos
 */
async function createHtmlQuizImageFormQuestion(atom, outputPath) {
  // create directory for image assets
  const pathImg = (0, _utils.makeDir)(outputPath, 'img');
  const {
    question
  } = atom; // download quiz background image

  const filename = `${atom.question.evaluation_id}.gif`;
  const promiseDownloadImage = (0, _utils.downloadImage)(question.background_image, pathImg, filename);
  const promiseLoadTemplate = (0, _templates.loadTemplate)('atom.quiz.imageFormQuestion');
  const [filenameImg, html] = await Promise.all([promiseDownloadImage, promiseLoadTemplate]);
  let htmlWidgets = '';

  for (let i = 0, len = question.widgets.length; i < len; i += 1) {
    const widget = question.widgets[i];
    htmlWidgets += (0, _index.createHtmlWidget)(widget);
  }

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

  const dataTemplate = {
    htmlWidgets,
    srcImg: `img/${filenameImg}`
  };
  return template(dataTemplate);
}