import { Text, Linking } from 'react-native';
import { useStyles } from './styles';
import { memo, useCallback } from 'react';
import { useNavigation } from '@react-navigation/native';
import ReadMore from '@fawazahmed/react-native-read-more';
import { createUrlRegex } from '../../../core/constants';
const RenderTextWithMention = ({
  mentionPositionArr,
  textPost,
  isTitle
}) => {
  const styles = useStyles();
  const navigation = useNavigation();
  const linkArr = useCallback(text => {
    const links = [];
    const urlRegex = 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 = useCallback(userId => {
    navigation.navigate('UserProfile', {
      userId: userId
    });
  }, [navigation]);
  const handleLinkClick = useCallback(async url => {
    try {
      const hasProtocol = /^(https?|ftp|mailto):/.test(url);
      const formattedUrl = hasProtocol ? url : `https://${url}`;
      await Linking.openURL(formattedUrl);
    } catch (error) {
      console.warn('Failed to open URL:', error);
    }
  }, []);
  const handleOnClick = useCallback((link, userId) => {
    if (link) return handleLinkClick(link);
    return mentionClick(userId);
  }, [handleLinkClick, mentionClick]);
  if (heightlightTextPositions.length === 0) {
    return /*#__PURE__*/React.createElement(ReadMore, {
      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(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(Text, {
    selectable: true,
    key: "nonHighlighted-last",
    style: [styles.inputText, isTitle && styles.bold]
  }, remainingText)]);

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