'use strict';
import React from 'react';
import { apiPost } from '../../../fd-toolbox/api/api-client.js';
import { apiPathNames } from '../../../fd-toolbox/api/api-path-names.js';
import { logStringError } from '../../../fd-toolbox/logging/loggers.js';
import { isNullOrUndefined } from '../../../fd-toolbox/functions/value-checking-functions.js';
import { apiErrorMessages } from '../../../fd-toolbox/constants/api-error-constants.js';
import { htmlTags } from '../../../base-repo/constants/html-element-names.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
const allowedTags = /* @__PURE__ */new Set(["p", "strong", "em", "del", "h1", "h2", htmlTags.h3, "h4", "h5", "h6", "li", "ol", "ul", htmlTags.a, "blockquote", "pre", htmlTags.code, "hr"]);
async function markdownToHtml(markdown) {
  const response = await apiPost(apiPathNames.markdown, {
    markdown
  });
  const responseIsSuccessful = !isNullOrUndefined(response.data?.html);
  if (responseIsSuccessful) {
    return response.data?.html;
  } else {
    logStringError(response.message ?? apiErrorMessages.invalidResponseFromApi);
    return void 0;
  }
}
__name(markdownToHtml, "markdownToHtml");
async function parseMarkdown(markdown) {
  const htmlOutput = await markdownToHtml(markdown);
  return parseText(htmlOutput);
}
__name(parseMarkdown, "parseMarkdown");
function parseText(htmlString) {
  const doc = new DOMParser().parseFromString(htmlString ?? "", "text/html");
  return validateHtml(doc.body.childNodes);
}
__name(parseText, "parseText");
function validateHtml(nodes) {
  const result = [];
  for (const node of Array.from(nodes)) {
    if (node instanceof Text) result.push(node.wholeText);else if (node instanceof HTMLElement && allowedTags.has(node.tagName.toLowerCase())) {
      const tagName = node.tagName.toLowerCase();
      if (node.childNodes.length > 0) {
        const childNodes = validateHtml(node.childNodes);
        if (childNodes.length) {
          result.push(React.createElement(tagName, {
            key: result.length
          }, ...childNodes));
        }
      } else result.push(React.createElement(tagName, {
        key: result.length
      }));
    } else if (node instanceof HTMLElement) {
      const dummy = document.createElement(htmlTags.div);
      dummy.appendChild(node);
      result.push(dummy.textContent);
    }
  }
  return result;
}
__name(validateHtml, "validateHtml");

export { markdownToHtml, parseMarkdown, parseText };
