"use strict";

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

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

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

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

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

/**
 * Create HTML content for RadioQuizAtom
 * @param {object} atom atom json
 * @param {string} targetDir output directory path
 * @returns {string} HTML content
 */
async function createHtmlRadioQuizAtom(atom, targetDir) {
  const prompt = await (0, _utils.createHtmlText)(atom.question.prompt, targetDir, atom.id);
  const answers = [];
  let solution;

  for (let i = 0, len = atom.question.answers.length; i < len; i += 1) {
    const answer = atom.question.answers[i];
    const {
      id
    } = answer;
    const isCorrect = answer.is_correct;
    const text = await (0, _utils.createHtmlText)(answer.text, targetDir, atom.id); // if this is the correct answer, add to solution

    if ('is_correct' in answer && isCorrect !== null && isCorrect === true) {
      solution = answer;
    }

    answers.push({
      id,
      name: atom.id,
      text
    });
  }

  const html = await (0, _templates.loadTemplate)('atom.radioQuiz');
  const dataTemplate = {
    answers,
    solution,
    prompt
  };

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

  return template(dataTemplate);
}