"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.formatText = exports.extractTrailingImageMarkdown = void 0;
var _ui = require("@mirai/ui");
var _react = _interopRequireDefault(require("react"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const IMAGE_MARKDOWN_REGEX = /!\[image\]\((https?:\/\/[^\s)]+)\)\s*$/;
const LINK_TOKEN_REGEX = /\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)|(https?:\/\/[^\s]+)/g;
const TRAILING_PUNCTUATION_REGEX = /[.,!?;:]$/;
const renderTextWithLineBreaks = (value, keyPrefix) => {
  const lines = value.split('\n');
  if (lines.length === 1) return value;
  return lines.flatMap((line, index) => {
    if (index === lines.length - 1) return line;
    return [line, /*#__PURE__*/_react.default.createElement("br", {
      key: "".concat(keyPrefix, "-br-").concat(index)
    })];
  });
};
const extractTrailingImageMarkdown = text => {
  if (!text) return {
    imageUrl: undefined,
    text
  };
  const match = text.match(IMAGE_MARKDOWN_REGEX);
  if (!match) return {
    imageUrl: undefined,
    text
  };
  const [, imageUrl] = match;
  const nextText = text.replace(IMAGE_MARKDOWN_REGEX, '').replace(/[ \t]+$/g, '');
  return {
    imageUrl,
    text: nextText
  };
};
exports.extractTrailingImageMarkdown = extractTrailingImageMarkdown;
const formatText = text => {
  if (!text) return text;
  const hasNewlines = text.includes('\n');
  const hasLinks = LINK_TOKEN_REGEX.test(text);
  LINK_TOKEN_REGEX.lastIndex = 0;
  if (!hasLinks && !hasNewlines) return text;
  if (!hasLinks) return renderTextWithLineBreaks(text, 'text');
  const output = [];
  let lastIndex = 0;
  let match;
  while ((match = LINK_TOKEN_REGEX.exec(text)) !== null) {
    const [rawMatch, markdownLabel, markdownUrl, rawUrl] = match;
    const matchIndex = match.index;
    if (matchIndex > lastIndex) {
      const beforeText = text.slice(lastIndex, matchIndex);
      const renderedBefore = renderTextWithLineBreaks(beforeText, "text-".concat(matchIndex));
      if (Array.isArray(renderedBefore)) {
        output.push(...renderedBefore);
      } else {
        output.push(renderedBefore);
      }
    }
    const linkUrl = (markdownUrl || rawUrl).replace(TRAILING_PUNCTUATION_REGEX, '');
    const linkLabel = markdownLabel || linkUrl;
    output.push(/*#__PURE__*/_react.default.createElement(_ui.Action, {
      key: "link-".concat(matchIndex),
      href: linkUrl,
      paragraph: true,
      rel: "noopener noreferrer",
      target: "_blank",
      underline: true
    }, linkLabel));
    lastIndex = matchIndex + rawMatch.length;
  }
  if (lastIndex < text.length) {
    const remainingText = text.slice(lastIndex);
    const renderedRemaining = renderTextWithLineBreaks(remainingText, "text-".concat(lastIndex));
    if (Array.isArray(renderedRemaining)) {
      output.push(...renderedRemaining);
    } else {
      output.push(renderedRemaining);
    }
  }
  return output;
};
exports.formatText = formatText;
//# sourceMappingURL=formatText.js.map