import mjml2html from "mjml-browser";
import editorToHtml from "./editorToHtml";
import renderComponentToHtml from "./renderComponentToHtml";
const getIfDoc = async (docOrObj) => {
  var _a;
  let output = docOrObj;
  try {
    if (typeof (docOrObj === null || docOrObj === void 0 ? void 0 : docOrObj.get) === "function") {
      const doc = await docOrObj.get();
      output = { id: doc === null || doc === void 0 ? void 0 : doc.id, ...(((_a = doc === null || doc === void 0 ? void 0 : doc.data) === null || _a === void 0 ? void 0 : _a.call(doc)) || {}) };
    }
  }
  catch (_b) {
    console.log("Error getting doc");
  }
  return output;
};
/**
 * Turn a template into compiled HTML
 */
export default async function templateToHtml(input) {
  var _a, _b, _c;
  const template = input === null || input === void 0 ? void 0 : input.template;
  const frame = await getIfDoc((input === null || input === void 0 ? void 0 : input.frame) || (template === null || template === void 0 ? void 0 : template.frame));
  const head = ((_a = input === null || input === void 0 ? void 0 : input.hydrate) === null || _a === void 0 ? void 0 : _a.head) || (frame === null || frame === void 0 ? void 0 : frame.head) || (template === null || template === void 0 ? void 0 : template.head) || "";
  const header = await getIfDoc((frame === null || frame === void 0 ? void 0 : frame.header) || (template === null || template === void 0 ? void 0 : template.header) || "");
  const footer = await getIfDoc((frame === null || frame === void 0 ? void 0 : frame.footer) || (template === null || template === void 0 ? void 0 : template.footer) || "");
  const body = await editorToHtml(template === null || template === void 0 ? void 0 : template.editor, {
    parserConfig: input === null || input === void 0 ? void 0 : input.parserConfig,
    customParsers: input === null || input === void 0 ? void 0 : input.customParsers,
    embedMarkup: input === null || input === void 0 ? void 0 : input.embedMarkup,
  });
  let html = [
    (!(input === null || input === void 0 ? void 0 : input.excludeHeader) && (header === null || header === void 0 ? void 0 : header.html)) || "",
    (frame === null || frame === void 0 ? void 0 : frame.id) ? `{{#> ${frame === null || frame === void 0 ? void 0 : frame.id} }}${body}{{/${frame === null || frame === void 0 ? void 0 : frame.id}}}` : body,
    (!(input === null || input === void 0 ? void 0 : input.excludeFooter) && (footer === null || footer === void 0 ? void 0 : footer.html)) || "",
  ].join("");
  try {
    if ((_b = input === null || input === void 0 ? void 0 : input.handlebars) === null || _b === void 0 ? void 0 : _b.compile)
      html = input.handlebars.compile(html)((input === null || input === void 0 ? void 0 : input.data) || (template === null || template === void 0 ? void 0 : template.sampleData) || {});
  }
  catch (e) {
    console.log("Error rendering handlebars: ", e);
  }
  try {
    if (input === null || input === void 0 ? void 0 : input.mjml)
      html = (_c = mjml2html(html, (typeof (input === null || input === void 0 ? void 0 : input.mjml) === "object" && input.mjml) || {})) === null || _c === void 0 ? void 0 : _c.html;
  }
  catch (e) {
    console.log("Error rendering MJML", e);
  }
  try {
    if (!(input === null || input === void 0 ? void 0 : input.disableRender))
      html = await renderComponentToHtml(html, {
        ...((input === null || input === void 0 ? void 0 : input.hydrate) || {}),
        head,
      });
  }
  catch (e) {
    console.log("Error rendering components", e);
  }
  return html;
}
