"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.IOMarkdown = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _context = require("../../context");
var _typography = require("../typography");
var _parser = require("./parser");
var _rules = require("./rules");
var _jsxRuntime = require("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.
 */
const IOMarkdown = ({
  content,
  onLinkPress,
  textAlign,
  small,
  testID,
  disabledRules,
  rules = {}
}) => {
  const theme = (0, _context.useIOTheme)();
  const ast = (0, _react.useMemo)(() => (0, _parser.parse)(content, disabledRules), [content, disabledRules]);
  const handleLinkPress = (0, _react.useCallback)(url => {
    if (onLinkPress) {
      onLinkPress(url);
    } else {
      _reactNative.Linking.openURL(url).catch(() => null);
    }
  }, [onLinkPress]);
  const context = (0, _react.useMemo)(() => ({
    onLinkPress: handleLinkPress,
    linkColor: theme["interactiveElem-default"],
    textAlign: textAlign ?? "auto",
    fontSize: small ? _typography.bodySmallFontSize : _typography.bodyFontSize,
    lineHeight: small ? _typography.bodySmallLineHeight : _typography.bodyLineHeight
  }), [handleLinkPress, textAlign, small, theme]);
  const mergedRules = (0, _react.useMemo)(() => ({
    ..._rules.DEFAULT_RULES,
    ...rules
  }), [rules]);
  const renderChildren = (0, _react.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__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    style: {
      gap: 8
    },
    testID: testID,
    children: rendered
  });
};
exports.IOMarkdown = IOMarkdown;
//# sourceMappingURL=IOMarkdown.js.map