"use strict";

import { useCallback, useMemo } from "react";
import { Linking, View } from "react-native";
import { useIOTheme } from "../../context";
import { bodyFontSize, bodyLineHeight, bodySmallFontSize, bodySmallLineHeight } from "../typography";
import { parse } from "./parser";
import { DEFAULT_RULES } from "./rules";
import { jsx as _jsx } from "react/jsx-runtime";
/**
 * Full-featured markdown component that renders markdown content
 * using design system primitives.
 *
 * @remarks
 * This component is still experimental. Check that it is correctly
 * formatting your text before proceeding to use it.
 *
 * Supports headings, paragraphs, bold, italic, links, lists,
 * blockquotes (as Banner), images, code, horizontal rules, and HTML breaks.
 *
 * Individual node types can be disabled via `disabledRules`, and
 * render rules can be overridden via the `rules` prop.
 */
export const IOMarkdown = ({
  content,
  onLinkPress,
  textAlign,
  small,
  testID,
  disabledRules,
  rules = {}
}) => {
  const theme = useIOTheme();
  const ast = useMemo(() => parse(content, disabledRules), [content, disabledRules]);
  const handleLinkPress = useCallback(url => {
    if (onLinkPress) {
      onLinkPress(url);
    } else {
      Linking.openURL(url).catch(() => null);
    }
  }, [onLinkPress]);
  const context = useMemo(() => ({
    onLinkPress: handleLinkPress,
    linkColor: theme["interactiveElem-default"],
    textAlign: textAlign ?? "auto",
    fontSize: small ? bodySmallFontSize : bodyFontSize,
    lineHeight: small ? bodySmallLineHeight : bodyLineHeight
  }), [handleLinkPress, textAlign, small, theme]);
  const mergedRules = useMemo(() => ({
    ...DEFAULT_RULES,
    ...rules
  }), [rules]);
  const renderChildren = useCallback(nodes => nodes.map(node => {
    const rule = mergedRules[node.type];
    return rule ? rule(node, renderChildren, context) : null;
  }), [mergedRules, context]);
  const rendered = renderChildren(ast);
  return /*#__PURE__*/_jsx(View, {
    style: {
      gap: 8
    },
    testID: testID,
    children: rendered
  });
};
//# sourceMappingURL=IOMarkdown.js.map