"use strict";

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

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 MatchingQuizAtom
 * @param {object} atom atom json
 * @returns {string} HTML content
 */
async function createHtmlMatchingQuizAtom(atom) {
  const {
    question
  } = atom;
  const prompt = (0, _utils.markdownToHtml)(question.complex_prompt.text);
  const concepts = [];
  const answers = [];
  const solutions = [];
  const conceptsLabel = (0, _utils.markdownToHtml)(question.concepts_label);
  const answersLabel = (0, _utils.markdownToHtml)(question.answers_label);

  for (let i = 0, len = question.concepts.length; i < len; i += 1) {
    const concept = question.concepts[i];
    concepts.push({
      text: (0, _utils.markdownToHtml)(concept.text)
    });
  }

  for (let j = 0, len = question.answers.length; j < len; j += 1) {
    const answer = question.answers[j];
    answers.push({
      text: (0, _utils.markdownToHtml)(answer.text)
    }); // create solution

    for (let k = 0, lenConcepts = question.concepts.length; k < lenConcepts; k += 1) {
      const concept = question.concepts[k];
      const correctAnswer = concept.correct_answer;

      if (correctAnswer && correctAnswer.text && correctAnswer.text.toLowerCase().trim() === answer.text.toLowerCase().trim()) {
        solutions.push({
          answerText: (0, _utils.markdownToHtml)(answer.text),
          matchingConcept: (0, _utils.markdownToHtml)(concept.text)
        });
      }
    }
  }

  const html = await (0, _templates.loadTemplate)('atom.matchingQuiz');
  const dataTemplate = {
    answers,
    answersLabel,
    concepts,
    conceptsLabel,
    solutions,
    prompt
  };

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

  return template(dataTemplate);
}