"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DEFAULT_RULES = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _banner = require("../banner");
var _layout = require("../layout");
var _Body = require("../typography/Body");
var _BodyMonospace = require("../typography/BodyMonospace");
var _H = require("../typography/H1");
var _H2 = require("../typography/H2");
var _H3 = require("../typography/H3");
var _H4 = require("../typography/H4");
var _H5 = require("../typography/H5");
var _H6 = require("../typography/H6");
var _IOText = require("../typography/IOText");
var _CodeBlock = require("./CodeBlock");
var _ImageRenderer = require("./ImageRenderer");
var _utils = require("./utils");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/* ─── Inline flattening (shared between heading and paragraph rendering) ─── */

/**
 * Recursively walks inline AST nodes and produces flat styled segments
 * with accumulated bold/italic/link state.
 */
const flattenInlineNodes = (nodes, inherited) => nodes.reduce((acc, node) => {
  switch (node.type) {
    case "text":
    case "code_inline":
      return [...acc, {
        key: node.key,
        text: node.content ?? "",
        style: inherited
      }];
    case "softbreak":
    case "hardbreak":
      return [...acc, {
        key: node.key,
        text: "\n",
        style: inherited
      }];
    case "strong":
      return [...acc, ...flattenInlineNodes(node.children, {
        ...inherited,
        bold: true
      })];
    case "em":
      return [...acc, ...flattenInlineNodes(node.children, {
        ...inherited,
        italic: true
      })];
    case "link":
      {
        const href = node.attributes?.href;
        return [...acc, ...flattenInlineNodes(node.children, {
          ...inherited,
          link: href
        })];
      }
    default:
      return acc;
  }
}, []);

/**
 * Renders a single styled segment as either a raw string
 * or an IOText element with the appropriate props.
 */
const renderSegment = (segment, context, isCode) => {
  if (isCode) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_BodyMonospace.BodyMonospace, {
      children: segment.text
    }, segment.key);
  }
  const {
    bold,
    italic,
    link
  } = segment.style;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_IOText.IOText, {
    ...(bold ? {
      weight: "Semibold"
    } : {}),
    ...(italic ? {
      fontStyle: "italic"
    } : {}),
    ...(link ? {
      color: context.linkColor,
      onPress: () => context.onLinkPress?.(link),
      accessibilityRole: "link",
      textStyle: {
        textDecorationLine: "underline"
      }
    } : {}),
    size: context.fontSize,
    lineHeight: context.lineHeight,
    children: segment.text
  }, segment.key);
};

/* ─── Heading component map ─── */

const headingComponentMap = {
  heading1: _H.H1,
  heading2: _H2.H2,
  heading3: _H3.H3,
  heading4: _H4.H4,
  heading5: _H5.H5,
  heading6: _H6.H6
};

/* ─── Block rendering helpers ─── */

/**
 * Renders a paragraph block by flattening inline children
 * into styled segments.
 */
const renderParagraph = (node, context) => {
  const segments = flattenInlineNodes(node.children, {
    bold: false,
    italic: false
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Body.Body, {
    style: {
      textAlign: context.textAlign
    },
    children: segments.map(seg => {
      const matchingNode = node.children.find(c => c.key === seg.key);
      const isCode = matchingNode?.type === "code_inline";
      return renderSegment(seg, context, isCode);
    })
  }, node.key);
};

/**
 * Creates a render rule for a heading level using the corresponding
 * DS heading component (H1-H6). This ensures headings inherit
 * dynamicTypeRamp, uppercase/letterSpacing (H5), legacy typeface (H6),
 * and theme colors automatically.
 */
const makeHeadingRule = Heading => (node, _renderChildren, context) => {
  const segments = flattenInlineNodes(node.children, {
    bold: false,
    italic: false
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    accessibilityRole: "header",
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Heading, {
      style: {
        textAlign: context.textAlign
      },
      children: segments.map(seg => seg.text)
    })
  }, node.key);
};

/* ─── Default render rules ─── */

