"use strict";

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

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

var _path = _interopRequireDefault(require("path"));

var _atoms = require("./atoms");

var _ = require(".");

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

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

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

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

/**
 * Write lesson concept content to file
 * @param {object} concept contain lesson's concept to convert to html
 * @param {object} next concept
 * @param {string} htmlSidebar sidebar html content
 * @param {string} targetDir output directory path
 * @param {number} index the numbering of the concept for producing file prefix
 * @param {function} doneLesson callback function of lesson loop
 */
async function writeHtmlConcept(concept, nextConcept, htmlSidebar, targetDir, index) {
  let contentMain = '';
  const conceptTitle = concept.title || ''; // prefix for file names

  const prefix = index < 10 ? `0${index}` : index;

  try {
    for (let i = 0, len = concept.atoms.length; i < len; i += 1) {
      const atom = concept.atoms[i];
      const atomTitle = (0, _utils.markdownToHtml)(atom.title);
      let promiseAtom;
      const semanticType = atom.semantic_type;
      const instructorNote = await (0, _utils2.createHtmlText)(atom.instructor_notes, targetDir, `${atom.id}-instructor-note`);

      if (semanticType === 'ImageAtom') {
        promiseAtom = (0, _atoms.createHtmlImageAtom)(atom, targetDir);
      } else if (semanticType === 'TaskListAtom') {
        promiseAtom = (0, _atoms.createHtmlTaskListAtom)(atom, targetDir, prefix);
      } else if (semanticType === 'TextAtom') {
        promiseAtom = (0, _atoms.createHtmlTextAtom)(atom, targetDir);
      } else if (semanticType === 'VideoAtom') {
        promiseAtom = (0, _atoms.createHtmlVideoAtom)(atom, targetDir, prefix);
      } else if (semanticType === 'CheckboxQuizAtom') {
        promiseAtom = (0, _atoms.createHtmlCheckboxQuizAtom)(atom, targetDir);
      } else if (semanticType === 'MatchingQuizAtom') {
        promiseAtom = (0, _atoms.createHtmlMatchingQuizAtom)(atom, targetDir);
      } else if (semanticType === 'RadioQuizAtom') {
        promiseAtom = (0, _atoms.createHtmlRadioQuizAtom)(atom, targetDir);
      } else if (semanticType === 'ReflectAtom') {
        promiseAtom = (0, _atoms.createHtmlReflectAtom)(atom, targetDir, prefix);
      } else if (semanticType === 'QuizAtom') {
        promiseAtom = (0, _atoms.createHtmlQuizAtom)(atom, targetDir, prefix);
      } else if (semanticType === 'ValidatedQuizAtom') {
        promiseAtom = (0, _atoms.createHtmlValidatedQuizAtom)(atom, targetDir);
      } else if (semanticType === 'WorkspaceAtom') {
        promiseAtom = (0, _atoms.createHtmlWorkspaceAtom)(atom);
      } else {
        const msg = 'Unknown lesson atom type. Please contact the developer to make it compatible with this atom type!';
        promiseAtom = new Promise(resolve => resolve(msg));
      }

      const [htmlAtom, htmlTemplate] = await Promise.all([promiseAtom, (0, _templates.loadTemplate)('atom')]);

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

      const dataTemplate = {
        atomTitle,
        instructorNote,
        htmlAtom
      };
      contentMain += template(dataTemplate);
    } // create HTML body content


    const title = `${prefix}. ${conceptTitle}`; // decide how many folders it needs to go up to access the assets

    const pathToAssets = '../'; // write html file

    const templateDataIndex = {
      contentMain,
      docTitle: conceptTitle,
      sidebar: htmlSidebar,
      pathToAssets,
      title
    };

    if (nextConcept) {
      const nextIndex = index + 1;
      const nextPrefix = nextIndex < 10 ? `0${nextIndex}` : nextIndex;
      const nextConceptTitle = nextConcept.title || '';
      const nextFile = (0, _utils.filenamify)(nextConceptTitle);
      templateDataIndex.nextConceptFile = `${nextPrefix}. ${nextFile}.html`;
    }

    let file = (0, _utils.filenamify)(conceptTitle);
    file = _path.default.join(targetDir, `${prefix}. ${file}.html`);
    await (0, _.writeHtml)(templateDataIndex, file);

    _utils.logger.info(`Completed rendering lesson file ${file}`);

    _utils.logger.info('____________________\n');
  } catch (error) {
    throw error;
  }
}