"use strict";

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

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 WorkspaceAtom
 * @param {object} atom atom json
 * @returns {string} HTML content
 */
async function createHtmlWorkspaceAtom(atom) {
  if (!atom) {
    return '(No Workspace data available)';
  }

  const {
    configuration
  } = atom;
  let defaultPath = '';
  let openFiles = [];
  let userCode;
  let kind = 'Unknown'; // attempt to get workspace information

  if (configuration && configuration.blueprint) {
    ({
      kind
    } = configuration.blueprint);
    const {
      conf
    } = configuration.blueprint;

    if (conf) {
      // Jupyter workspace usually has defaultPath
      if (conf.defaultPath) ({
        defaultPath
      } = conf.defaultPath); // Coding workspace usually has openFiles

      if (conf.openFiles && conf.openFiles.length) {
        ({
          openFiles
        } = conf.openFiles);
      }

      ({
        userCode
      } = conf);
      userCode = (0, _utils.markdownToHtml)(userCode);
    } //.if conf

  } //.if configuration


  const html = await (0, _templates.loadTemplate)('atom.workspace');
  const data = {
    defaultPath,
    kind,
    openFiles,
    userCode
  };

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

  return template(data);
}