"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _clsx = require("clsx");
var _react = _interopRequireWildcard(require("react"));
var _core = require("./core");
var _DebugPanel = _interopRequireDefault(require("./DebugPanel"));
var _hooks = require("./hooks");
var _tail = require("./utils/tail");
require("./index.css");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
const XMarkdown = /*#__PURE__*/_react.default.memo(props => {
  const {
    streaming,
    config,
    components,
    paragraphTag,
    content,
    children,
    rootClassName,
    className,
    style,
    openLinksInNewTab,
    dompurifyConfig,
    protectCustomTagNewlines,
    escapeRawHtml,
    debug
  } = props;
  const tailContent = (0, _react.useMemo)(() => (0, _tail.resolveTailContent)(streaming?.tail), [streaming?.tail]);
  const TailComponent = typeof streaming?.tail === 'object' ? streaming.tail.component : undefined;
  const shouldShowTail = !!streaming?.hasNextChunk && tailContent;

  // ============================ style ============================
  const mergedCls = (0, _clsx.clsx)('x-markdown', rootClassName, className);

  // ============================ Streaming ============================
  const output = (0, _hooks.useStreaming)(content || children || '', {
    streaming,
    components
  });

  // ============================ Merge components with xmd-tail ============================
  const mergedComponents = (0, _react.useMemo)(() => {
    if (!shouldShowTail) {
      return components;
    }
    const TailElement = TailComponent ? ( /*#__PURE__*/_react.default.createElement(TailComponent, {
      content: tailContent
    })) : /*#__PURE__*/_react.default.createElement("span", {
      className: "xmd-tail"
    }, tailContent);
    return {
      ...components,
      'xmd-tail': () => TailElement
    };
  }, [shouldShowTail, components, TailComponent, tailContent]);

  // ============================ Render ============================
  const parser = (0, _react.useMemo)(() => new _core.Parser({
    markedConfig: config,
    paragraphTag,
    openLinksInNewTab,
    components: mergedComponents,
    protectCustomTagNewlines,
    escapeRawHtml
  }), [config, paragraphTag, openLinksInNewTab, mergedComponents, protectCustomTagNewlines, escapeRawHtml]);
  const renderer = (0, _react.useMemo)(() => new _core.Renderer({
    components: mergedComponents,
    dompurifyConfig,
    streaming
  }), [mergedComponents, dompurifyConfig, streaming]);
  const htmlString = (0, _react.useMemo)(() => {
    if (!output) {
      return '';
    }
    return parser.parse(output, {
      injectTail: !!shouldShowTail
    });
  }, [output, parser, shouldShowTail]);
  const renderedContent = (0, _react.useMemo)(() => htmlString ? renderer.render(htmlString) : null, [htmlString, renderer]);
  if (!output) {
    return null;
  }
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
    className: mergedCls,
    style: style
  }, renderedContent), debug ? /*#__PURE__*/_react.default.createElement(_DebugPanel.default, null) : null);
});
if (process.env.NODE_ENV !== 'production') {
  XMarkdown.displayName = 'XMarkdown';
}
var _default = exports.default = XMarkdown;