import { renderToString } from "@ionic/core/hydrate";
/**
 * render a component to HTML string
 */
export default async function renderComponentToHtml(html, options = {}) {
  if (!html)
    throw Error("HTML is required");
  const renderedComponent = await renderToString(html, options.hydrateOptions
    ? options.hydrateOptions
    : {
      prettyHtml: true,
      removeScripts: true,
    });
  let renderedHtml = (options === null || options === void 0 ? void 0 : options.head) && (renderedComponent === null || renderedComponent === void 0 ? void 0 : renderedComponent.html)
    ? renderedComponent.html.replace(`<meta charset="utf-8">`, `<meta charset="utf-8">
    ${(options === null || options === void 0 ? void 0 : options.head) || ""}
    `)
    : (renderedComponent === null || renderedComponent === void 0 ? void 0 : renderedComponent.html) || "";
  if (options.replace) {
    for (const key of Object.keys(options.replace)) {
      renderedHtml = renderedHtml.replace(new RegExp(key, "g"), options.replace[key]);
    }
  }
  renderedHtml = renderedHtml
    .replace(new RegExp("{{&gt;", "g"), "{{>")
    .replace(new RegExp("{{#&gt;", "g"), "{{#>");
  return options.partial && renderedHtml.includes("</body>")
    ? /<body[^>]*>((.|[\n\r])*)<\/body>/im.exec(renderedHtml)[1]
    : renderedHtml;
}
