import * as Handlebars from "handlebars";
import getHandlebarsHelpers from "./getHandlebarsHelpers";
let currentHelpers = [];
/**
 * Render a lump of handlebars
 */
export default function renderHandlebars(templateStr, data, options) {
  for (const partial of (options === null || options === void 0 ? void 0 : options.partials) || []) {
    Handlebars.registerPartial(partial.id, (partial === null || partial === void 0 ? void 0 : partial.html) || "");
  }
  try {
    const helpers = Object.keys((options === null || options === void 0 ? void 0 : options.helpers) || []).length
      ? options.helpers
      : getHandlebarsHelpers();
    for (const [helperName, helperFn] of Object.entries(helpers)) {
      try {
        if (currentHelpers.includes(helperName))
          Handlebars.unregisterHelper(helperName);
        Handlebars.registerHelper(helperName, helperFn);
        if (!currentHelpers.includes(helperName))
          currentHelpers = [...currentHelpers, helperName];
      }
      catch (error) {
        console.log(`Error setting helper ${helperName}.`, error);
      }
    }
  }
  catch (_a) {
    console.log("Error setting helpers.");
  }
  return Handlebars.compile(templateStr)(data);
}
//# sourceMappingURL=renderHandlebars.js.map