"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require$$0 = require("react/jsx-runtime");
const designSystem = require("@choiceform/design-system");
const React = require("react");
const index = require("./hooks/index.cjs");
const tv = require("./tv.cjs");
const markdownRenderers = require("./components/markdown-renderers.cjs");
const markdownBlock = require("./components/markdown-block.cjs");
const defaultFilename = require("./utils/default-filename.cjs");
const index$1 = require("../code-block/index.cjs");
function extractLanguage(className) {
  if (!className) return "plaintext";
  const match = className.match(/language-(\w+)/);
  return match ? match[1] : "plaintext";
}
const INITIAL_COMPONENTS = {
  code: function CodeComponent({ className, children, filename, ...props }) {
    const isInline = !props.node?.position?.start?.line || props.node?.position?.start?.line === props.node?.position?.end?.line;
    if (isInline) {
      return /* @__PURE__ */ require$$0.jsx("code", { ...props, children });
    }
    const language = extractLanguage(className);
    const effectiveFilename = filename ?? defaultFilename.getDefaultFilenameForLanguage(language);
    return /* @__PURE__ */ require$$0.jsxs(
      index$1.CodeBlock,
      {
        language,
        filename: effectiveFilename,
        children: [
          /* @__PURE__ */ require$$0.jsx(index$1.CodeBlock.Header, {}),
          /* @__PURE__ */ require$$0.jsx(index$1.CodeBlock.BlockCode, { code: children }),
          /* @__PURE__ */ require$$0.jsx(index$1.CodeBlock.Footer, {})
        ]
      }
    );
  }
};
const Markdown = React.memo(function Markdown2(props) {
  const { children, id, className, components: customComponents, mode = "default" } = props;
  const generatedId = React.useId();
  const blockId = id ?? generatedId;
  const blocks = index.useMarkdownBlocks(children);
  const tv$1 = React.useMemo(() => tv.mdTv({ mode }), [mode]);
  const tvComponents = React.useMemo(() => markdownRenderers.createComponents(tv$1), [tv$1]);
  const components = React.useMemo(() => {
    const base = {
      ...tvComponents,
      ...customComponents,
      ...INITIAL_COMPONENTS
    };
    return base;
  }, [tvComponents, customComponents]);
  return /* @__PURE__ */ require$$0.jsx("div", { className: designSystem.tcx(tv$1.root(), className), children: blocks.map((block, index2) => /* @__PURE__ */ require$$0.jsx(
    markdownBlock.MarkdownBlock,
    {
      content: block,
      components
    },
    `${blockId}-block-${index2}`
  )) });
});
exports.Markdown = Markdown;
//# sourceMappingURL=markdown.cjs.map
