"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _styles = require("./styles");
var _react = require("react");
var _native = require("@react-navigation/native");
var _reactNativeReadMore = _interopRequireDefault(require("@fawazahmed/react-native-read-more"));
var _constants = require("../../../core/constants");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const RenderTextWithMention = ({
  mentionPositionArr,
  textPost,
  isTitle
}) => {
  const styles = (0, _styles.useStyles)();
  const navigation = (0, _native.useNavigation)();
  const linkArr = (0, _react.useCallback)(text => {
    const links = [];
    const urlRegex = (0, _constants.createUrlRegex)();
    let match;
    while ((match = urlRegex.exec(text)) !== null) {
      links.push({
        link: match[0],
        index: match.index,
        length: match[0].length
      });
    }
    return links;
  }, []);
  const heightlightTextPositions = [...linkArr(textPost), ...mentionPositionArr].sort((a, b) => a.index - b.index);
  const mentionClick = (0, _react.useCallback)(userId => {
    navigation.navigate('UserProfile', {
      userId: userId
    });
  }, [navigation]);
  const handleLinkClick = (0, _react.useCallback)(async url => {
    try {
      const hasProtocol = /^(https?|ftp|mailto):/.test(url);
      const formattedUrl = hasProtocol ? url : `https://${url}`;
      await _reactNative.Linking.openURL(formattedUrl);
    } catch (error) {
      console.warn('Failed to open URL:', error);
    }
  }, []);
  const handleOnClick = (0, _react.useCallback)((link, userId) => {
    if (link) return handleLinkClick(link);
    return mentionClick(userId);
  }, [handleLinkClick, mentionClick]);
  if (heightlightTextPositions.length === 0) {
    return /*#__PURE__*/React.createElement(_reactNativeReadMore.default, {
      key: textPost,
      numberOfLines: 8,
      seeMoreStyle: styles.moreLessButton,
      style: [styles.inputText, isTitle && styles.bold],
      seeLessStyle: styles.moreLessButton,
      seeLessText: ""
    }, textPost);
  }
  let currentPosition = 0;
  const result = heightlightTextPositions.map(({
    index,
    length,
    userId,
    link
  }, i) => {
    // Add non-highlighted text before the mention
    const nonHighlightedText = textPost.slice(currentPosition, index);
    // Add highlighted text
    const highlightedText = /*#__PURE__*/React.createElement(_reactNative.Text, {
      onPress: () => handleOnClick(link, userId),
      key: `highlighted-${i}`,
      style: styles.mentionText,
      selectable: true
    }, textPost.slice(index, index + length));

    // Update currentPosition for the next iteration
    currentPosition = index + length;

    // Return an array of non-highlighted and highlighted text
    return [nonHighlightedText, highlightedText];
  });

  // Add any remaining non-highlighted text after the mentions
  const remainingText = textPost.slice(currentPosition);
  result.push([/*#__PURE__*/React.createElement(_reactNative.Text, {
    selectable: true,
    key: "nonHighlighted-last",
    style: [styles.inputText, isTitle && styles.bold]
  }, remainingText)]);

  // Flatten the array and render
  return /*#__PURE__*/React.createElement(_reactNative.Text, {
    selectable: true,
    style: [styles.inputText, isTitle && styles.bold]
  }, result.flat());
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(RenderTextWithMention);
//# sourceMappingURL=RenderTextWithMention.js.map