const paragraphRule = (node, _renderChildren, context) => renderParagraph(node, context);
const textRule = node => /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.Fragment, {
  children: node.content ?? ""
}, node.key);
const strongRule = (node, renderChildren) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_IOText.IOText, {
  weight: "Semibold",
  children: renderChildren(node.children)
}, node.key);
const emRule = (node, renderChildren) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_IOText.IOText, {
  fontStyle: "italic",
  children: renderChildren(node.children)
}, node.key);
const linkRule = (node, renderChildren, context) => {
  const href = node.attributes?.href;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_IOText.IOText, {
    color: context.linkColor,
    onPress: href ? () => context.onLinkPress?.(href) : undefined,
    accessibilityRole: "link",
    textStyle: {
      textDecorationLine: "underline"
    },
    children: renderChildren(node.children)
  }, node.key);
};
const softbreakRule = node => /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.Fragment, {
  children: "\n"
}, node.key);
const hardbreakRule = node => /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.Fragment, {
  children: "\n"
}, node.key);
const bulletListRule = (node, renderChildren) => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
  accessible: true,
  accessibilityRole: "list",
  style: {
    paddingLeft: 12
  },
  children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.VSpacer, {
    size: 8
  }), node.children.map(child => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
    style: {
      flexDirection: "row"
    },
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Body.Body, {
      children: (0, _utils.getUnorderedListBullet)(node.listDepth ?? 0)
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.HSpacer, {
      size: 8
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: {
        flex: 1,
        flexShrink: 1
      },
      children: renderChildren(child.children)
    })]
  }, child.key)), /*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.VSpacer, {
    size: 8
  })]
}, node.key);
const orderedListRule = (node, renderChildren) => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
  accessible: true,
  accessibilityRole: "list",
  style: {
    paddingLeft: 12
  },
  children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.VSpacer, {
    size: 8
  }), node.children.map((child, i) => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
    style: {
      flexDirection: "row"
    },
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Body.Body, {
      children: (0, _utils.getOrderedListMarker)(i + 1, node.listDepth ?? 0)
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.HSpacer, {
      size: 8
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: {
        flex: 1,
        flexShrink: 1
      },
      children: renderChildren(child.children)
    })]
  }, child.key)), /*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.VSpacer, {
    size: 8
  })]
}, node.key);
const listItemRule = (node, renderChildren) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
  style: {
    flex: 1,
    flexShrink: 1
  },
  children: renderChildren(node.children)
}, node.key);
const blockquoteRule = node => {
  const allText = (0, _utils.collectRawText)(node);
  const pictogramName = (0, _utils.extractPictogramName)(allText);

  // Find the first heading child for the banner title
  const headingNode = node.children.find(c => c.type.startsWith("heading"));
  const title = headingNode ? (0, _utils.collectRawText)(headingNode).trim() : undefined;

  // Collect content from paragraph children, stripping the pictogram pattern
  const content = node.children.filter(c => c.type === "paragraph").map(c => (0, _utils.stripPictogramPrefix)((0, _utils.collectRawText)(c)).trim()).filter(Boolean).join("\n");
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_banner.Banner, {
    pictogramName: pictogramName,
    color: "neutral",
    title: title,
    content: content || undefined
  }, node.key);
};
const imageRule = node => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
  style: {
    marginVertical: 16
  },
  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_ImageRenderer.ImageRenderer, {
    node: node
  })
}, node.key);
const codeInlineRule = node => /*#__PURE__*/(0, _jsxRuntime.jsx)(_BodyMonospace.BodyMonospace, {
  children: node.content ?? ""
}, node.key);
const fenceRule = node => /*#__PURE__*/(0, _jsxRuntime.jsx)(_CodeBlock.CodeBlock, {
  content: (node.content ?? "").trimEnd()
}, node.key);
const hrRule = node => /*#__PURE__*/(0, _jsxRuntime.jsx)(_layout.Divider, {}, node.key);
const htmlRule = node => {
  if (node.content && (0, _utils.isBrTag)(node.content)) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.Fragment, {
      children: "\n"
    }, node.key);
  }
  return null;
};

/**
 * The complete set of default render rules for all supported node types.
 */
const DEFAULT_RULES = exports.DEFAULT_RULES = {
  heading1: makeHeadingRule(headingComponentMap.heading1),
  heading2: makeHeadingRule(headingComponentMap.heading2),
  heading3: makeHeadingRule(headingComponentMap.heading3),
  heading4: makeHeadingRule(headingComponentMap.heading4),
  heading5: makeHeadingRule(headingComponentMap.heading5),
  heading6: makeHeadingRule(headingComponentMap.heading6),
  paragraph: paragraphRule,
  text: textRule,
  strong: strongRule,
  em: emRule,
  link: linkRule,
  softbreak: softbreakRule,
  hardbreak: hardbreakRule,
  bullet_list: bulletListRule,
  ordered_list: orderedListRule,
  list_item: listItemRule,
  blockquote: blockquoteRule,
  image: imageRule,
  code_inline: codeInlineRule,
  fence: fenceRule,
  hr: hrRule,
  html_block: htmlRule,
  html_inline: htmlRule
};
//# sourceMappingURL=rules.js.map