"use strict";

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

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

var _fsExtra = _interopRequireDefault(require("fs-extra"));

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

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

var _ = require(".");

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

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

function createHtmlConceptSummary(concepts, linkLesson) {
  if (!concepts) return '';
  let html = '';

  for (let i = 0, len = concepts.length; i < len; i += 1) {
    const concept = concepts[i];
    const numbering = i + 1 < 10 ? `0${i + 1}` : i + 1;
    const prefix = `Concept ${numbering}`;
    const title = concept.title ? concept.title : '';

    const link = _path.default.join(linkLesson, (0, _utils.filenamify)(`${numbering}. ${title}.html`));

    html += `
      <li>
        <a href="${link}"><strong>${prefix}:</strong> ${title}</a>
      </li>
    `;
  }

  return html;
}

function createHtmlCourseLessonSummary(lessons) {
  if (!lessons) return '';
  let html = '';

  for (let i = 0, len = lessons.length; i < len; i += 1) {
    const lesson = lessons[i];
    const numbering = i + 1 < 10 ? `0${i + 1}` : i + 1;
    const prefix = `Lesson ${numbering}`;
    const pathLesson = `${prefix}_${(0, _utils.filenamify)(lesson.title.trim())}`;

    const link = _path.default.join(pathLesson, 'index.html');

    const summary = lesson.summary ? `<p>${(0, _utils.markdownToHtml)(lesson.summary)}` : '';
    const htmlConcepts = createHtmlConceptSummary(lesson.concepts, pathLesson);
    html += `
    <li>
      <details>
        <summary>
          <a href="${link}"><strong>${prefix}:</strong> ${lesson.title}</a>
          ${summary}
        </summary>
        <ul>
          ${htmlConcepts}
        </ul>
      </details>
    </li>
    `;
  }

  return `<ul>${html}</ul>`;
}

async function writeHtmlCourseSummary(jsonPath, targetDir, courseName) {
  const pathData = _path.default.join(jsonPath, 'data.json');

  let course; // read json file for data

  try {
    let data = await _fsExtra.default.readFile(pathData, 'utf-8');
    data = JSON.parse(data);
    ({
      course
    } = data.data);
  } catch (error) {
    throw error;
  } // loop through lessons to create the table of course content


  const htmlCourseLessons = createHtmlCourseLessonSummary(course.lessons);
  const html = await (0, _templates.loadTemplate)('summary.course');
  const {
    key,
    version,
    title,
    locale
  } = course;
  const templateDataCourseSummary = {
    key,
    locale,
    htmlCourseLessons,
    version
  };

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

  const contentMain = template(templateDataCourseSummary);
  const templateDataIndex = {
    contentMain,
    docTitle: courseName,
    pathToAssets: '',
    title
  };

  const filePath = _path.default.join(targetDir, 'index.html');

  await (0, _.writeHtml)(templateDataIndex, filePath);
}